I am trying to hide the options menu when the Nav Drawer is opened. How do I do this? The hide() doesn't work and I tried a few variations already..
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_write"
android:title="Write"
android:showAsAction="always"
/>
</menu>
public final class OptionsMenu{
private Menu menu;
public void onCreateOptionsMenu(MenuInflater inflater, Menu menu) {
inflater.inflate(R.menu.sample, menu);
this.menu = menu;
}
public boolean onMenuItemSelected(MenuItem item) {
switch (item.getItemId()) {
default:
return false;
}
}
public void hide(){
menu.findItem(R.id.menu_points).setVisible(false); //Doesn't work
}
}
Try this...
private boolean isDrawerOpened; // Global variable
implement DrawerListener...
drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
#Override
public void onDrawerOpened(View view) {
isDrawerOpened = true;
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View view) {
isDrawerOpened = false;
invalidateOptionsMenu();
}
});
and validate state of DrawerLayout in onCreateOptionsMenu...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
if (!isDrawerOpened) {
getMenuInflater().inflate(R.menu.sample, menu);
}
return true;
}
use closeOptionsMenu() for close option menu
Answer is here Android: How to enable/disable option menu item on button click?
onPrepareOptionsMenu(Menu menu)
Related
I want to open a menu from a OnClickListener
without using the method onCreateOptionsMenu
My code:
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
}
});
Thanks in advance!
I think you want to show/hide a menu item based on actions from users. To do that you must use onCreateOptionsMenu and whenever you want to show/hide the menu item, then call invalidateOptionsMenu (this method will call onCreateOptionsMenu again).
boolean mShowMenu = false;
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.your_menu, menu);
MenuItem item = menu.findItem(R.id.your_menu_item);
item.setVisible(showMenu);
return true;
}
And in your code, when you want to show menu item.
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
mShowMenu = true;
invalidateOptionsMenu();
}
});
And give it a try.
You should need to create menu Interface in xml File Like this
<item
android:id="#+id/settings"
android:title="Setting"
app:showAsAction="never" />
<item
android:id="#+id/my_activity"
android:title="My Activity"
app:showAsAction="always"
android:icon="#android:drawable/btn_radio"/>
After that in the Java code of a particular class You need to create the code like this;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.my_activity) {
Intent intent1 = new Intent(this,MyActivity.class);
this.startActivity(intent1);
return true;
}
if (id == R.id.settings) {
Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Hopefully this may resolve your problem
I have two option items in a Toolbar. When one item is clicked, that item will be enabled. Then another item must be disabled. But, once the item was disabled I can't fire click event anymore on that item. Is there anyway that I can click on the disable item?
Thank you
I did like this but its not working anymore
MenuItem tureMenuItem;
MenuItem dingMenuItem
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.lore_fragment, menu);
tureMenuItem = menu.findItem(R.id.menu_ture);
dingMenuItem = menu.findItem(R.id.menu_ding);
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_ding:
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
break;
case R.id.menu_ture:
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
break;
}
return super.onOptionsItemSelected(item);
}
Using setEnabled() will work. manage with your conditions.
you must be doing some action on those menu items right. You can enable the other item at the end of action.since you have not posted I have only option. Using Handler.PostDelayed.
boolean isMenuEnabled;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_settings) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
handler.postDelayed(runnable,1000);
return true;
}
else if(item.getItemId()== R.id.action_settings1) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
handler.postDelayed(runnable,1000);
return true;
}
return super.onOptionsItemSelected(item);
}
remove the callbacks
#Override
protected void onDestroy() {
handler.removeCallbacks(runnable);
super.onDestroy();
}
runnable with delay
Runnable runnable = new Runnable() {
#Override
public void run() {
if(isMenuEnabled){
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(true);
isMenuEnabled=false;
}
}
};
You need to check below example code to disable and enable menu item vice-versa.
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
if(menu.getItem(0))
{
menu.getItem(0).setEnable(false);
menu.getItem(1).setEnable(true);
}
else if(menu.getItem(1))
{
menu.getItem(1).setEnable(false);
menu.getItem(0).setEnable(true);
}
return super.onPrepareOptionsMenu(menu);
}
am having success with this code below but its not what i want.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
present.
getMenuInflater().inflate(R.menu.main, menu);
**MenuItem item = menu.findItem(R.id.helpp);
item.setVisible(false);**
return true;
}
this code above works but its not how i want it , i want to add this code on a button click event.
Menu menu;
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MenuItem item = menu.findItem(R.id.helpp);
item.setVisible(false);
}
});
this crashes my app kindly tell me how to do this as i have wasted 9 hours thanks.
First of all, you are probably getting null pointer exception, because you're accessing uninitialized field menu. Except that, Android's Activity class provides method invalidateOptionsMenu() which requests menu to be recreated.
Your activity class might look like:
boolean buttonClicked = true;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (buttonClicked) {
menu.findItem(R.id.helpp).setVisible(false);
}
return true;
}
Button listener:
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
buttonClicked = true;
invalidateOptionsMenu();
}
});
I have a menu which is inflated from main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/act_sync"
android:showAsAction="always"
android:actionLayout="#layout/sync_action"
android:icon="#android:drawable/ic_popup_sync" />
</menu>
and here is the code in the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
But onOptionsItemSelected is not called when I touch the menu item. When I remove the actionLayout attribute of the menu item, it works fine. How can I fix this? Thanks.
you should use below snippet ( Just for reference )
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
sync();
}
});
return true;
}
In addition to the answer provided by Vipul Shah.
Make sure you disable the clickable option of all items in your action layout:
android:clickable="false"
Otherwise, it may steal the click so that you still cant receive onClick call back.
How to set navigation bar in android and how to add option button on navigation bar ?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menuDialog == null) {
menuDialog = new Dialog(this, R.style.Dialog_Fullscreen);
menuDialog.setContentView(menuView);
menuDialog.show();
} else {
menuDialog.show();
}
return false;
}