Search code examples
androidandroid-activityandroid-menu

Option button not showing in action bar Android


I am trying to add action button , but they do not showing in action bar, they are display at button when click on hardware menu button . And the up button is also not working . i am sharing my code , tell me where i am wrong this is my manifest file :

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
       >
        <!-- Parent activity meta-data to support API level 7+ -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".secindActivity"
        android:label="Items"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

</application> 

I am adding the up button in second activity ,. it is showing but not working . And also trying to add action bar buttons in secondActivity . Menu file is as below :

      <?xml version="1.0" encoding="utf-8"?>

     <menu xmlns:Digicare="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
 <item
    android:id="@+id/item_back"
    android:icon="@drawable/home2"
    Digicare:showAsAction="ifRoom"
    android:title="Home">

</item>
</menu>

this is my java file of secondActivity :

it never comes in the if statement . i put log in it , so i get it that it never comes in if statement

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      if  ( item.getItemId() == R.id.home){
          Log.w("asdfasdfasdf","asdfasdf");
          Intent upIntent = NavUtils.getParentActivityIntent(this);
          NavUtils.navigateUpTo(this, upIntent);
      }
      return super.onOptionsItemSelected(item);
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater Mymenu = getMenuInflater();
    Mymenu.inflate(R.menu.item_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

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


    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
 }

These are problems . how to done this ????


Solution

  • First issue

    This is a duplicate of this question. I think that it doesn't show up because you have an options button on your device. See this post about modifying this behavior in reflection (however, I'm not sure I would recommend it, as people have expectations about their device's behavior).

    Second issue

    If I followed your code correctly, then the item id is item_back and not R.id.home...