Android menuItem not displaying until you click on it....? - android

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...

Related

why does not appear my menu icon on action bar?

well the problem is that my icon of my option add item on the action bar does not appear(it has space for show the icon but it does not). my option just appear on the dropdown menu (the 3 points XD) thanks you. here is my code xml then my code java:
<?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/menuAddItem"
android:title="Add Item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="ifRoom"
/>
</menu>
java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater infladorMenu = new MenuInflater(this);
infladorMenu.inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menuAddItem:
this.listaDatos.add("Dato Añadido");
this.adaptador.notifyDataSetChanged();
break;
}
return super.onOptionsItemSelected(item);
}
As you can see the icon does not appear but the actionbar has space
As you can see the icon does not appear but the actionbar has space, my option is just showed in the dropdown menu. thank you for your help.

how to use setIcon function on android

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

How to disable the Long-Press event of Action Bar menu Item?

In Android, I Have created an action bar and added few menus item to it. When I click menu items, I am performing some action.
On long click I find a empty toast message. My Question is how to disable the toast on long click?
Instead trying to disable the empty "toast like" view, you may add the "android:title" attribute to describe what's the action actually do.
Example:
<item android:id="#+id/action_websearch"
android:icon="#drawable/action_search"
android:title="#string/action_websearch"
android:showAsAction="ifRoom|withText" />
in menu_add_key layout refer to a custom layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tumblr="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menuAddKey"
android:actionLayout="#layout/item_addkey"
android:showAsAction="always"
android:title=""/>
</menu>
define your item_addkey layout as you want
and in java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_add_key, menu);
menu.findItem(R.id.menuAddKey).getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showAddKeyDialog("");
}
});
return true;
}
Disable the long click on a menu item:
menu.findItem(R.id.menuAddKey).getActionView() return null

android option menu with icon

How to show icon with option menu.I have tried the following code but my option menu is without image icon.I am using android version 4.0 for developing app.
Java code :
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Contacts").setIcon(
R.drawable.ic_launcher);
return true;
}
Following is my app's screen shot
I need image to be displayed on the top of "Add Contacts" item.
Override OnPrepareOptionsMenu and add icon from there too
and if its for above 3.0, use android:showAsAction in xml.
eg. android:showAsAction="ifRoom|withText"
I tried the code in two line and it works:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Contacts");
menu.getItem(0).setIcon(R.drawable.ic_launcher);
return true;
}
You can create a custom menu like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add_contacts"
android:icon="#drawable/ic_launcher"
android:title="#string/add_contacts"
/>
</menu>
And then inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
More on this here:
http://developer.android.com/guide/topics/ui/menus.html#options-menu
you can directly set this into the xml file.
<item android:id="#+id/add_contacts"
android:icon="#android:drawable/plus_icon"
android:title="Add Contacts"/>
You Can try Following this Link.
Check this out and tell me if it worked or not.
Or you can do some thing like this.
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 access 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);
}
}
To enable Option Menu with Icons:
Wrap the Items with an Item with showAsAction="always" and a 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/title_menu"
android:icon="#mipmap/ic_icon_menu"
app:showAsAction="always">
<menu>
<item
android:id="#+id/action1"
android:orderInCategory="100"
android:title="#string/action1"
android:icon="#mipmap/ic_icon_action1"
app:showAsAction="never" />
</menu>
</item>
</menu>
If you use some following attribute in manifest file then it's will be show your icon....
<activity android:name=".ui.CategoryActivity"
android:label="#string/app_name"
**android:theme="#android:style/Theme.NoTitleBar"**></activity>
It's work fine for me...:)
+1 for my own effort...
**must be enter.
Easiest way is to use the #drawable only when setting your menu item.
OR
Simply put the #drawable before the title declaration.
<?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/addToFavorites"
android:icon = "#drawable/ic_favorite_border_white_24dp"
android:title = "Hello"
app:showAsAction="always" />
<item
android:id ="#+id/about"
android:title ="About"
app:showAsAction="never" />
</menu>
the problem is the Androidmanifest.xml.
Remove android:theme="#style/AppTheme" and it will work just fine

Android Option Menu showing as list

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">

Categories

Resources