Ammar
Ammar

Reputation: 1233

Icon is not showing up on the action bar

For some reason the icons are not showing up on the action bar even though everything seems correct to me. If I press the menu button on my phone I do get the menu items as text which is fine. However, I have a search icon that I want to display on the action bar but it doesn't appear for some reason. Here is a snippet of the code that seems relevant.

OnCreate

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

        ActionBar actionBar = getActionBar();
        actionBar.show();
        actionBar.setSubtitle("subtitle");
        actionBar.setTitle("title");
    }

Option Menu

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Menu 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="com.ammar.customlistview1.app.MainActivity" >
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="2"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Action Search"
        android:orderInCategory="1"
        app:showAsAction="always"  />

</menu>

Manifest

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

Let me know if you need any further details, thanks in advance.

Upvotes: 0

Views: 1161

Answers (2)

Blo
Blo

Reputation: 11988

You actually use the current ActionBar without any Support Library to add item to it, which allow you to get the ActionBar on API level 11 or higher and create a menu (first snippet code from the Docs).

Therefore, the showAsAction attribute does not need to be with a custom prefix like you do with the Support Library (third snippet code from the Docs). Just call the Android default library as follows:

android:showAsAction="always"  

Also you have to remove xmlns:context, xmlns:app and xmlns:tools attributes, they are useless in your menu file.

Upvotes: 2

Ando Masahashi
Ando Masahashi

Reputation: 3122

HI just use bellow menu xml file and try

tell me if you got any error

<menu xmlns:android="http://schemas.android.com/apk/res/android"

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

<item android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Action Search"
    android:orderInCategory="1"
    android:showAsAction="always"  />
</menu>

Upvotes: 0

Related Questions