How to instantiate menu object? - android

I am trying to hide and show a menu item in my action bar.
when i click on the menu item, i run a function called showAddFrag(). Inside that function I am trying to hide the menu item. so my code is something like this
public void showAddFrag(){
Menu menu = new Menu (); // giving me this error: "Cannot instantiate the type Menu"
menu.findItem(R.id.add_item).setVisible(false); //hiding the add_item menu item
....
}
I know I am doing it wrong, but can someone point me in the right direction. Is there a function that I can reference to the context menu object?
Thanks in advance

You can use this callback: onPrepareOptionsMenu() and this method: menu.findItem(..) to save your button as an instance variable
Then set an onClickListener for your button that will disable it once clicked. And enable it back when you have to.
I hope this is what you're trying to do

Related

get Menu outside onOptionsItemSelected

Im looking to set an actionbar menu item visible from a helper class. Is it possible to access the actionbar Menu outside of onOptionsItemSelected through a reference to an activity? code below.
public boolean getMenuFromActivity(BaseActivity activity){
// something like Menu menu = activity.getActionBar().getMenu()?
// then get menu item by id and set visibility..
//return true if found
return false
}
Simple answer: Call findItem() on the Menu after you have inflated your menu resource in onCreateOptionsMenu(), and hold onto that MenuItem in a data member of your activity, so you can use it later.
Slightly less-simple answer: Hold onto the Menu from onCreateOptionsMenu() in a data member of your activity, and use it later to find your item.

How to know if the Done button was clicked Action Mode

I'm implementing a actionmode is'm doubts as whether the done button was clicked,
Thanks
You need to use:onActionItemClicked(ActionMode, MenuItem)
Docs
I'm using this approach - set a boolean flag when any action item is clicked (in onActionItemClicked). Then in callback's onDestroyActionMode I check this flag and is it is not set then the done button was tapped. It assumes that you finish action mode in onActionItemClicked.
if the Done button was clicked 。
this method onDestroyActionMode(ActionMode mode) {} will call back .
so you can call your method onSave().
if you can't find this method, you can gooogle find which interface you
need implements.

Android OptionsMenu, add items on runtime

I've got an optionsmenu looking like this right now:
Lets say that if I click item 1, i want two new items added to the menu looking like this:
I'm having problems doing this at runtime(while it's open) since onCreateOptionsMenu is only called once and onPrepareOptionsMenu seems only to be called when the menubutton of the phone is clicked. I just want it to refresh with these new items added.
Thanks for any help!
When you select an option item it causes the system to close the options menu. This happens after onOptionsItemSelected() runs. I'm guessing what you need to have happen is for that entire process to complete then have the options menu programmatically opened again so onPrepareOptionsMenu() is called.
Try this:
In onOptionsItemSelected(), when the user selects your "add two more options" option, set a flag in your activity but don't do anything else.
Override onOptionsMenuClosed(). Check if the flag is set. If it is, then post to be executed by the UI thread on its next pass. It should do nothing but open the options menu again, e.g.
runOnUiThread(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
});
Override onPrepareOptionsMenu(). Check if the flag is set. If it is, then add your two additional menu items and un-set the flag. You'll need to do some additional work to prevent the "add more items" menu item from continuing to add new items every time its pressed, unless that's the behavior you're looking for.

Show Menu Option Automatically At First Activity Of Application

I develop a application
and in which i have a Menu option which i invoke from onCreateOptionMenu()
But this is called only when any user press the menu button
so now i want that my application start and first Activity is Welcome.java
then in onCreate(Bundle b)
can i write sone line from which the menu is invoked automatically without pressing Menu button
i used openOptionMenu() but it not works.
2) can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible
So give me some suggestion on this
Thanks
You can request the menu be opened with an Activity method
openOptionsMenu();
If you want to show a menu immediately, you'll have to wait for the window focus to change, rather than using onResume:
#Override
public void onWindowFocusChanged(boolean hasFocusFlag) {
super.onWindowFocusChanged(hasFocusFlag);
if (hasFocusFlag) {
openOptionsMenu();
}
}
See openOptionsMenu()
Hi I will like to provide answer for your question
(" can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible")
Answer:
Step 1-Create a button/ image button in your layout
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/a"
android:onClick="expand"
android:src="#drawable/button" />
Here I have set onClick method as "expand"
Step 2-Now in your MainActivity.java class define your "expand" method which will be call once user click on your button
public void expand(View v)
{
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.GONE);
openOptionsMenu();
}
In this code I have set visibility as "gone", as I want the button to disappear once menu is shown
Step 3-*(In case you are setting visibility for the button)* You can also write code to set visibility as "visible" once menu is closed using below method
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.VISIBLE);
}
Hope this will help you.....

how to rename existing menu item?

Do you know how to rename existing menu ?
I can rename when press menu item. But I don't know how to access to menu item when press the button.
Please advice.
It would be good if you can clarify the question a little, but each time the user presses the Menu on their Android device while inside one of your activities, the onPrepareOptionsMenu method is called. The first time the menu is shown (i.e. only once), the onCreateOptionsMenu method is called.
Basically, the onPrepareOptionsMenu method is where you should make any changes such as enabling/disabling certain menu items, or changing the menu item text depending on the circumstances.
As an example:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Check current message count
boolean haveMessages = mMessageCount != 0;
// Set 'delete' menu item state depending on count
MenuItem deleteItem = menu.findItem(R.id.menu_delete);
deleteItem.setTitle(haveMessages ? R.string.delete : R.string.no_messages);
deleteItem.setEnabled(haveMessages);
return super.onPrepareOptionsMenu(menu);
}
Use MenuItem.setTitle(). If this isn't what you needed, you have to be more specific.
The onPrepareOptionsMenu is the proper place to make changes to menuitems.

Categories

Resources