Hide Action bar onCreateOptionMenu icon for some activites and fragment android - android

A sliding menu was implemented using google navigation drawer with actionbar class. My problem is onCreateOptionsMenu is being shown in every activities. how can i make onCreateOptionsMenu icon visible and invisible at will. Any idea please.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}

Either you create menu runtime or have different menu layouts as per your need and in onCreateOptionsMenu of your activity set that layout or runtime create those menus or if you want to show no menu icons then just do menu.clear()
Activity A
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_a, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
Activity B
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.clear();
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_b, menu);
// OR
Drawable tmpDrawable = getResources().getDrawable(R.drawable.share_sharingicon);
// tmpDrawable.setColorFilter(getResources().getColor(R.color.colorGrayFont), PorterDuff.Mode.MULTIPLY);
menu.add("ShareMap").setIcon(tmpDrawable).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Directions").setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ShareMap:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
case R.id.Directions:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}

To hide or show some icon on action bar, you need override method:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem your_icon = menu.findItem(R.id.action_your_icon);
//show icon
your_icon.setvisible(true);
//hide icon
your_icon.setvisible(false);
...
}
Besides you need 'supportInvalidateOptionsMenu()' to Invalidate the activity's options menu when action bar items have some change

Related

Fragment Menuitem Click

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragement, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.color_menu:
intent.setClass(rootView.getContext(), CandleColorActivity.class);
getActivity().startActivityForResult(intent,COLOR_ACTION);
break;
}
return super.onOptionsItemSelected(item);
}
Using the above code menu item is visible in the fragment but item click is not working. Menu item xml:
<item
android:title="selectColor"
android:icon="#drawable/addcolor"
app:showAsAction="always"
android:id="#+id/color_menu"></item>
main activity menu xml always show on each of the fragment
<item
android:title="menu"
android:icon="#drawable/menu"
app:showAsAction="always"
android:id="#+id/uper_menu"></item>
Main activity menu java code open a dialog box on the item click
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.uper_menu:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.menu_dialog);
alertDialog = builder.show();
alertDialog.getWindow().setGravity(Gravity.BOTTOM);
alertDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
alertDialog.getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
viewIds();
break;
default:
break;
}
return true;
}
You should not call the super class' onOptionsItemSelected(), if you handled the event yourself. So change your method to this:
public boolean onOptionsItemSelected(MenuItem item) {
...
switch (item.getItemId()) {
case R.id.color_menu:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT
In fragment and activity, only return true, if you handled the event, otherwise return super.onOptionsItemSelected(item);
Reason is, that the system first asks the activity to handle the event, and if the activity says it did handle it (by returning true), the system doesn't ask the fragment anymore.

Handling Click Event for menu Used for Floating Action Button Options

I used a library recently that Provides a Floating Action Button and changing to a Something like Navigation bar by clicking on it.
but i cant set click Event for my navigation bar Items.
after Pressing on items i don't see any reaction!
Anyone can help me?
fabOptions = (FabOptions) findViewById(R.id.fabcontainer);
fabOptions.setButtonsMenu(R.menu.menu1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
newBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void newBack() {
Toast.makeText(this, "back", Toast.LENGTH_SHORT).show();
}
}
First of all you should have to use break whenever you use switch statement otherwise the program will go to default status. so use break; the code below.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getId(); // Retrieve the id of the selected menu item
switch(id) {
case R.id.back:
newBack();
return true;
break;
case ...
}
return super.onOptionsItemSelected(item);
}

What is the best (most efficient or conventional) way to control "MenuItem"s in Android's "ActionBar"?

When controlling a MenuItem, I have been doing this:
Menu menu;
(...)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
return true;
}
(...)
public void handleSearch(View view) {
Button button = (Button) view;
if(menu.findItem(R.id.action_search).isVisible()) {
button.setText(R.string.button_search_show);
menu.findItem(R.id.action_search).setVisible(false);
} else {
button.setText(R.string.button_search_hide);
menu.findItem(R.id.action_search).setVisible(true);
}
}
"this" being referencing the menu created in onCreateOptionsMenu with a Menu that any method in the class can use. The handleSearch method controls MenuItems by using findItem twice. This doesn't feel very conventional or efficient (a very scientific observation, I might add). Is there a more conventional or efficient way to do this?
You can save MenuItem in a variable instead of using findItem twice.
MenuItem myMenuitem = menu.findItem(R.id.action_search);
There is a method that handles actionBar's menu itself.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return (true);
case R.id.menu_home:
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
return (true);
}
return (super.onOptionsItemSelected(item));
}

Not perform action in menu items

I have one action bar activity.when i am switching from one fragment to another fragment that fragment have two menus like setting,gridview.When i click the setting it perform the action but not in gridview.In each fragment first menu item only perform action not in second fragment.Please give me solution thanks.....
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.settingmenu, menu);
getActivity().getMenuInflater().inflate(R.menu.menugridcalendar, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_setting:
Log.i(TAG,"InsdieGridmenu");
Intent intent = new Intent(getActivity(), SettingPage.class);
startActivity(intent);
break;
case R.id.gridmenuid:
Activity activity = getActivity();
if (activity instanceof ListItemClickListener) {
((ListItemClickListener)activity).OpenGridView();
}
break;
}
return true;
}

How to hide an optionsmenu button at runtime

In my app i have an optionsmenu. It has 2 buttons. Depending on a boolean value i would like to show/hide one of the buttons. I've got the following code but it doesn't hide the button. How can i do this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menushowmoredetails, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(displayRotaDetails.equalsIgnoreCase("false")){
if(item.getItemId() == R.id.moredetails)
item.setVisible(false);
}
switch (item.getItemId()) {
case R.id.back:
onBackPressed();
return true;
case R.id.moredetails:
You have to use the onPrepareOptionMenu method like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// add your conditions here and change 0 with the R.id.moredetails item postion.
if(displayRotaDetails.equalsIgnoreCase("false")){
menu.getItem(1).setVisible(false);
}
}

Categories

Resources