Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suspect that it was a conscious decision by the Android developers to never display a single menu item's text and icon on a narrow action bar. But if you really want to do so, you can use <a href="http://developer.android.com/guide/topics/resources/menu-resource.html" rel="noreferrer">android:actionLayout</a> in your menu.xml file. The <a href="http://developer.android.com/guide/topics/ui/actionbar.html#ActionView" rel="noreferrer">Android ActionBar documentation</a> has a slightly better explanation.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+id/menu_foo" android:title="@string/menu_foo" android:icon="@drawable/ic_menu_foo" android:showAsAction="always" android:actionLayout="@layout/action_button_foo" /&gt; &lt;/menu&gt; </code></pre> <p>Then create your <code>action_button_foo.xml</code> layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="14dp" android:paddingBottom="14dp" android:gravity="center" android:text="@string/menu_foo" android:drawableLeft="@drawable/ic_menu_foo" android:background="@drawable/bg_btn_action_bar" android:clickable="true" /&gt; </code></pre> <p>and use a selector for its background <code>bg_btn_action_bar.xml</code>, so it changes color when you tap it:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/bg_action_bar_pressed" /&gt; &lt;item android:drawable="@color/transparent" /&gt; &lt;/selector&gt; </code></pre> <p>Now you'll need to make your custom view handle click events. In your Activity, I like to do this, so that I can handle the click in <code>onOptionsItemSelected</code> along with all my other, non-custom items.</p> <pre><code>@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_menu, menu); final MenuItem item = menu.findItem(R.id.menu_foo); item.getActionView().setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onOptionsItemSelected(item); } }); return super.onCreateOptionsMenu(menu); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload