I have mainscreen with 5 menus and after a certain action I need to delete 2 of them in a method that peforms the action, can I delete menu items programetically ?
Run this sample code:
public class Abc extends MainScreen
{
ButtonField click;
MenuItem saveMenu;
public Abc()
{
createGUI();
}
private void createGUI()
{
saveMenu=new MenuItem("Save", 100, 101);
addMenuItem(saveMenu);
click=new ButtonField("click");
click.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
removeTheMenu(saveMenu);
}
});
add(click);
}
public void removeTheMenu(MenuItem menuItem)
{
Screen screen=Ui.getUiEngine().getActiveScreen();
Menu menu=screen.getMenu(0);//Gives the Menu list of active screen only;
for(int i=0;i<menu.getSize();i++)
{
if(menu.getItem(i).toString().equalsIgnoreCase(menuItem.toString()))
{
removeMenuItem(menuItem);
Status.show("Removed Successfully", 1000);
}
}
}
}
This code may help you;
Related
I am using finish() to close current activity before quit application in Android.
However, I cannot close screen in blackberry.
public class Main_AllLatestNews extends MainScreen {
public Main_AllLatestNews() {
super(USE_ALL_WIDTH);
}
private boolean Dialog() {
final Bitmap logo = Bitmap.getBitmapResource("icon.png");
d = new Dialog("确定离开?", new String[] { "是", "否" }, new int[] {
Dialog.OK, Dialog.CANCEL }, Dialog.OK,
logo) {
public void setChangeListener(FieldChangeListener listener) {
if (d.getSelectedValue() == Dialog.OK) {
} else {
d.close();
}
};
};
d.show();
return (d.doModal() == Dialog.OK);
}
public boolean onClose(){
if(Dialog()){
System.exit(0);
return true;
}else
return false;
}
}
Here is my Main class
public class Main extends UiApplication {
public static void main(String[] args) {
Main theApp = new Main();
theApp.enterEventDispatcher();
}
public Main() {
pushScreen(new MyScreen());
}
public final class MyScreen extends MainScreen {
private Bitmap logo = Bitmap.getBitmapResource("logo_page.png");
private BitmapField bmfield;
public MyScreen() {
setTitle("Oriental Daily");
bmfield = new BitmapField(logo, Field.FIELD_HCENTER
| BitmapField.FOCUSABLE) {
protected boolean navigationClick(int status, int time) {
Main.this.pushScreen(new Main_AllLatestNews());
Main.this.popScreen(MyScreen.this);
return true;
}
};
}
}
It depends on exactly how you want your close behaviour to work. Also, I can only read English, so I'm not 100% sure what your Dialog says. I'm assuming it's something to do with closing the app (yes or no)?
Anyway, usually, my apps close by overriding the onClose() method in the MainScreen subclass. You don't actually need to listen for the escape key. onClose() will get called normally when the user escapes all the way out of the app, or presses the little button with the blackberry icon, and then selects Close.
public final class MyScreen extends MainScreen {
/** #return true if the user chooses to close the app */
private boolean showDialog() {
Bitmap logo = Bitmap.getBitmapResource("icon.png");
Dialog d = new Dialog("确定离开?",
new String[] { "是", "否" },
new int[] { Dialog.OK, Dialog.CANCEL },
Dialog.OK,
logo);
return (d.doModal() == Dialog.OK);
}
/** Shutdown the app? */
public boolean onClose() {
if (showDialog()) {
System.exit(0);
return true;
} else {
// the user does not want to exit yet
return false;
}
}
}
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();
}
I have a main class and a screen class. I have a button that launches a datepicker. When the datepicker is closed I need the label of the button to be refreshed with the selected value. How should I do that?
I tried with invalidate, creating a CustomButtonField that implements different onFocus, onUnFocus, and some other stuff but I guess I implemented them in a wrong way...
My two clases are these ones...
Main...
public class TestClass extends UiApplication
{
public static Calendar alarm1time = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
public static void main(String[] args)
{
TestClass testClass = new TestClass ();
testClass.enterEventDispatcher();
}
public TestClass()
{
pushScreen(new TestClassScreen());
}
}
Screen...
public final class TestClassScreen extends MainScreen
{
public TestClassScreen()
{
ButtonField alarm1 = new ButtonField("Alarm : " + TestClass.alarm1time.get(Calendar.HOUR_OF_DAY) + ":" + TestClass.alarm1time.get(Calendar.MINUTE) ,ButtonField.FOCUSABLE)
{
public boolean navigationClick (int status , int time)
{
datePicker(1);
return true;
}
};
setTitle("Test Alarm");
add(new RichTextField(" "));
add(alarm1 );
}
public void datePicker(final int alarmNumber)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{
Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;
//Here I need the label to be refreshed, after the datePicker is Ok
}
}
});
}
}
I found one way in the Blackberry Development Forum...
public boolean navigationClick (int status , int time)
{
datePicker(1, this);
return true;
}
public void datePicker(final int alarmNumber, final ButtonField buttonToUpdate)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{
Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;
buttonToUpdate.setLabel(....)
}
}
});
}
A more usual way would be to have change listener listen for Button Clicks and do similar processing in there.
I think, this helps you:
public class Def extends MainScreen
{
ButtonField show;
LabelField label;
String str="";
ObjectChoiceField choiceField;
public Def()
{
createGUI();
}
private void createGUI()
{
String st_ar[]={"Date Picker"};
choiceField=new ObjectChoiceField("Select Date: ", st_ar)
{
protected boolean navigationClick(int status, int time)
{
DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), "yyyy-MM-dd", null);
datePicker.doModal();
Calendar calendar=datePicker.getDateTime();
str=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.YEAR);
return true;
}
};
add(choiceField);
label=new LabelField("",Field.NON_FOCUSABLE);
add(label);
show=new ButtonField("Show");
show.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
label.setText("Time is: "+str);
}
});
add(show);
}
}
I want to add mymenuitem on smssending menulist,
How could I do that?
You just override the makeMenu method and add whatever you want.
protected void makeMenu(Menu menu, int instance) {
menu.add(getExitItem(0,0));
}
public MenuItem getExitItem(int ordinal, int priority) {
return new MenuItem("Close", ordinal, priority) {
public void run() {
System.exit(0);
}
};
}
I am not able select the custombuttonfield
Here is my code:-
public MenuScreen()
{
super(Screen.DEFAULT_CLOSE);
verticalOffset=Display.getWidth();
menuBackgroundImage=Bitmap.getBitmapResource("com/greetings/Images/MenuBackground.jpg");
categories=new CustomSmallButton(Bitmap.getBitmapResource("com/greetings/ButtonImage/categoryon.png"),Bitmap.getBitmapResource("com/greetings/ButtonImage/categoryoff.png"),0);
categories.setChangeListener(this);
help=new CustomSmallButton(Bitmap.getBitmapResource("com/greetings/ButtonImage/helpon.png"),Bitmap.getBitmapResource("com/greetings/ButtonImage/helpoff.png"),0);
help.setChangeListener(this);
developer=new CustomSmallButton(Bitmap.getBitmapResource("com/greetings/ButtonImage/aboutuson.png"),Bitmap.getBitmapResource("com/greetings/ButtonImage/aboutuspoff.png"),0);
developer.setChangeListener(this);
VerticalFieldManager manager = new VerticalFieldManager()
{
protected void sublayout(int width, int height)
{
width=fwidth;
height=fHeight;
super.sublayout(width,height);
setPositionChild(categories, width-240, height-350);
setPositionChild(help, width-240, height-290);
setPositionChild(developer, width-240, height-230);
setExtent(width, height);
}
};
manager.setBackground(BackgroundFactory.createBitmapBackground(menuBackgroundImage));
manager.add(categories);
manager.add(help);
manager.add(developer);
add(manager);
}
public void fieldChanged(Field field, int context) {
if(field==categories)
{
templateCategories=new TemplateCategories(2);
UiApplication.getUiApplication().pushScreen(templateCategories);
}
if(field== help)
{
helpScreen=new HelpScreen();
UiApplication.getUiApplication().pushScreen(helpScreen);
}
if(field == developer)
{
developerScreen=new DeveloperScreen();
UiApplication.getUiApplication().pushScreen(developerScreen);
}
}
when i am clicking help or developer button its not selecting.
override the
protected void drawFocus(Graphics graphics,boolean on) {
//code for custom focus like draw onFocusbitmap
super.drawFocus(graphics, on);
}
in your CustomSmallButton class