VasFou
VasFou

Reputation: 1669

The menu button of the action bar are being displayed only in the action Over flow

So i was using the appCombat action bar and the action menu button was being displayed in the action bar but I made an imigration to theme holo and theme material. Now the action menu buttons are being displayed only in the action overflow (three dots). Is it possible to remove action overflow and action menu button would be displayed in the action bar only?

Code from 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">
  <!-- search -->
  <item android:id="@+id/action_search"
        android:icon="@drawable/ic_search"
        android:title="search"
        android:visible="false"
        app:showAsAction="always"
      />
<!-- share -->
  <item android:id="@+id/action_share"
      android:icon="@drawable/ic_share"
      android:title="share"
      app:showAsAction="ifRoom"
      android:visible="false"/>
</menu>

Code from main activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // toggle nav drawer on selecting action bar app icon/title
    if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    menu.findItem(R.id.action_search).setVisible(false);
    menu.findItem(R.id.action_share).setVisible(false);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mExpandableListView);

    if(mDrawerLayout!=null && drawerOpen)
        menu.clear();

    return super.onPrepareOptionsMenu(menu);
}

code from a fragment that is connected with the main activity:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    //the button search is setting to visible
    menu.findItem(R.id.action_search).setVisible(true);
    menu.findItem(R.id.action_share).setVisible(false);
    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    // Handle action bar actions click
    switch (item.getItemId()) {
        case R.id.action_search:
                isSearchMode = true;
                //Log.d("billy","inside action_search");
                rightHeaderButtonClick();
                return true;
        case R.id.action_share:
            return false;
        default:
            return super.onOptionsItemSelected(item);
    }
}

code from a second activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    menu.findItem(R.id.action_search).setVisible(false);
    menu.findItem(R.id.action_share).setVisible(true);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar actions click
    switch (item.getItemId()) {
        case R.id.action_search:
            return false;
        case R.id.action_share:
            doShare();
        case android.R.id.home:
            ActivityDetails.this.finish();
        ActivityDetails.this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            return true;
        default:
            return super.onOptionsItemSelected(item);

    }
}

All my activities extends Activity , Thank you!!

Upvotes: 0

Views: 256

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006654

Now the action menu buttons are being displayed only in the action overflow (three dots)

That is because you are still using the app: namespace in your menu XML. That is only for appcompat-v7 or other libraries. For the native action bar, change app:showAsAction to android:showAsAction.

Upvotes: 1

Related Questions