JibW
JibW

Reputation: 4562

Android, Dynamically create menu items

Hi in my application I need to add Some menu items. The problem is, one menu item should only display when the focus is on one of the listview items.

Simply one of the menu items should only display if the focus is on at one the of the listview items. When the focus is at anywhere else then should display only the other menu items except that. Other menu items will display always.

I can track the focus. I tried to run following code only when focus is on listview.

menu.add(0, Menu.FIRST, 0, "Add to favourite List"); [Problem: if I press menu button more than one time while focus there it creates new menu item each time when I press the menu button.]

I am inflating from xml menu and create others.

Can anyone guide me how to do this... Thanks

Upvotes: 1

Views: 2421

Answers (2)

NujnaH
NujnaH

Reputation: 173

You could have a variable to keep track of whether that menu item was added or not.

Upvotes: 1

Khushbu Shah
Khushbu Shah

Reputation: 1683

use onPrepareOptionsMenu method and clear all menu first using

menu.clear(); 

then add menu.

Upvotes: 2

Related Questions