create custom menu on blackberry - blackberry

I create a custom menu in new class extends popupScreen. I went when the user clock on the menu button the menu display. I use this code
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
return true;
}
else
return super.keyDown(keycode, time);
}
but I obtain error on this line UiApplication.getUiApplication().pushScreen(popup); causes by pushScreen. How can I change this code or there is another method to display this menu.

try this
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
synchronized (UiApplication.getEventLock())
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
}
return true;
}
else
return super.keyDown(keycode, time);
}

In Blackberry, creating, displaying and hiding of Menu is handled by the system; you will be provided with callbacks at appropriate time. The callbacks you will need to code are:
public boolean onMenu( int instance )
{
return true; // Menu is created
}
public void makeMenu( Menu menu, int instance )
{
MenuItem _desired_menu_item = new MenuItem()
{
public void run()
{
}
}
menu.add( _desired_menu_item );
super.makeMenu( menu, instance)
}
That's it! It would work. Please check Blackberry "UI and Navigation guide" for more details

Related

Invoking navigationClick from navigationClick 'father' in Blackberry

Edited
...
I´m trying to deploy a new component in my BlackBerry app. I have a new class that extends of HorizontalFieldManager, where I instanced a new ListField. Inside of this, I overwrote the navigationClick method and I open a popup. The name of this component is CustomComponentHorizontal. I use this component in other component customized, its name is CustomComponentVertical. This is the code:
In Main.java:
...
CustomComponentVertical ccv=new CustomComponentVertical();
...
add(ccv);
In CustomComponentVertical
...
CustomComponentHorizontal cch=new CustomComponentHorizontal{
...
protected boolean navigationClick(int status, int time) {
//Should I do something here??
return super.navigationClick(status, time);
}
};
...
add(cch);
In CustomComponentHorizontal:
public class CustomComponentHorizontal extends HorizontalFieldManager {
ListField choiceField=null;
PopupScreen popup=null;
public CustomComponentHorizontal (){
choiceField = new ListField(){
public boolean navigationClick(int status, int time) {
Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
if (focus instanceof ListField) {
popup = new ChoicePopupScreen(10, 50, choices);
popup.setChoice(choices);
UiApplication.getUiApplication().pushScreen(popup);
setPopup(popup);
return super.navigationClick(status, time);
}
};
}
}
}
My purpose is that when I click in my component, it is launched the navigationClick of listfield. When I put the focus in my component and click with trackpad, the popup no open. But, If I touch the screen over the component, and then I click with trackpad, popup open.
How could I open the popup from component without to use the touch event?
Thank you very much.
I copy pasted your code. There were some errors, and your callback was missing. So I fixed it up a bit and created a simple callback to test with, and the navigationClick fires. Hopefully this helps, let me know if there is a different issue I didn't understand.
public CustomComponent()
{
elements = new Vector();
elements.addElement("Element 0");
elements.addElement("Element 1");
elements.addElement("Element 2");
elements.addElement("Element 3");
elements.addElement("Element 4");
elements.addElement("Element 5");
choiceField = new ListField(elements.size())
{
public boolean navigationClick(int status, int time)
{
int index = getSelectedIndex();
Dialog.inform((String) elements.elementAt(index));
return true;
}
};
choiceField.setCallback(new ListFieldCallback()
{
public int indexOfList(ListField listField, String prefix, int start)
{
return elements.indexOf(prefix, start);
}
public int getPreferredWidth(ListField listField)
{
return Display.getWidth();
}
public Object get(ListField listField, int index)
{
return elements.elementAt(index);
}
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
String obj = (String) get(listField, index);
graphics.setColor(0x000000);
graphics.drawText(obj, 0, y, 0, width);
}
});
add(choiceField);
}

Back Button handling in blackberry

I want to come to home screen when clicking on the back button from my application in blackberry. I have searched for this in google and stackoverflow, But I didn't get any solution. Anybody help for this.
In My Starting Screen i wrote like this for back button.
protected boolean keyDown(int keycode, int time)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
close();
return true;
}
else
{
return super.keyDown(keycode, time);
}
}
In my HomeScreen i wrote like this.
protected boolean keyDown(int keycode, int time)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
UiApplication.getUiApplication().pushScreen(new StartingScreen());
return true;
}
else
{
return super.keyDown(keycode, time);
}
}
From my application HomeScreen I am able to come to starting screen of the my application. after that when i click on back button from starting screen i need to go to blackberry home screen that means i need to exit the app and come out of that. I wrote close(); to come to blackberry home screen. but its not working. it is again coming to my application homescreen.
Override keyDown method in your subclass of MainScreen.
protected boolean keyDown(int keycode, int time) {
int key = Keypad.key(keycode);
if(key==Characters.ESCAPE){
// do something here
return true;
}
return super.keyDown(keycode, time);
}
To come to home screen
while(!(UiApplication.getUiApplication().getActiveScreen() instanceof HomeScreen)){
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
I got the Solution... i have written lik this.
public boolean onClose()
{
int choose=Dialog.ask(Dialog.D_YES_NO, "Are you sure Want to Exit?");
if(choose==Dialog.YES)
{
System.exit(0);
}
return true;
}
Try this -
public class yourclass extends MainScreen{
public yourclass(){
}
}
public boolean onClose() {
Application.getApplication().invokeLater(new Runnable() {
public void run() {
//close this screen and push your home screen
}
});
return true;
}

Blackberry Bitmap fieldChanged throws IllegalStateException

I have a bitmap field in my blackberry5 app with fieldChanged listener attached to it which works absolutely fine
now my problem is that I also have an associated menu for the same purpose (I can not remove it's the requirement) and on click the menu I get a JVM 104 IllegalStateException
here is my menu class
public class TabMenu extends MenuItem{
MainScreen menuScreen;
Field button;
public TabMenu(String menuLabel,MainScreen menuScreen,Field button)
{
super(menuLabel, 1, 0);
this.menuScreen = menuScreen;
this.button = button;
}//end constructor
public void run()
{
FieldChangeListener listener = (FieldChangeListener)this.menuScreen;
listener.fieldChanged(this.button, this.button.getIndex());
this.button.setFocus();
}
}
and here is menu and fieldchnaged code
protected void makeMenu(Menu menu, int instance) {
menu.add(new RefreshMenu());
menu.addSeparator();
menu.add(new TabMenu("Go >", this, goTab));
menu.addSeparator();
}
public void fieldChanged(Field field, int context) {
if (field == goTab) {
Dialog.alert("goinf")
}
}
Try changing your TabMenu#run() method to the following:
public void run() {
this.button.fieldChangedNotify(this.button.getIndex());
this.button.setFocus();
}

listeners for ObjectChoice field

I am creating a BlackBerry application which contains two ObjectChoiceFields. I want to add listeners for each of them. I searched for that but did not find any of useful code.
It is like a country-state selector on websites. I am looking for simple logic, and I have no need of any database oriented solution. Can anyone describe how to add listeners for those two ObjectChoiceField?
I have have attached my code below, but it is only working for country choice field. Nothing happens when I change state choice.
public class MyApplication extends MainScreen implements FieldChangeListener
{
ObjectChouceField choice_c,choice_s;
public MyApplication ()
{
this.setTitle("hai");
choice_c = new MyChoiceField("Select a Country", countryArray);
choice_c.setChangeListener(this);
this.add(choice_c);
choice_s = new MyChoiceField("Select a State", stateArray);
choice_s.setChangeListener(this);
this.add(choice_s);
..................
..................
}
public void fieldChanged(Field field, int context)
{
if(field == choice_category)
{
Dialog.alert("choice country has been pressed");
}
else if(field == choice_round)
{
Dialog.alert("choice state has been pressed");
}
}
}
Have you tried overriding the navigationClick method of the ObjectChoiceField?
Here is an example:
ObjectChoiceField selectionField = new ObjectChoiceField("Select the Country",countryArray)
{
protected boolean navigationClick(int arg0, int arg1)
{
return super.navigationClick(arg0, arg1);
}
};
cf is the ChoiceField. Use the getSelectedIndex to and trigger events accordingly.
cf.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
switch (cf.getSelectedIndex()) {
case 0:
//code here //
break;
default:
break;
}
}
});

How to set action for button on this custom dialog?

I make a custom Dialog like as :
public class CustomDialog extends Dialog {
public CustomDialog(String s) {
super(s, new String[] {"View","Cancel"}, new int [] {1,2}, 1, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Manager.FOCUSABLE);
}
How can I set action for "View button" and " Cancel button " ?
I searched and not found what I have to do .
Please help me !
Attach a DialogClosedListener to your CustomDialog using Dialog.setDialogClosedListener(). When someone clicks either of the buttons, the DialogClosedListener.dialogClosed() method will be called and the button index will be passed as the choice parameter.
Check out this code.. this might help you..
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.HorizontalFieldManager;
public class CustomAlertDialog extends Dialog {
public CustomAlertDialog() {
super("Your Custom message for Dialoug" , null, null, Dialog.DISCARD, null, Dialog.VERTICAL_SCROLL);
HorizontalFieldManager hfm = new HorizontalFieldManager();
ButtonField view = null;
view = new ButtonField("view") {
protected boolean navigationClick(int status, int time) {
// do what ever you want
return true;
}
protected boolean keyChar(char key, int status, int time) {
// do what ever you want
return true;
}
};
ButtonField cancel = null;
cancel = new ButtonField("Cancel") {
protected boolean navigationClick(int status, int time) {
// do what ever you want
return true;
}
protected boolean keyChar(char key, int status, int time) {
// do what ever you want
return true;
}
};
hfm.add(view);
hfm.add(cancel);
this.add(hfm);
}
}

Resources