Dark Light
Dark Light

Reputation: 973

Adding icon on Action Bar instead of three dots

I'm practicing follow this guide http://developer.android.com/training/basics/actionbar/adding-buttons.html but it doesn't display icon "search" on action bar, it just have "Search" string after I clicked on "three dots". How to remove three dots and showing search icon on Action Bar?

My app like this but there is no search icon, but there is a "Search" string in "three dots"

enter image description here

Upvotes: 1

Views: 8377

Answers (3)

user961376
user961376

Reputation:

What you have to do is setHasOptionsMenu method in true and override onCreateOptionsMenu and onOptionsItemSelected if you are using fragments.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.events_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearchView();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

This is the menu xml file that displays a search option on the ActionBar:

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/search"
    app:showAsAction="always" />

Upvotes: 1

gsanskar
gsanskar

Reputation: 669

You can remove the 3-dots menu by just adding below attribute android:showAsAction="never" to it:

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"/>

And also use app:showAsAction="always" in all other menu items.

Hope this helps!

Upvotes: -2

iGio90
iGio90

Reputation: 3291

add this to your menu item:

    android:icon="@drawable/my_icon"
    app:showAsAction="ifRoom"

Upvotes: 5

Related Questions