android remove item from actionbar buttonclick - android

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();
}
});

Related

BaseActivity with common toolbar and menuItems activities can not catch onMenuItemClick

I've the base activity where all toolbar initializations and options menu are done, the activities extending the base can not fire onitemclick
In the base i have
public class BaseActivity extends AppCompatActivity {
private MenuItem refresh;
public Toolbar getToolbar() {
return toolbar;
}
public MenuItem getRefresh() {
return refresh;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
refresh = menu.findItem(R.id.action_refresh);
refresh.setActionView(R.layout.menu_item_view);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return false;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (mState) {
case Const.STATE_IDLE:
refresh.setVisible(true);
break;
case STATE_WORKING:
refresh.setVisible(false);
break;
default:
refresh.setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
}
In one of the activites I handle it like
public class CommentsActivity extends BaseToolbarActivity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.action_refresh){
setState(Const.STATE_WORKING);
showMsg(contentRoot,"oops");
return true;
}
return super.onOptionsItemSelected(item);
}
}
but the click items won't fire
After a while I came to realize that since I was setting a custom layout for my items (useful for animations) an option menu with a custom view that is either set in the xml or dynamically using
item.setActionView(R.layout.menu_lay);
Just like my problem above the menu item can never be invoked by the normal onOptionsItemSelected listener so the way to make it work is to implement onClickListener on the item's custom View so in my case the way to make it invoke is by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getRefresh.getActionView().setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//refresh some data
}
});
return true;
}

How to click disabled option menu item in Toolbar android?

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);
}

updating ActionBar status from OnCancelled method of AsyncTask

I have an a refresh button in ActionBar that refreshes the current activity when Clicked. This button is activated only when AysncTask is callcelled
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.desc_xml, menu);
refreshItem = menu.findItem(R.id.action_refresh);
return true;
}
onPrepareOptionsMenu method, I set it to false
#Override
public boolean onPrepareOptionsMenu(Menu menu){
refreshItem.setEnabled(false);
super.onPrepareOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
new AsycnTaskName(MyClass.this).execute();
break;
}
return super.onOptionsItemSelected(item);
}
and inside onCancelled method of AsyncTask, I re-enabled it.
#Override
protected void onCancelled(String result) {
Toast.makeText(c, "Enabled", Toast.LENGTH_LONG).show();
invalidateOptionsMenu();
refreshItem.setEnabled(true)
}
but the status of the refresh button stays disable even after onCancelled method is executed.
What Am I doing wrong ?
When you call invalidateOptionsMenu() it calls onPrepareOptionsMenu() again.
Your problem is, you are setting refreshItem.setEnabled(false) each time while preparing option menu. So this menu does not get activated even if you set it to true.
So you need to take one global variable at class level and set it to false by default. And then handle this variable only.
Like below
private boolean refreshItemEnabled = false; // At class level
.
#Override
public boolean onPrepareOptionsMenu(Menu menu){
refreshItem.setEnabled(refreshItemEnabled); // Set variable here, not hardcoded value
super.onPrepareOptionsMenu(menu);
return true;
}
and then your onCancelled() will be like
#Override
protected void onCancelled(String result) {
Toast.makeText(c, "Enabled", Toast.LENGTH_LONG).show();
refreshItemEnabled = true; // Set menu enabled
invalidateOptionsMenu();
}
declare a static boolean variable in your main activity class.
static boolean setStatus = false;
in your onPrepareOptionsMenu(Menu menu),
#Override
public boolean onPrepareOptionsMenu(Menu menu){
refreshItem.setEnabled(YourClassName.setStatus);
super.onPrepareOptionsMenu(menu);
return true;
}
update that variable in your OnCancelled method,
YourClassName.setStatus = true;
refreshItem.setEnabled(YourClassName.setStatus)
You can also set your button to invisible when it is inactive,
public boolean onPrepareOptionsMenu(Menu menu){
refreshItem.setEnabled(YourClassName.setStatus);
refreshItem.setVisible(YourClassName.setStatus);
super.onPrepareOptionsMenu(menu);
return true;
}

How to hide options menu

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)

How to implement a onMenuItemClickListener in android

How do I implement an onMenuItemClickListener?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.gameoptions, menu);
menu.findItem(R.id.menu_item).setIntent(new Intent(this, QMenuActivity.class));
menu.findItem(R.id.back_item).setOnMenuItemClickListener;
return true;
}
I want the back_item once clicked on to call a function within the page, how do I do it?
I'm assuming you want to access a non-static function in your activity. Without too much information from you, try something like what's below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.back_item);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
YourActivity.this.someFunctionInYourActivity();
return true;
}
});
return true;
}
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
change the code to include MenuItem after new.

Categories

Resources