Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Bar not showing action view icons

I'm using the new Action Bar Support and all my action views are shown in overflow and not as icons in the bar. My app is for 7+ API.

HomeActivity:

public class HomeActivity extends ActionBarActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         ActionBar actionBar = getSupportActionBar();         actionBar.setTitle(R.string.app_name);         actionBar.setDisplayHomeAsUpEnabled(true);         actionBar.setHomeButtonEnabled(true);     }      @Override     public boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.home, menu);         return true;     } } 

home.xml

<item     android:id="@+id/action_settings"     android:orderInCategory="100"     android:showAsAction="never"     android:title="@string/action_settings"/> <item     android:id="@+id/action_browse"     android:orderInCategory="100"     android:showAsAction="never"     android:title="@string/title_activity_browse"/> <item     android:id="@+id/action_search"     android:actionViewClass="android.widget.SearchView"     android:icon="@android:drawable/ic_menu_search"     android:showAsAction="ifRoom|collapseActionView"     android:title="@string/text_search"/> <item     android:id="@+id/action_scan"     android:icon="@drawable/action_scan"     android:showAsAction="always"     android:title="@string/title_activity_browse"/> 

I'm deploying on Nexus 7 with 4.3 and on LGP500 with 2.3.3 and no icons. I've also added android:theme="@style/Theme.AppCompat.Light.DarkActionBar" on the manifest and my project is correctly referencing android-support-v7-appcompat as described in the official doc.

like image 721
Jumpa Avatar asked Aug 07 '13 09:08

Jumpa


People also ask

How do I add action to action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

Where is the action overflow menu icon?

The right-hand side of the action bar shows the actions. The action buttons (3) show the most important actions of your app. Actions that do not fit in the action bar are moved to the action overflow, and an overflow icon appears on the right.

How do I replace my action bar toolbar?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .


1 Answers

you have to define your menu resource files with also the attributes for the support library.

To implement the back support it reads them instead of the ones defined in older Android version.

<menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:yourapp="http://schemas.android.com/apk/res-auto" >  <item     android:id="@+id/action_settings"     android:orderInCategory="100"     android:showAsAction="never"     yourapp:showAsAction="never"     android:title="@string/action_settings"/> <item     android:id="@+id/action_browse"     android:orderInCategory="100"     android:showAsAction="never"     yourapp:showAsAction="never"     android:title="@string/title_activity_browse"/> <item     android:id="@+id/action_search"     android:actionViewClass="android.support.v7.widget.SearchView"     android:icon="@android:drawable/ic_menu_search"     android:showAsAction="ifRoom|collapseActionView"     yourapp:showAsAction="ifRoom|collapseActionView"     yourapp:actionViewClass="android.support.v7.widget.SearchView"     android:title="@string/text_search"/> <item     android:id="@+id/action_scan"     android:icon="@drawable/action_scan"     android:showAsAction="always"     yourapp:showAsAction="always"     android:title="@string/title_activity_browse"/>  </menu> 

NB remeber that for the SearchView class changed. it's now used the one from the support library so you also have to update your code in the onCreateOptionsMenu()

EDIT: here is a pretty good tutorial on how to migrate from ActionBarSherlok to AppCompat

like image 156
Mario Lenci Avatar answered Sep 29 '22 07:09

Mario Lenci