Hello guys I m trying to set an Icon in a menuItem. I read the Android Developers Blog and it says:
public abstract MenuItem setIcon (int iconRes)
Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons.
This method will set the resource ID of the icon which will be used to lazily get the Drawable when this item is being shown.
Parameters :iconRes The new icon (as a resource ID) to be displayed.
Returns : This Item so additional setters can be called.
I should put as parameter an int. In particoulare the ID of my icon. But I can't figure out where I have to find this ID. I simply put the icon named "badIcon.ico" inside the drawable folder. Now should I proceed?
Thanks guys
this is so simple....
See this tutorial..here
Create menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/next"
android:icon="#drawable/ic_next"
android:title="#string/next" />
<item android:id="#+id/previous"
android:icon="#drawable/ic_previous"
android:title="#string/previous" />
<item android:id="#+id/list"
android:icon="#drawable/ic_list"
android:title="#string/list" />
</menu>
And now you will be able to set ICON on menu
Now in CreateOptionMenu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
And to get that menu..
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.next:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
…
default:
return super.onOptionsItemSelected(item);
}
If you want to add icons to your menu item in static way you can write
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/yourId"
android:icon="#drawable/badIcon"
android:title="#string/yourTitle" />
</menu>
You can add icon programmatically like this
menu.add(0, MENU_TITLE, 0, "title").setIcon(R.drawable.badIcon);
Related
I have a menu in the title bar of my Android app, that is not a pop-up Menu. In it I have some items. I want to add a line, or a separator, between just one pair of items in the list. I don't want dividers between all the items, just one pair. I tryed with groups who have different IDs, not worked, also tryed with android:actionlayout, no succes.
My current menu looks like this in design mode. I want to do something like this.
My XML containing my menu:
<?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:title="#string/editare_nume_jucatori">
<!-- submeniul meu -->
<menu>
<item
android:id="#+id/M_Jucator1"
android:enabled="true"
android:title="#string/Jucatorul1" />
<item
android:id="#+id/M_Jucator2"
android:enabled="true"
android:title="#string/Jucatorul2" />
</menu>
</item>
<item
android:id="#+id/M_Detalii"
android:icon="#drawable/dice10"
android:title="#string/detalii_text_meniu" />
<item
android:id="#+id/M_Despre_Aplicatie"
android:icon="#drawable/dice10"
android:title="#string/despre_aplicatie" />
<item
android:id="#+id/M_Iesire_Aplicatie"
android:icon="#drawable/m3"
android:title="#string/IesireAplicatie" />
</menu>
My Java code for the menu:
Menu meniu1; //a variable used in my menu
//to show my menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.meniul_meu, menu);
meniu1 = menu; //this is my variable from up declaration
return true;
}
//here execute different actions for items clicked in menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//click on my item ID from menu and execute
case R.id.M_Jucator1:
...(code code)...
return true;
//click on my item ID from menu and execute
case R.id.M_Jucator2:
..(code code)...
return true;
//click on my item ID from menu and execute
case R.id.M_Detalii:
..(code code)...
return true;
//cand dai click pe iesire din meniu
case R.id.M_Iesire_Aplicatie:
..(code code)..
return true;
default:
return super.onOptionsItemSelected(item);
}
} //finish meniu codes
Group your items in the XML menu, such as:
......
<group>
<items...
</group>
<group>
<items...
</group>
.....
And the in your code use:
final Menu menu = ((Toolbar)this.findViewById(R.id.your_toolbar)).getMenu();
MenuCompat.setGroupDividerEnabled(menu, true);
I need to show the submenu below the bar, not on top of the bar itself.
Copying my actionbar xml below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/action_pages"
android:orderInCategory="1"
android:showAsAction="withText|always"
android:icon="#drawable/ic_action_pages"
android:title="">
<menu>
<item android:id="#+id/item1" android:title="Placeholder"></item>
</menu>
</item>
</menu>
In the activity (App also has a navigation drawer)
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
Simply.
<style name="AppTheme" parent="AppBaseTheme">
<item name="actionOverflowMenuStyle">#style/OverflowMenu</item>
</style>
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!-- Required for pre-Lollipop. -->
<item name="overlapAnchor">false</item>
<!-- Required for Lollipop. -->
<item name="android:overlapAnchor">false</item>
</style>
Preamble
As usual, I faced a strange problem while developing an Android app, tried to find a solution and landed to this question. As it was in many cases before, there is no an answer. So I was compelled to solve the problem from scratch and now posting the answer with my workaround.
Input
I have an Android app with action bar and some menu items, which have to be extended with dropdown submenu. First attempt was to implement it as suggested by Android documentation. So I added new menu item menu_sort into existing action bar menu and sub-menu container into it:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/id1" android:icon="#drawable/ic_1"
android:title="#string/id1" android:showAsAction="withText|always"/>
...
<item
android:id="#+id/menu_sort"
android:icon="#drawable/ic_menu_sort_selector"
android:title="▾"
android:titleCondensed="▾"
android:showAsAction="withText|always">
<menu>
<item
android:id="#+id/menu_sort_by_name"
android:showAsAction="never"
android:checkable="true"
android:checked="true"
android:title="#string/sort_by_name"/>
<item
android:id="#+id/menu_sort_by_priority"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_priority"/>
<item
android:id="#+id/menu_sort_by_memory"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_memory"/>
</menu>
</item>
</menu>
Result
The effect was exactly as described in the question: the submenu is displayed on top of the action bar. Here is the screenshot taken on Android 5.1.1:
I played with many options and code snippets - nothing helped. Finally I came to the following
Solution
First, move all the submenu into a separate menu layout, say, menu/sorting.xml, and remove it from menu_sort item of the main menu (shown above).
Second, modify or create onPrepareOptionsMenu event handler with the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// as solution utilizes PopupMenu,
// take care about older Android versions if necessry
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
// here goes most crazy part: we use menu id
// to retrieve corresponding view, automatically created by OS;
// imho, this is a hack, and menu item should have getView() method or similar;
View menuItemView = findViewById(R.id.menu_sort);
// by the way, menuItemView could probably be null under some circumstances
// create a popup anchored to the view (menu item)
final PopupMenu popupMenu = new PopupMenu(this, menuItemView);
// API 14
// popupMenu.inflate(R.menu.sorting);
// API 11 (HONEYCOMB)
popupMenu.getMenuInflater().inflate(R.menu.sorting, popupMenu.getMenu());
// process popup clicks as appropriate
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId())
{
// ... place some code
}
return true;
}
});
// bind the popup to the item menu
menu.findItem(R.id.menu_sort).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
}
Here is the result:
Now the dropdown is displayed as expected from very beginning.
#Stan's solution doesn't work for me, so here's my way to implement sub-menu on top of ActionBar (but below the main-menu of course):
I've created 2 xml files: menu_main.xml and menu_more.xml located in res/menu directory
The first one 'menu_main.xml' contain the menu:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- our addMenu doesn't have sub-items-->
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_note_add_white_24dp"
android:title="#string/action_add"
app:showAsAction="ifRoom"/>
<!-- our moreMenu which show drop-down menu when clicked-->
<item
android:id="#+id/action_more"
android:icon="#drawable/ic_more_vert_white_24dp"
android:title="#string/action_more" <!--in text: "more"-->
app:showAsAction="always"/>
</menu>
And the second one 'menu_more.xml' contain the drop-down menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- This menu will be hidden by default-->
<!-- But will be visible when moreMenu with '#+id/action_more' is clicked-->
<item
android:id="#+id/action_settings"
app:showAsAction="ifRoom|withText"
android:title="#string/action_settings" <!-- In text: "Settings"-->
android:visible="true"/>
</menu>
Here is what previous menus look like:
result-after-add-2-xmls (i have not enough 10 reputation to display image)
In the activity, i've overridden this method:
public boolean onPrepareOptionsMenu(Menu menu)
In the previous method, i get reference to the main menuItem (in this case is menu with #+id/action_more located in menu_main.xml file), then set setOnMenuItemClickListener on it and finally, declare and set up a PopupMenu instance to manage and display sub-menu items:
// show popup menu when menuMore clicked
menu.findItem(R.id.action_more).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
// get reference to menuMore item
View menuMore = findViewById(item.getItemId());
// create a popup anchored to the view (menuMore)
// notes: if declare and set up PopupMenu Outside of this onMenuItemClick()
// then it'll not work!
// Because: the view you put into PopupMenu() could be null
final PopupMenu popupMenu = new PopupMenu(getApplicationContext(), menuMore);
// inflate 'menu_more.xml' layout file
// which contain all sub-items of menu
popupMenu.getMenuInflater().inflate(R.menu.menu_more, popupMenu.getMenu());
// process popup clicks on sub-items
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "showing SettingsActivity..",
Toast.LENGTH_SHORT).show();
break;
// more items go here
}
return true;
}
});
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
And here is final result:
final-look-drop-down-menu
I'm coming back to my main activity from a fragment and for some logic I have to change the appearence of an icon on the action bar menu.
This is the menu on the action bar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="it.gn.sfa.Main">
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="Search" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_action_filter_empty"
android:showAsAction="ifRoom"
android:title="Filter" />
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="New" />
</menu>
I have to change the sencond item (the one with id = action_filter).
I've tried different solutions, found on different post. The most rated is
mOptionsMenu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_action_filter));
but seems not to work.
On the other side getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_filter)); changes the logo, and I don't want so.
How can i change only the second item on menu?
try this one
mOptionsMenu.findItem(R.id.action_filter).setIcon(R.drawable.ic_action_filter);
Assuming you have it all set up for mOptionsMenu in
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// inflating your menu here
mOptionsMenu = menu;
return super.onCreateOptionsMenu(menu);
}
Hope it helps :)
I hope it will be help for you
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>" + "Messages" + "</font>"));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.messagebar_color)));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_arrow_black);
You have to modify your onCreateOptionsMenu(Menu menu)
I changed the color of my search bar programmatically. I am posting the code here. Hope it helps.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
menu.getItem(0).setIcon(getTintedDrawable(R.drawable.search, R.color.blue));
return super.onCreateOptionsMenu(menu);
}
Where getTintedDrawable() is a function that i created which returns a drawable. So all you need to do is replace getTintedDrawable(R.drawable.search, R.color.blue) by your drawable.
NOTE: I have used menu.getItem(0) my code since I had only 1 item defined in menu/items.xml. If you have multiple try different values (from 0 to one less than number of menu items). My guess would be that its the number at which the item is defined but I'm not too sure.
I manage to rotate/change the icon this way:
MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);
<prepare the image view from drawable here>
item.setActionView(imageView);
Seems to work OK.
You could also simply use the item.setIcon() instead.
I have created a menu which is activated on clicking the menu button on the device however it is showing as a list
My in res/menu/menu.xml code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuRefresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"/>
<item android:id="#+id/menuAbout"
android:icon="#drawable/ic_menu_info_details"
android:title="About"/>
</menu>
and in my main activity i have:
//Initiate Menu XMl file
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
/**
* Even handling for individual menu items selected
* Identity single menu item by its id
*/
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuRefresh:
Toast.makeText(MainActivity.this, "Refresh Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuAbout:
Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If i select an item from the list, i get the notification as expected.
How do i make the menu look like the Options menu on the android dev site?
That is what the options menu looks like on new devices, a list. If you want your options to be part of the top bar (called the Action Bar), add android:showAsAction to your xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuRefresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"
android:showAsAction = "always"/>
<item android:id="#+id/menuAbout"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction = "always"/>
</menu>
For demo purposes, I chose the attribute to be "always", but you have more options:
"ifRoom"
"never"
"withText"
"always"
"collapseActionView"
Even in new devices, you can set the theme to an old one and the menu will display in the old 6-item table layout:
<style name="AppBaseTheme" parent="android:Theme">
I believe my question is simple for most experienced Android developers but I cannot figure it out.
I am trying to set the name in a menuItem and make the text WHITE colored.
The text gets displayed only if I click on the menuItem, or rather when I click on the menuItem, the text turns white and is readable.
Any useful help how I can make the text white and visible all the time in the menuItem?
item.setTitle(this.task.getName()); is supposed to be white and visible all the time in the menu.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.show_task_feedback_menu, menu );
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch ( item.getItemId() )
{
case R.id.show_task_feedback_menu_add_feedback:
item.setTitle(this.task.getName()); <------------- Here is the problem!!!!!!!!!!
this.startTaskFeedback();
return true;
default: return true;
}
}
The XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/show_task_feedback_menu_add_feedback"
>
</item>
</menu>
I think I understand your question, seems to me you just need to add the following line to your xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/show_task_feedback_menu_add_feedback"
android:showAsAction="ifRoom|withText"> <!-- this line -->
</item>
</menu>
Not sure I understand which text you want to set to white...