yawers
yawers

Reputation: 223

Adding Action to the Action Bar Android

I am trying to add a search icon to the action bar, and it shows up in the xml preview in Android Studio, but when I run my app it shows up in the overflow menu instead of an icon on the action bar. What am I doing wrong?

menu_launch.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LaunchActivity">

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

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

I tried setting app:showAsAction to "always" but it did not work.

In my LaunchActivity.java:

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

My LaunchActivity.java extends Activity, not ActionBarActivity, I don't know if that has anything to do with it.

Thank you for any help, this is my first app.

Upvotes: 1

Views: 227

Answers (5)

1234567
1234567

Reputation: 2485

android:showAsAction="never"

android:showAsAction="ifRoom"

these are the issues just change it to

android:showAsAction="always"

try changing app:showAsAction to android:showAsAction

Upvotes: 0

Ajay P. Prajapati
Ajay P. Prajapati

Reputation: 2023

Try to change your theme. It depends on your theme too. in styles.xml file

     <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

let me know if that worked or not, because everything else seems working in your code. You need to also put this reference to manifest.xml file. for your main activity

<application
android:theme="@style/AppTheme" />

according to the documentation..

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

This declares that the Search action should appear as an action button when room is available in the action bar, but the Settings action should always appear in the overflow. (By default, all actions appear in the overflow, but it's good practice to explicitly declare your design intentions for each action.)

So try to rotate your screen, your app will display it, whenever room is available for the icon to get fit in. You should set showAsAction="always".

Upvotes: 0

tachyonflux
tachyonflux

Reputation: 20221

For an ActionBarActivity, you would use app:showAsAction, since it would be an AppCompat property.

For an Activity, you should use android:showAsAction since that is the only property that it reads.

If you want it to always appear as an icon, set the property to "always".

Upvotes: 1

Yousef Zakher
Yousef Zakher

Reputation: 1544

Why you are sitting app:showAsAction="never" in action_settings try this

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".LaunchActivity">

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

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

or

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".LaunchActivity">

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

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


 </menu>

Upvotes: 0

gareoke
gareoke

Reputation: 746

Have your Activity extend ActionBarActivity. The menu_launch.xml you have will work.

Upvotes: 0

Related Questions