Blackberry - how to animate Custom Field? - blackberry

I have created a custom field class "Seek" to draw a fillrectangle.
class Seek extends Field {
int fill;
protected void layout(int width, int height) {
setExtent(320, 5);
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.RED);
graphics.fillRect(0, 0, fill, 5);
}
protected void setValue(int value) {
fill = value;
}
}
And I have created another class Test seek to set the fill value using a timer
public class TestSeek extends UiApplication {
public static void main(String[] args) {
TestSeek gbResults = new TestSeek();
gbResults.enterEventDispatcher();
}
private TestSeek() {
pushScreen(new ProgressScreen());
}
}
class ProgressScreen extends MainScreen {
Timer timer = new Timer();
int i = 80;
Seek SeekField = new Seek();
public ProgressScreen() {
add(SeekField);
timer.schedule(new RemindTask(), 100, 10);
}
class RemindTask extends TimerTask {
public void run() {
if (i < 320) {
i += 1;
SeekField.setValue(i);
} else
timer.cancel();
}
}
}
But I am unable to animate filling the rectangle.

Try calling invalidate() in your setValue method.

Related

Setting selected value in editfield?

I have made a custom auto suggest list.
I want to set the selcted value in edit field.
I am using this Place holder text on a AutoCompleteField in blackberry
CustomAutoCompleteField.js
package mypackage;
import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.collection.util.BasicFilteredListResult;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.ListField;
class CustomAutoCompleteField extends AutoCompleteField {
private int yOffset = 0;
private int xOffset = 0;
public CustomAutoCompleteField(BasicFilteredList filteredList) {
super(filteredList);
}
protected void paint(Graphics g) {
super.paint(g);
if (xOffset == 0) {
// initialize text offsets once
xOffset = getEditField().getContentLeft();
yOffset = getEditField().getContentTop();
}
String text = getEditField().getText();
if (text == null || text.length() == 0) {
int oldColor = g.getColor();
g.setColor(Color.GRAY);
g.drawText("enter text", xOffset, yOffset);
g.setColor(oldColor);
}
}
public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
ListField _list = getListField();
if (_list.getSelectedIndex() > -1) {
final String selectedText = getEditField().getText();
if(selectedText!=null){
final BasicFilteredListResult result = (BasicFilteredListResult) selection;
Application.getApplication().invokeLater(new Runnable() {
public void run() {
selectedText.setText(result._object.toString());
}
});
// selectedText.setText(result._object.toString());
}
}
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(250, 250);
}
}
myscreen.js
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday(TAS)","Tuesday-(PAQ)","Wednesday-(MAN)",
"Thursday","Friday","Saturday","Sunday(I_)"};
filterList.addDataSet(1,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
HorizontalFieldManager hr =new HorizontalFieldManager();
LabelField userName= new LabelField("hjsdhas");
hr.add(userName);
CustomAutoCompleteField autoCompleteField = new CustomAutoCompleteField(filterList);
hr.add(autoCompleteField);
add(hr);
}
}
CustomAutoCompleteField.java is my custom java class and I call the class from myscreen.java class

How can i create a popup for custom Choicefield and call it?

I have created my own simple ChoiceField by extending ObjectChoiceField.. It is like..
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Now i want to display the my choices when i select MyChoiceField in custom menu(not the default popup that blackberry provides when user clicks on a ChoiceField)..
How can i achieve it? Sample codes will be appreciable..
Thanks
Try Overriding the method fieldChangeNotify(int context) of ObjectChoiceField like this:
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
protected void fieldChangeNotify(int context) {
int index = getSelectedIndex();
try{
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(new LabelField("[Pop Up Content]\nChoice Field Changed #\n"+choices[index]+" selected."));
PopupScreen popUpScreen = new PopupScreen(vfm){
public boolean onClose()
{
this.close();
return true;
}
};
UiApplication.getUiApplication().pushScreen(popUpScreen);
} catch(Exception exc) {
System.out.println("Exception in choiceField fieldChangeNotify");
}
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Use MyChoiceField now somewhat like this:
String[] choices; // Keep This Global
VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
choices = new String[2];
choices[0] = new String("Choice1");
choices[1] = new String("Choice2");
vfm.add(new MyChoiceField("Choise", choices));
Let me know whether this solution works.

Blackberry: custom ButtonField with disappearing focus highlight

I'm trying to create a custom ButtonField, whose focus (the blue highlight color) would disappear after few seconds of inactivity - like in original music player on BB phones with touchscreen.
I've almost succeeded in that with the following example:
Here are the east.png and west.png (courtesy of openclipart.org):
Here is my test code MyFocus.java:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyFocus extends UiApplication {
public static void main(String[] args) {
MyFocus app = new MyFocus();
app.enterEventDispatcher();
}
public MyFocus() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
public MyScreen() {
setTitle("2nd trackpad click not working");
getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(Color.WHITE, Color.GRAY, Color.DARKGRAY, Color.GRAY));
add(new MyButton(MyButton.EAST));
add(new MyButton(MyButton.WEST));
add(NF);
}
class MyButton extends ButtonField {
public final static int EAST = 0;
public final static int WEST = 1;
public final static int WIDTH = 100;
public final static int HEIGHT = 100;
private final XYEdges EDGES = new XYEdges(0, 0, 0, 0);
private final Application _app = UiApplication.getUiApplication();
private final static long FOCUS_DURATION = 3000L;
private int _focusTimer = -1;
private final int _direction;
public MyButton(int direction) {
setMargin(EDGES);
setPadding(EDGES);
setImageSize(WIDTH, HEIGHT);
setBorder(VISUAL_STATE_NORMAL,
BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(EDGES));
setBackground(VISUAL_STATE_NORMAL, BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
setBackground(VISUAL_STATE_FOCUS, BackgroundFactory.createSolidTransparentBackground(Color.BLUE, 100));
setBackground(VISUAL_STATE_ACTIVE, BackgroundFactory.createSolidTransparentBackground(Color.RED, 200));
_direction = direction;
switch(_direction) {
case EAST:
setImage(ImageFactory.createImage("east.png"));
break;
case WEST:
setImage(ImageFactory.createImage("west.png"));
break;
}
}
// display red background on long touch and hold
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.CLICK) {
applyThemeOnStateChange();
return true;
}
return super.touchEvent(event);
}
protected void onUnfocus() {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
super.onUnfocus();
}
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyButton.super.onUnfocus();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
public int getPreferredHeight(){
return HEIGHT;
}
public int getPreferredWidth(){
return WIDTH;
}
protected void layout(int width, int height) {
setExtent(WIDTH, HEIGHT);
}
}
}
My problem is visible, when I first select a button and wait few seconds for its focus to disappear. Then I click on the track pad and while the button is pushed (verified that), you don't see anything at the screen - it doesn't turn blue or red.
I've tried all combinations of navigationClick() and trackwheelUnclick() but can not fix that.
Any help please?
Alex
UPDATE 1:
I've tried the following, but it doesn't work well (focus disappears forever, probably because button thinks it is in the needed visual state already):
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyButton.this.setBorder(BorderFactory.createSimpleBorder(EDGES));
MyButton.this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
invalidate();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
UPDATE 2:
A try with a NullField, still not working properly:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyFocus extends UiApplication {
public static void main(String[] args) {
MyFocus app = new MyFocus();
app.enterEventDispatcher();
}
public MyFocus() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static NullField NF = new NullField();
HorizontalFieldManager hfm = new HorizontalFieldManager() {
int lastFocused = -1;
protected int firstFocus(int direction) {
lastFocused = super.firstFocus(direction);
System.out.println("XXX firstFocus: " + lastFocused);
return lastFocused;
}
protected int nextFocus(final int direction, final int axis) {
if (getFieldWithFocus() == NF) {
return lastFocused;
}
lastFocused = super.nextFocus(direction, axis);
System.out.println("XXX nextFocus: " + lastFocused);
return lastFocused;
}
};
public MyScreen() {
setTitle("2nd trackpad click not working");
getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(Color.WHITE, Color.GRAY, Color.DARKGRAY, Color.GRAY));
hfm.add(new MyButton(MyButton.WEST));
hfm.add(new MyButton(MyButton.EAST));
hfm.add(new MyButton(MyButton.EAST));
hfm.add(NF);
add(hfm);
}
class MyButton extends ButtonField {
public final static int EAST = 0;
public final static int WEST = 1;
public final static int WIDTH = 100;
public final static int HEIGHT = 100;
private final XYEdges EDGES = new XYEdges(0, 0, 0, 0);
private final Application _app = UiApplication.getUiApplication();
private final static long FOCUS_DURATION = 3000L;
private int _focusTimer = -1;
private final int _direction;
public MyButton(int direction) {
setMargin(EDGES);
setPadding(EDGES);
setImageSize(WIDTH, HEIGHT);
setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(EDGES));
setBackground(VISUAL_STATE_NORMAL, BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
setBackground(VISUAL_STATE_FOCUS, BackgroundFactory.createSolidTransparentBackground(Color.BLUE, 100));
setBackground(VISUAL_STATE_ACTIVE, BackgroundFactory.createSolidTransparentBackground(Color.RED, 200));
_direction = direction;
switch(_direction) {
case EAST:
setImage(ImageFactory.createImage("east.png"));
break;
case WEST:
setImage(ImageFactory.createImage("west.png"));
break;
}
}
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.CLICK) {
applyThemeOnStateChange();
return true;
}
return super.touchEvent(event);
}
protected void onUnfocus() {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
super.onUnfocus();
}
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyScreen.NF.setFocus();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
public int getPreferredHeight(){
return HEIGHT;
}
public int getPreferredWidth(){
return WIDTH;
}
protected void layout(int width, int height) {
setExtent(WIDTH, HEIGHT);
}
}
}
I would probably ovverride the drawFocus() method to check a flag _showFocus before calling super.drawFocus(), then in onFocus(), set the flag to true and schedule the Runnable to change the _showFocus flag to false and invalidate (also setting it to false in onUnfocus()). Don't have to worry about canceling timers or anything this way either, as it is just going to change a flag and invalidate, keeping it in the appropriate state.

Splashscreen not working

The below splashscreen is not displaying correctly. It works fine when I push just the splashscreen but not when I push the splashscreen and then another screen. Welcome screen is the splashscreen.
So this works -
pushScreen(new WelcomeScreen());
But not this -
pushScreen(new WelcomeScreen());
pushScreen(new MenuScreen());
In the second scenario the menuscreen is displayed straight away but not the splashscreen.
Here is the splash screen code - two classes.
package screens;
import com.src.driver.Driver;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.component.LabelField;
public class WelcomeScreen extends SplashScreen {
public WelcomeScreen() {
super(Bitmap.getBitmapResource("icon.png"), 5);
//normal mainscreen items:
//setTitle("SplashScreen Test");
//add(new LabelField("HelloWorld!"));
}
}
package screens;
import java.util.Timer;
import java.util.TimerTask;
import com.src.driver.Driver;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.MainScreen;
public class SplashScreen extends MainScreen {
Bitmap popup;
SplashScreen screen = this;
private Timer splashTimer = new Timer();
private TimerTask splashTask;
int count = 0;
int screenWidth = Display.getWidth();
int screenHeight = Display.getHeight();
int yCoord;
int xCoord;
boolean showSplash = true;
boolean splashDisplayed = false;
public SplashScreen(Bitmap popup, final int seconds) {
this.popup = popup;
xCoord = (screenWidth - popup.getWidth()) / 2;
yCoord = (screenHeight - popup.getHeight()) / 2;
splashTask = new TimerTask() {
public void run() {
if (showSplash && !splashDisplayed) {
count++;
if (count == seconds * 10) {
showSplash = false;
splashDisplayed = true;
splashTimer.cancel();
invalidate();
}
}
}
};
splashTimer.scheduleAtFixedRate(splashTask, 100, 100);
}
protected void paint(Graphics graphics) {
super.paint(graphics);
if (showSplash && !splashDisplayed) {
graphics.drawBitmap(xCoord, yCoord, popup.getWidth(), popup.getHeight(), popup, 0, 0);
//draw border, delete if not needed:
//graphics.setColor(0xcccccc);
//graphics.drawRect(xCoord, yCoord, popup.getWidth(), popup.getHeight());
}
}
protected void onUiEngineAttached(boolean attached) {
showSplash = true;
invalidate();
super.onUiEngineAttached(attached);
}
//allow user to dismiss splash screen:
protected boolean navigationMovement(int dx, int dy, int status, int time) {
return DismissSplash();
}
protected boolean navigationClick(int status, int time) {
return DismissSplash();
}
protected boolean keyChar(char c, int status, int time) {
return DismissSplash();
}
private boolean DismissSplash() {
if (showSplash) {
showSplash = false;
splashDisplayed = true;
invalidate();
return true;
}else{
return false;
}
}
}
Thank you
It looks like the problem is that MenuScreen is pushed immediately after WelcomeScreen. You will need to put in some kind of mechanism that waits for WelcomeScreen to "finish" before pushing the MenuScreen.
Maybe create a Listener object that is passed into WelcomeScreen. Then when WelcomeScreen "finishes" it would call the Listener (e.g. myListener.splashScreenFinished()). Then the implementation of the Listener would create and push the MenuScreen.
This might look something like this:
interface MyListener {
public void splashScreenFinished();
}
class MyApp implements MyListener {
...
public void splashScreenFinished() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
pushScreen(new MenuScreen());
}
});
}
public void startupApp() {
pushScreen(new WelcomeScreen(this));
}
...
}
class WelcomeScreen {
private MyListener l;
public WelcomeScreen(MyListener listener) {
l = listener;
...
}
protected onSplashTimerDone() {
if (l != null)
l.splashScreenFinished();
}
}

updated on image

i am developing an application that use some images. We require some text print on image but that is not a fixed mean when change the variable value automatically change the text. Simply we say that we print a variable on image?
try this
public class Test extends UiApplication {
public static void main(String[] arg) {
Test app = new Test();
app.enterEventDispatcher();
}
public Test() {
MyScreen screen = new MyScreen();
pushScreen(screen);
}
}
class MyScreen extends MainScreen {
LabelField label;
public MyScreen() {
label = new LabelField() {
protected void paint(Graphics g) {
Bitmap bitmap = Bitmap.getBitmapResource("bgimage.jpg");
g.drawBitmap(0, 0, getWidth(), getHeight(), bitmap, 0, 0);
super.paint(g);
}
};
ButtonField button = new ButtonField("update") {
protected void fieldChangeNotify(int context) {
update();
super.fieldChangeNotify(context);
}
};
add(label);
add(button);
}
public void setMessage(String message) {
synchronized (UiApplication.getEventLock()) {
label.setText(message);
}
}
private void update() {
LocationHandler handler = new LocationHandler(this);
handler.start();
}
}
class LocationHandler extends Thread {
private MyScreen screen;
public LocationHandler(MyScreen screen) {
this.screen = screen;
}
public void run() {
Criteria criteria = new Criteria();
criteria.setVerticalAccuracy(50);
criteria.setHorizontalAccuracy(50);
criteria.setCostAllowed(true);
criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
try {
LocationProvider provider = LocationProvider.getInstance(criteria);
Location location = provider.getLocation(-1);
String speed = location.getSpeed() + "m/s";
screen.setMessage(speed);
} catch (LocationException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Resources