Alaa
Alaa

Reputation: 541

Adding items to actionbar

I want to use action bar in my application. So far I was able to add the action bar from the support library. Now I want to add items to my action bar. I want the icons of the items to be displayed in my action bar, so I did the following:

first I created menu.xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_locate"
      android:icon="@drawable/ic_action_location_found"
      android:title="@string/locate"
      android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->

and I added these functions to my mainActivity

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}
    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_locate:
             Toast.makeText(this,"locate is selected",Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

When I run my application I only see the title of the action bar, the locate icon is not there. When I touch the options button on my phone a list with only locate in it appears. What I need is to have locate icon appearing on the right corner of the action bar. Can anybody please tell me what I'm doing wrong and why its not appearing on the right corner of the action bar??

Upvotes: 0

Views: 557

Answers (2)

Namrata
Namrata

Reputation: 1684

Try This :-

Menu Xml:

<item
    android:id="@+id/action_location_found"
    android:clickable="true"
    android:icon="@drawable/more_btn"
    android:showAsAction="always"
    android:title="action_location_found">
    <menu>
        <item
            android:id="@+id/action_user_profile"
            android:orderInCategory="1"
            android:showAsAction="never"
            android:title="User Profile">
        </item>
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="2"
            android:showAsAction="never"
            android:title="Settings">
        </item>
    </menu>
</item>

In your Activity:-

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {



    // Handle action buttons
    switch (item.getItemId()) {



    case R.id.action_user_profile:


         Toast.makeText(getApplicationContext(), "User Profile",
          Toast.LENGTH_LONG).show();


        break;
    case R.id.action_settings:


          Toast.makeText(getApplicationContext(), "Setting",
         Toast.LENGTH_LONG).show();


        break;

    default:
        return true;
    }
    return super.onOptionsItemSelected(item);

}

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

You need to add a namespace

xmlns:yourapp="http://schemas.android.com/apk/res-auto" 

Then

yourapp:showAsAction="always" 

Edit:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto"   >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_locate"
      android:icon="@drawable/ic_action_location_found"
      android:title="@string/locate"
      yourapp:showAsAction="always" />

Quoting docs

Using XML attributes from the support library

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

Upvotes: 1

Related Questions