Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Regular Contributor
shafich
Posts: 65
Registered: 01-12-2012
My Carrier: Developer

Adding/Removing MenuItems dynamically (in the same page/screen)

Hello,
My application has one class myscreen that extends MainScreen. So as user starts using the application I just update myscreen ( so I don't use popscreen or pushscreen nor do I use a new instance of any screen)
So my Menu is the same over all the application.

My question is can I remove/add MenuItems ? I know how add/remove MenuItem if I am using different Screens, however my application has only one screen.
How can I get an instance of the "Menu" so that I can use Menu.add(menuItem) Menu.delete(MenuItemIndex) from my functions (my functions are in synchronized blocks inside a timerTask)

I hope I am clear, so that anyone could help...

Thanks in advance ...

 

Please use plain text.
Super Contributor
shahumang8
Posts: 306
Registered: 08-09-2010

Re: Adding/Removing MenuItems dynamically (in the same page/screen)

if possible then send the code.

 

Thanks.

Please use plain text.
Regular Contributor
shafich
Posts: 65
Registered: 01-12-2012
My Carrier: Developer

Re: Adding/Removing MenuItems dynamically (in the same page/screen)

Solved...
I over ridden makeMenu and added to it some dynamic static variables that I change as my application change (with needs) ... 
-----------working code-----------------------

static boolean d_FromMainpage=true;
protected void makeMenu(Menu menu , int instance) {


//d_FromMainpage changes as my application goes on ... (so as I please I toggle between adding each of the items)
// note that -makeMenu()- is called each while or has a auto-change-listen ... I am really not sure about any of this , //what I am sure about is that this CODE worked for me. Hope I helped.


menu.deleteAll();


if(d_FromMainpage)
menu.add(RestartItem);
else menu.add(StartItem);


menu.add(RateUs);
menu.add(MenuItem.separator(122));
menu.add(Help);
menu.add(MenuItem.separator(155));
menu.add(exitItem);


}

Please use plain text.
Developer
alishaik786
Posts: 249
Registered: 08-26-2011
My Carrier: Not Specified

Re: Adding/Removing MenuItems dynamically (in the same page/screen)

[ Edited ]

This is a sample Code gives how to add and remove the menuItem from the Menu;

 

According to this you can you can add or remove your menuItems:

 

The below is sample class: Abc.java

 

public class Abc extends MainScreen implements FieldChangeListener	
{	
	String menuItemName="Add or Remove";	
	ButtonField addMenu,removeMenu;		
	MenuItem menuItem;
	public Abc()
    {      
		createGUI();             
    }

	
	public void createGUI() 
	{
		addMenu=new ButtonField("Add Menu");
		addMenu.setChangeListener(this);		
		add(addMenu);
		
		removeMenu=new ButtonField("Remove Menu");
		removeMenu.setChangeListener(this);		
		add(removeMenu);
		
		menuItem=new MenuItem(menuItemName,100,101) 
		{
			public void run() 
			{
				Dialog.alert("Add or Remove Menu Item");
			}
		};
		addMenuItem(menuItem);		
	}

	public void fieldChanged(Field field, int context) 
	{
		if(field==addMenu)
		{
			if(AddOrRemoveMenuItems.addTheMenu(menuItem))
			{
				Dialog.alert("Aleady Added to MenuItem");
			}
			else
			{
				addMenuItem(menuItem);
				Dialog.alert("Added to MenuItem Successfully");
			}
		}
		else if(field==removeMenu)
		{
			if(AddOrRemoveMenuItems.removeTheMenu(menuItem))
			{
				removeMenuItem(menuItem);
				Dialog.alert("MenuItem Removed Successfully");
			}
			else
			{
				Dialog.alert("Already Removed from MenuItem");
			}
		}
	}
}

 

I took both addTheMenu(menuItem) and removeTheMenu(menuItem) methods in separate class. So I can call anywhere in my application;

 

AddOrRemoveMenuItems.java

 

public class AddOrRemoveMenuItems 
{
	public static boolean addTheMenu(MenuItem menuItem) 
	{	
		boolean isAdding=false;
		Screen screen=Ui.getUiEngine().getActiveScreen();
		Menu menu=screen.getMenu(0);
		for(int i=0;i<menu.getSize();i++)
		{
			if(menu.getItem(i).toString().equalsIgnoreCase(menuItem.toString()))
			{
				isAdding=true;
			}			
		}
		return isAdding;
	}
	
	public static boolean removeTheMenu(MenuItem menuItem) 
	{	
		boolean isRemoving=false;
		Screen screen=Ui.getUiEngine().getActiveScreen();
		Menu menu=screen.getMenu(0);
		for(int i=0;i<menu.getSize();i++)
		{
			if(menu.getItem(i).toString().equalsIgnoreCase(menuItem.toString()))
			{
				isRemoving=true;	
			}		
		}
		return isRemoving;
	}
}

Check this sample code first and then use this according to your requirement;

 =====================================================================================

Feel to click LIKE button if the solution helps you;

 

 

============================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.