This question already has answers here:
How to add button in ActionBar(Android)?
(2 answers)
Add button in android action bar
(2 answers)
Closed 5 years ago.
As title says i don't know how to add button to android action bar near title of app with icon.
I need to add it here as shown on picture
enter image description here
First, create a menu resource, name it whatever you want.
Then, add this to your new .xml resource:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="AlwaysShowAction">
<item android:id="#+id/your_item_id"
android:icon="#drawable/your_drawable_id"
android:title="#string/your_string_id"
app:showAsAction="always" />
</menu>
The item tag defines the id, the icon and the title. the showAsAction defines if item is located at the action bar or submenu.
It's recommended to use string resources for your title, specially if you want to translate your app.
The menu icon is defined by your drawable resource. In order to use the correct size, I recommend to use the default icon asset wizard.
Add this to your Activity to add the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu_name, menu);
return true;
}
In order to detect menu pushes, use this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.your_item_id) {
//Do your stuff here
return true;
}
return super.onOptionsItemSelected(item);
}
More info: https://developer.android.com/guide/topics/ui/menus.html
Related
Graying out the Save item in toolbar only happens in design mode but never on runtime.
I'm using this code:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(count > 0) {
menuItemSave.setEnabled(true);
} else {
menuItemSave.setEnabled(false);
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
menuItemSave = menu.findItem(R.id.save);
return true;
}
It's enabling/disabling but it just really not turning into gray when disabled.
You can use custom color to change color when button is enabled or disabled. [ Change color according to your requirement ]
colors/custom_button_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FF0000" android:state_enabled="false" />
<item android:color="#CCCCCC"/>
</selector>
In menu.xml file set to your required button item like
android:textColor="#color/custom_button_color"
According to Android docs onPrepareOptionsMenu is called just before menu is opened.But if menu is always open, it will not be called.
So,
On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu that is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items.
This is my layout for a menu.
Notice the item:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_best_stores"
android:title="#string/showStores"
android:onClick="showHideStores"
android:enabled="true"
android:checkable="true"
app:showAsAction="never"
/>
</menu>
This is the onAction function.
public void showHideStores(MenuItem m) {
m.setChecked(!m.isChecked());
SHOWING_STORES = !SHOWING_STORES;
showStoresProcess(SHOWING_STORES);
refreshData();
invalidateOptionsMenu();
}
Whenever I run my program, the function works because things that are supposed to happen, happen. Except, for the part where the ItemMenu itself changes... It just doesn't do anything, the checkbox stays unchecked... I also tried setTitle, doesn't change anything either.
So, line 1 isn't producing any change.
What is going on?
This is the code where I inflate the Menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_list_actions, menu);
super.onCreateOptionsMenu(menu);
this.menu = menu;
refreshData();
return true;
}
Notice how the function works because the screen changes, but the menu itself doesn't get "checked off" or "unchecked".
The problem is explained as follow:
When you call invalidateOptionsMenu(), it will reset to default menu. So nothing happened.
Solution: override onPrepareOptionsMenu()
#override
public bool onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
MenuItem m = memenu.findItem(R.id.YourItemId);
m.setChecked(!m.isChecked());//something like this
return true;
}
Remove your call to invalidateOptionsMenu(). Calling invalidateOptionsMenu() is going trigger a call back to onCreateOptionsMenu() which is going to create a new menu which will create a new un-checked MenuItem, basically exactly what you don't want to halpen.
I have a refresh icon on my menu which when clicked will refresh the contents of the Activity. But the problem is that the refresh icon is still clickable while it is refreshing which I do not want. Here is my menu.xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:icon="#mipmap/ic_refresh"
android:enabled="true"
android:title="#string/action_settings"
app:showAsAction="always"/>
And here is where I handle the click event for the icon
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
// getData() fetches the data and updates the display
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Also, is there a way to maybe dim the icon so that the user knows that they have already clicked the icon?
When you click the icon, use the isEnabled() method of your Menuitem to check if it is currently enabled. If it is, then disable it until the data is loaded. Once the data loads successfully, then you can enable it again. Example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(item.isEnabled()) {
// "Dim" the icon like you said. You can use other alpha values if you like.
item.getIcon().setAlpha(130);
// Disable the menu icon
item.setEnabled(false);
}
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, you said that you have a getData() method which updates the display after it fetches the data. Once the data is fetched, check if the Menuitem is disabled. If it is disabled, then enable it again. Example:
if(item != null && item.isEnabled() == false) {
// "Undim" the icon
item.getIcon().setAlpha(255);
// Enable it again
item.setEnabled(true);
}
You can use setEnabled(true) method to enable/disable menu item
You could create a second menu.xml, that is used after the refresh button is clicked. To force a menu redraw call invalidateOptionsMenu();
In this second menu.xml, the refresh button could be disabled, or switched to a diffrent one, indicating the user, that the refresh is on going.
Easiest way to fix this is to hold on to the menu item in your activity.
private MenuItem mRefreshItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater infl = new MenuInflater(this);
infl.inflate(R.menu.menu, menu);
mRefreshItem = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}
/** Call this when you begin or end loading */
private void setRefreshEnabled(boolean enabled) {
mRefreshItem.setEnabled(enabled);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've tried searching around but I am only limited to a very minimal amount of internet usage till 16th so I was looking around on how I could implement buttons to my action bar.
Right so when you enter the playlist section on my app, there is an about button on the layout. But id like to move that button to my action bar, how can I achieve this?
This is an example how you can add HELP icon to your action bar
Create a menu XML inside your menu folder like the following
<item
android:id="#+id/help_menu_item"
android:icon="#android:drawable/ic_menu_help"
android:title="Help"
android:showAsAction="ifRoom" />
And in your activity do something like that
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.help_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help_menu_item:
//do your menu press here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayUseLogoEnabled(false);
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.titlebar);
homeButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_home);
menuButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_menu);
titlebar_title = (TitleTextView)mActionBar.getCustomView().findViewById(R.id.titlebar_title);
titlebar_title.setText("TITLE");
you can give click events for the buttons also.Here i am taking one custom layout added to action bar. so create custom layout in your res/layout folder. and give appropriate id's to them.and give click events
hope this helps
Hello I having a problem trying to get eclipse to recognized my menu icon. I want to have the icon appear in the action bar and when clicked, I want to display a toast.The problem is that my save icon is not being recognized in eclipse. Here is my code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profilemenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case android.R.id.home:
finish();
case android.R.id.save:
save cannot be resolved or is not a field
}
return false;
}
The menu :
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save" android:showAsAction="ifRoom"
android:title="#string/save_str" android:icon="#drawable/content_save" />
</menu>
You're using the incorrect identifier within the switch statement, you should use just R.id.Save, like so:
switch(item.getItemId()) {
case R.id.home:
finish();
case R.id.save:
Toast.makeText(getContext(), "Toast message", Toast.LENGTH_SHORT).show();
}
Using the android identifier means you are trying to find a resource built into the android sdk which doesn't exist.