I'm trying to implement simple user interaction with field2.BrowserField: when button is clicked BrowserField loads
another page, but I'm getting IllegalStateException.
here's my code:
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1");
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test1();
}
});
add(btn1);
ButtonField btn2 = new ButtonField("test2");
btn2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test2();
}
});
add(btn2);
browser = new BrowserField();
add(browser);
browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
}
private void test1() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
});
}
private void test2() {
synchronized (Application.getEventLock())
{
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
}
}
Try this one:
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1");
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//test1();
test1("http://www.blackberry.com/developers");
}
});
add(btn1);
ButtonField btn2 = new ButtonField("test2");
btn2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//test2();
test1("http://www.blackberry.com/developers");
}
});
add(btn2);
browser = new BrowserField();
add(browser);
//browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
test1("http://stackoverflow.com/");
}
private void test1(final String url) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
browser.requestContent(url + ";deviceside=true");
}
});
}
// private void test2() {
// synchronized (Application.getEventLock())
// {
// browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
// }
// }
}
Wow, adding browser.setFocus(); do the trick
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1", ButtonField.CONSUME_CLICK);
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test1();
}
});
add(btn1);
browser = new BrowserField();
add(browser);
browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
}
private void test1() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
browser.setFocus();
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
});
}
}
Related
I am developing a blackberry app. I want to open an screen from the home screen when user will press one button and hold it for 2 seconds.
Any way?
Thanks.
Here is the code for your question. I have make use of this BlackBerry LongClickListener implementation link which contains good explanation too.
public class HoldButtonScreen extends MainScreen implements FieldChangeListener {
ButtonField _btnHold;
Timer _timer;
public HoldButtonScreen() {
_btnHold = new ButtonField("Hold 2 sec to get popup")
{
protected boolean navigationClick(int status, int time) {
final Field _btnHold= this;
_timer = new Timer();
System.out.println("hi there");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try{
_timer.schedule(new TimerTask() {
public void run() {
fieldChanged(_btnHold, 0);
}}, 2000);
}catch(Exception e){
e.printStackTrace();
}
}
});
return true;
}
protected boolean navigationUnclick(int status, int time) {
System.out.println("hi unclick");
add(new LabelField("You have't hold button for 2 second."));
_timer.cancel();
return true;
}
};
_btnHold.setChangeListener(this);
add(_btnHold);
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
PopupScreen _popUpScreen = new PopupScreen(new VerticalFieldManager()){
public boolean onClose() {
close();
return true;
}
};
_popUpScreen.add(new LabelField("Hello , i am pop up after 2 second."));
UiApplication.getUiApplication().pushScreen(_popUpScreen);
}
});
}
}
Try this this will run successfully.
Just change the Screen in PushScreen.
private Thread splashTread;
protected int _splashTime = 200;
boolean countinue = true;
splashTread = new Thread() {
public void run() {
while (countinue == true) {
try {
synchronized (this) {
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
synchronized (UiApplication.getUiApplication().getAppEventLock()) {
UiApplication.getUiApplication().pushScreen(new Login());
SplashScreen.this.close();
}
countinue = false;
}
}
}
};
splashTread.start();
Now i have a MyScreen which extends from MainScreen containing a button. When the button clicked, it will show the PopupScreen containing list of RadioButtonFields, OK and Cancel buttons. My question is how to get RadioButtonField value from PopupScreen back to MyScreen.
Please help me in this case.
Thanks
Below is my code
MyScreen.java
public class MyScreen extends MainScreen{
private int currentValue = 0;
public MyScreen() {
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen());
}
});
add(lblField);
add(btnPopup);
}
}
MyPopupScreen
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
private int currentValue = -1;
public MyPopupScreen() {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE
);
add(new LabelField("Select Chart Type"));
RadioButtonGroup radioGroup = new RadioButtonGroup();
for (int i = 0; i < 10; i++) {
RadioButtonField radio = new RadioButtonField("Option no. " + i, radioGroup, false);
radio.setChangeListener(this);
add(radio);
}
ButtonField cancelBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(cancelBtn);
cancelBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen();
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//How to get radio value or index value to pass back to MyScreen
}
});
}
public void fieldChanged(Field field, int context) {
if (field instanceof RadioButtonField) {
RadioButtonField radio = (RadioButtonField) field;
currentValue = radio.getIndex();
}
}
}
As Vijay, suggested this is one way that we could achieve this using inner class. The other approach could be, using design observer design patterns.
Try understanding the below code, and use what ever is efficient for you.
MyScreen.java
public class MyScreen extends MainScreen implements MyListener {
MyScreen screen = null;
public MyScreen() {
screen = this;
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen(screen));
}
});
add(lblField);
add(btnPopup);
}
public void valueChanged(int selectedIdx) {
Dialog.alert(selectedIdx );
}
}
MyListener.java
public interface MyListener {
public void valueChanged(int selectedIdx);
}
MyPopupScreen.java
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
MyScreen deligate = null;
public MyPopupScreen(final MyScreen deligate) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE);
this.deligate = deligate;
add(new LabelField("Select Type"));
ButtonField closeBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(closeBtn);
closeBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
deligate.valueChanged(radioGroup.getSelectedIndex());
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
});
}
}
public class MyScreen extends MainScreen{
private int currentValue = 0;
String result; // Declare the result
public MyScreen() {
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen());
}
});
ButtonField btnGetResult = new ButtonField("result"); //Add new Button
btnGetResult.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.inform(result);
}
});
add(lblField);
add(btnPopup);
add(btnGetResult);// Add button to get the selected index
}
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
private int currentValue = -1;
RadioButtonField radio;
public MyPopupScreen() {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE
);
add(new LabelField("Select Chart Type"));
final RadioButtonGroup radioGroup = new RadioButtonGroup();
for (int i = 0; i < 10; i++) {
radio = new RadioButtonField("Option no. " + i, radioGroup, false);
radio.setChangeListener(this);
add(radio);
}
ButtonField cancelBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(cancelBtn);
cancelBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen(MyPopupScreen.this);
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
result = radioGroup.getSelectedIndex()+"";
UiApplication.getUiApplication().popScreen(MyPopupScreen.this);
}
});
}
public void fieldChanged(Field field, int context) {
if (field instanceof RadioButtonField) {
RadioButtonField radio = (RadioButtonField) field;
currentValue = radio.getIndex();
}
}
}
}
I'm giving localization support in my app in blackberry.Now the problem is i want to use system font in my application but the hieght should be constant.Now i want to use Status.show method to show some string on the screen.The string is coming from the resource bundle.
So kindly suggest how to set the font of that.
Thanx in advance
Regards
Deepak Goel
public class DialogBox extends PopupScreen implements FieldChangeListener {
private ButtonField okButton = new ButtonField("OK", ButtonField.CONSUME_CLICK);
private static DialogBox dialog = null;
private DialogBox( String text) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
add(new LabelField(text, Field.FIELD_HCENTER));
okButton.setChangeListener(this);
add(okButton);
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.BLACK);
super.paint(graphics);
}
public static void show(String text) {
dialog = new BMDialogBox(text);
Thread threadToRun = new Thread() {
public void run() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(dialog);
}
});
try {
sleep(2000);
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException("Exception detected while waiting: " + t.toString());
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
UiApplication.getUiApplication().popScreen(dialog);
} catch (Exception e) { }
});
}
};
threadToRun.start();
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
UiApplication.getUiApplication().popScreen(dialog);
} catch (Exception e) { }
}
});
}
}
you can set a fields font with original style of it and given height(15) just like that:
setFont(getFont().derive(getFont().getStyle(),15));
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();
}
}
}
i have invoked blackberry calender from my application
can anyone tell me how to fetch :
date
duration
notes
from the selected date
my code :
MenuItem importCalender = new MenuItem("Import from Calender",100,11)
{
public void run()
{
UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run()
{
try
{
EventList list = (EventList)PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE);
Enumeration events = list.items();
BlackBerryEvent e = (BlackBerryEvent) events.nextElement();
Invoke.invokeApplication(Invoke.APP_TYPE_CALENDAR, new CalendarArguments( CalendarArguments.ARG_VIEW_DEFAULT,e) );
}
catch (PIMException e)
{
//e.printStackTrace();
}
}
});
}
};
protected void makeMenu(Menu menu, int instance)
{
menu.add(importCalender);
}
You should register custom menu item for calendar application.
See How To - Add a custom menu item to an existing BlackBerry application
UPDATE
alt text http://img517.imageshack.us/img517/2789/caledar3.jpg
class Scr extends MainScreen {
VerticalFieldManager mManager;
UiApplication mApp;
public Scr() {
mApp = UiApplication.getUiApplication();
mManager = (VerticalFieldManager) this.getMainManager();
MyMenuItem myMenuitem = new MyMenuItem(0);
ApplicationMenuItemRepository.getInstance().addMenuItem(
ApplicationMenuItemRepository.MENUITEM_CALENDAR, myMenuitem);
}
class MyMenuItem extends ApplicationMenuItem {
MyMenuItem(int order) {
super(order);
}
public Object run(Object context) {
if (context instanceof Event) {
Event event = (Event) context;
final String text = "start: "
+ (new Date(event.getDate(Event.START, 0))).toString()
+ "\nend: "
+ (new Date(event.getDate(Event.END, 0))).toString()
+ "\nnote: " + event.getString(Event.NOTE, 0);
String message = "Import event\n" + text;
if (Dialog.YES == Dialog.ask(Dialog.D_YES_NO, message)) {
mApp.invokeLater(new Runnable() {
public void run() {
mApp.requestForeground();
mManager.add(new LabelField(text));
}
});
}
}
return context;
}
public String toString() {
return "Import Event";
}
}
MenuItem importCalender = new MenuItem("Import from Calender", 100, 11) {
public void run() {
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
Invoke.invokeApplication(Invoke.APP_TYPE_CALENDAR,
new CalendarArguments(
CalendarArguments.ARG_VIEW_DEFAULT));
}
});
}
};
protected void makeMenu(Menu menu, int instance) {
menu.add(importCalender);
}
}