Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to add an icon to the action bar and my code does not show icon in the action bar. what it shows is an icon with three dots and when i click on it it shows back by clicking it performs my specified operation.here is my following code

menu1.xml
Java
<item
        android:id="@+id/action_back"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/back"
        android:title="Back"/>


activity
Java
@Override
     public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.menu1, menu);
       return true;
     }
    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==R.id.action_back){
            Toast.makeText(getApplicationContext(), "Teritary", Toast.LENGTH_SHORT).show();
        }


       return true;
     }
Posted

The android:orderInCategory attribute dictates the order in which the menu items will appear within the menu when it is displayed. The menu items are arranged from left to right in ascending order of integer value of orderInCategory (i.e. 1,2,3... -> left to right).

android:orderInCategory is actually useful in two ways.
=> For menu items in ActionBar. Items will appear from left to right in ActionBar depending on the ascending order.

=> For menu items in overflow menu. Overflow menu items will be displayed from top to bottom depending upon the ascending order you have specified.

So, (Just a wild guess) make it android:orderInCategory="1", and you are good to go.

-KR
 
Share this answer
 
v2
Comments
Rahul Ramakrishnan 15-Jun-15 9:36am    
thanks for the reply. but the result is same. it doesn't show the icon
Krunal Rohit 16-Jun-15 2:04am    
Remove that line.

-KR
Hi,

I believe now the problem is compatibility.

In your menu.xml file, create a new XML Namespace.

This becomes:
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mynamespace="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_back"
        android:orderInCategory="100"
        android:icon="@drawable/back"
        android:title="Back"
        android:showAsAction="always"
        mynamespace:showAsAction="always" />
</menu>


The Java Source code could remain as it is.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900