3

SOLUTION: It was a problem with the image search icon. Android did not find the image for the search action , I had not added them in all the res-folders , it started to show after that...

I am trying to add the action bar to a app and I am following the basic app tutorial shown in the Google Developer Website

I have written the following code.

MainActivity

public class MainActivity extends ActionBarActivity {

    public void openSearch(){

        System.out.println("TEST SEARCH");

    }
    public void openSettings(){

        System.out.println("TEST SETTINGS");

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @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.main_activity_actions, 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_search:
                openSearch();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

res/menu/main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

AndroidManifest.xml

<uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sample.actionbarsample.MainActivity"
        android:label="@string/app_name" 
        android:theme="@style/Theme.AppCompat" 
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

But instead of a Action bar like this

enter image description here

I get A options menu. Which shows up when I press the menu button while running the app.

Screen Shot on Emulator running 4.2.2

What is that I am missing out from the tutorial, Or is there something wrong in the code?

EDIT

After doing what beworker asked me I got this result, still missing the search box.

enter image description here

Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58

2 Answers2

8

Nothing wrong either with you code or with the tutorial. It's all about configuration of your emulator. You need to configure your emulator in the way, that it has software buttons. Switch to "Device Definition" tab and edit device that is uses "Software" buttons as in the picture below.

enter image description here

Alternatively you can simply uncheck "Keyboard / Hardware keyboard present" property in ADV configuration (see picture below) and restart your emulator.

enter image description here

Android 4.0 emulates old-style menu, if a hardware menu button is present. That is why all menu actions will go in there. If you disable hardware menu in emulator, then actions will go to action bar, as expected.

Please note, that on phones with hardware menu buttons like Samsung G2, G3 etc. menu popup will still appear and you cannot change this in your code. Android will decide what is better for user experience on each and every device.

Update

Another note. If you use compatibility library, make sure you use xmlns:yourapp="http://schemas.android.com/apk/res-auto" and yourapp:showAsAction="always" as described below.

However, if your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix.

<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_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="always"  />
    ...
</menu>
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
1

What is that I am missing out from the tutorial, Or is there something wrong in the code?

Yes, there is a difference. Try to remove the sample word from your project.

Actually your project name is occupying all the space and in search menu item you have asked the system to provide the space if there is any room/space available which is not due to the long project name(may be activity or whatever) in your case.

So either change your project name to something small or use "always" like this:

  <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="always" />
Mohit
  • 2,940
  • 2
  • 24
  • 32