My action bar has this button on the top right :
And when I click it, it gives me a settings menu item which does nothing on click:
I would like to disable the button in the first picture (looks like an ellipsis but rotated) from my ActionBar.
How would I go about doing that?
This is the default overflow menu that is set by the SDK when you create a new Android project.
To disable the menu you just have to remove the code that adds it. You should have a method onCreateOptionsMenu() overridden somewhere in your Activity, which you can remove.
selects a contextual menu item
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
more info here - http://developer.android.com/guide/topics/ui/menus.html#context-menu
Related
When trying to change an icon to a downloaded drawable (or actually changing to any other icon during runtime), the icon changes once on the ActionBar.
I actually want to remove the ActionBar and leave only the bottomnav (tabs) for navigation, yet whatever i'm doing the icon changes only on the ActionBar.
The item inside bottom_nav_menu.xml:
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_notifications"
app:showAsAction="ifRoom"/>
The code that changes the icon:
#Override
public boolean onPrepareOptionsMenu (Menu menu){
menu.clear();
getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
menu.getItem(2).setIcon(this.bitmap_pic);
Log.e(TAG, "Icon Changed");
return super.onPrepareOptionsMenu(menu);
}
The result - Icon stays blank on BottomNav but appears on the ActionBar.
Expected result: BottomNav icon will be the image that shown on the top right.
Thanks
EDIT!
Issue was fixed after inflating the main_activity layout that contains the BottomNavView
Now the problem the picture isn't showing properly, attached a screenshot (Image is grey instead of showing the icon like in the ActionBar in the first picture):
Edit 2
Icon is still grey instead of showing the bitmap picture.
Added:
MenuItemCompat.setIconTintMode(bottomNavigationView.getMenu().getItem(2), PorterDuff.Mode.CLEAR);
But it still shows up like in the picture below
Edit 3
Fixed the issue using:
bottomNavigationView.setItemIconTintList(null);
I'm not sure if onPrepareOptionsMenu invoked for bottom navigation bar.
You should have to update navigation menu icon from onCreate method of that Activity.
Refer below code,
val menu = navigation.menu
val menuItem = menu.findItem(R.id.navigation_notifications) // find particular menu-item using its ID.
menuItem?.icon = this.bitmap_pic
Solution for Gray icon tint,
add below line.
MenuItemCompat.setIconTintMode(menuItem, PorterDuff.Mode.DST)
I have a MainActivity along with a fragment. I have a refresh menu option that will refresh the data (Using AsyncTask). Is there any difference in inflating the refresh menu option in the fragment than in the MainActivity?
Note: This is in context with Udacity's Developing Android Apps, Lesson 2.
Yes, if i understand right, you want to inflate your activity toolbar menu from the the fragment. If that is the case you can do it like this
Yout Fragment class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setHasOptionsMenu is important
//it's telling the parent activity that he wants to participate in inflation of the menu
setHasOptionsMenu(true);
}
//Rest of your methods (onCreateView, onPause, onResume etc...)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflate the menu file
inflater.inflate(R.menu.your_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//handle click
return true;
}
return super.onOptionsItemSelected(item);
}
You can also use setMenuVisibility(boolean) if you want to hide/show menu in certain childfragments
If you inflate the menu from within the activity, it will be shown in all of the fragments you load inside the activity, but the action will be available to activity only (This is good if you want to do some general stuff with your menu action like starting new activity, displaying information popup etc). If you inflate the menu from within the fragment you will be able to handle menu items from withing the fragment, which will allow you to create more specific menu actions based on which fragment is currently active. For example if you have viewpager with 3 different fragments lets say:
FragmentOne for image browsing
FragmentTwo for video browsing
FragmentThre for Text browsing
Lets say you want to allow users to upload the images only, and you want that upload button to be located in the menu.
If you inflate the the menu from within the activity your upload button will be visible inside all of your fragments, and you would have to create a custom/logic for showing hiding menu items. If you create a menu from within the fragment, you will be able to handle and show the menu for the fragment you need
Long story short i think this depends on the use case of the activity/fragment and what you want to achieve with it
How will it be possible to have a menu button which call option-menus with the following condistions ( must be ):
App hides the titlebar :
requestWindowFeature(Window.FEATURE_NO_TITLE);
the targetversion in Manifest is set to 16 :
android:targetSdkVersion="16"
Some Infos and researches from my side:
Setting targetSdkVersion="10" shows the menu still in the bottom, which I would like to achieve.
Showing the titlebar shows the menubutton in top ( 3 points icon ) and the menu is also callable. But I need to hide the titlebar.
any hints , suggestions ?
thanks & regards
Andreas
Another option is to use slider menu and have it on left or right. You shouldn't include button on action bar (like facebook), you can open menu after item click or onFling event.
There is nice and easy tutorial that I tried:
http://oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
So this is the deal, I have the following
And I want to remove it but still be able to access the menu item through the menu button, is this possible?
Here is my menu xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/updateShares"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Refresh"/>
</menu>
And here is my code creating the menu
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_action_bar_main, menu);
return true;
}
EDIT_____________________________________________________________________
I still want the "refresh" menu to appear when you press the menu button on the phone, I just want to remove the menu bar, thats why I can't set any option menu to visible=false because then I can't see them when pressing the menu bar.
I am not sure with the mark166 answer. You can try this link also. And another thing what i have done and you can try is....
Just don't inflate the layout on click of Menu. Even dont create menu.xml file.
You can try with the following code in java file
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
Toast.makeText(getApplicationContext(), "Do what you want to show here", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
It works for me and using same thing in app.
You just comment your code for the menu and try this one.
Hope it will help you.
You can just add in your onCreate following snippet:
this.requestWindowFeature(Window.FEATURE_NO_TITLE); so your title bar will gone.
This may help you with removing menu item https://stackoverflow.com/a/13099201/1045579
as per your second screenshot change the target SDK version to 4.0.3 so the menu ... will not visible.
I love this site all you guys are awesome! but here is another problem I have:
In my app I have a webview that displays a website in the entire screen, I have made a code to show a menu through pushing the phone's menu button from where i want 2 things to happen 1st menu item Go back to main screen of the app, 2nd menu item quit the app or exit the app.
First problem:
after pressing the menu button it shows the menu... if I press it again it shows the two choices twice, if I press it again now both items shows 3 times and so on!
Second Problem:
after choosing any of the two choices nothing happens!
here is my code please tell me what I'm doing wrong!
Thanks
menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menuToMenu"
android:title="Menu Principal"
/>
<item
android:id="#+id/menuToSalir"
android:title="Salir"
/>
</menu>
Backtomain.java
import android.app.Activity;
import android.os.Bundle;
public class Backtomain extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and where I call the menu:
public boolean onPrepareOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mostrar = getMenuInflater();
mostrar.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuToMenu:
startActivity (new Intent("my.app.BACKTOMAIN"));
return true;
case R.id.menuToSalir:
finish();
System.exit(0);
return true;
}
return false;
}
You are calling super.onCreateOptionsMenu() from onPrepareOptionsMenu(). And, you are inflating the same options into the menu in onPrepareOptionsMenu(). Rename onPrepareOptionsMenu() to onCreateOptionsMenu(), and it will probably behave better.
Also:
If you think the my.app.BACKTOMAIN activity is running, you probably want to add FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_CLEAR_TOP to the Intent.
Get rid of the menuToSalir menu choice. No well-written Android application will call System.exit(0). Users leave your application by pressing the HOME button, no different than they might in a Web app.
To fix your first problem try onCreateOptionsMenu(), rather than onPrepareOptionsMenu().
I am not positive how to fix second problem, I usually create my menus all in java instead of using xml like you have.
I found the problem it works! I was missing the "s" at...
public boolean onOptionItemSelected(MenuItem item){
the right way is
public boolean onOptionsItemSelected(MenuItem item){
thanks for your help Tim and CommonsWare
I ran into this problem. In my case I had set the background color of the activity screen to black. When the menu popped up it had a transparent background and black text so I didn't see it working.