How does one remove default Toolbar menu items and replace with different icons?

21,423

This is how I use the Toolbar. It is a standalone toolbar which give you more control over its functionality within the class.

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_height="56dp"
        android:layout_width="match_parent"
        android:elevation="5dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="@color/primary" />

    <FrameLayout
        android:layout_below="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- Main Content-->
    </FrameLayout>

</RelativeLayout>

Styles

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/secondary</item>
    <item name="colorAccent">@color/black</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionModeOverlay">true</item>
</style>

**MyActivity **

public class MyActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        // Set navigation icon
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_launcher));
        // Navigation onClickLister
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // finish(); // or your action here
            }
        });
        //Inflate menu
        toolbar.inflateMenu(R.menu.menu_my);
        // menu items 
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                    // Your action here
                    return true;
                }
                return false;
            }
        });
        // Clear toolbar icons 
        toolbar.getMenu().clear(); // I believe this is what you are looking for
        // Set title
        toolbar.setTitle("Title");
        //Set SubTitle
        toolbar.setSubtitle("Sub Title");

    }
}

Working Example of changing everything inside the toolbar

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="56dp"
        android:layout_width="match_parent"
        android:elevation="5dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="@color/primary" />

    <FrameLayout
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- Main Content-->

        <android.support.v7.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="Change Menu "
            android:gravity="center_vertical"
            android:id="@+id/swChaneMenu" />

    </FrameLayout>


</RelativeLayout>

Menu 1

<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">
    <item
        android:id="@+id/toast"
        android:title="Toast"
        android:orderInCategory="100"
        android:icon="@drawable/ic_done_black"
        app:showAsAction="always" />
</menu>

Menu 2

<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">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

Activity

public class TestingActionBar extends ActionBarActivity {
    SwitchCompat swChaneMenu;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testing_action_bar);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Toolbar Test 1");
        toolbar.inflateMenu(R.menu.menu_testing_action_bar);
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.nav_tint));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.toast) {
                    Toast.makeText(TestingActionBar.this, "Toasty", Toast.LENGTH_SHORT).show();
                    return true;
                }
                if (id == R.id.action_settings) {
                    Toast.makeText(TestingActionBar.this, "Settings", Toast.LENGTH_SHORT).show();
                    return true;
                }

                return false;
            }
        });
        swChaneMenu = (SwitchCompat) findViewById(R.id.swChaneMenu);
        swChaneMenu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                toolbar.setTitle("Toolbar Test 2");
                toolbar.getMenu().clear();
                toolbar.inflateMenu(R.menu.menu_settings);
                toolbar.setNavigationIcon(null);
                toolbar.setBackgroundColor(getResources().getColor(R.color.red));
            } else {
                toolbar.setTitle("Toolbar Test 1");
                toolbar.getMenu().clear();
                toolbar.inflateMenu(R.menu.menu_testing_action_bar);
                toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.nav_tint));
                toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
            }
            }
        });
    }
}

Switch Not Checked

enter image description here

Switch Checked

enter image description here

Share:
21,423
AJW
Author by

AJW

Trying to learn Java and Android...I appreciate all of the stackoverflow members who give generously of their time and expertise.

Updated on August 01, 2021

Comments

  • AJW
    AJW almost 3 years

    I am using a toolbar from v7.widget.Toolbar support library and v7.app.ActionBarActivity. When the onCreateOptionsMenu is inflated on the toolbar, I see a standard "Back" arrow on the leftmost side of the toolbar and standard "Overflow" dots on the rightmost portion of the toolbar.

    I would like to hide or disable these default icons and replace with another set of Android recommended icons. Any ideas on how to fix?

    MyActiviy.java:

    public class MyActivity extends ActionBarActivity {

    private Toolbar toolbar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home); {
    
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            if (toolbar != null) {
                setSupportActionBar(toolbar); 
                getSupportActionBar().setTitle("sc");
                getSupportActionBar().setHomeButtonEnabled(true); 
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
    ...
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    ...
    
  • AJW
    AJW over 9 years
    Ok so "always" (or "ifRoom") show the icons I am looking to show. However, the toolbar still shows the two default icons I am looking to hide: the leftmost arrow icon and the rightmost overflow icon. Any thoughts?
  • Eugene H
    Eugene H over 9 years
    Try this. // getMenuInflater().inflate(R.menu.menu_main, menu); inside the onCreateOptionsMenu and let me know if the action icons to the right are gone
  • AJW
    AJW over 9 years
    Ok the rightmost overflow icon is gone. The one one the left remains probably due to "getSupportActionBar().setHomeButtonEnabled(true); and "getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  • Eugene H
    Eugene H over 9 years
    Okay and remove your set as home statements
  • AJW
    AJW over 9 years
    Those statements are there for older API devices so I can't just remove them. Also, are you recommending I remove the "getMenuInflater() code? Any negatives to removing it?
  • Eugene H
    Eugene H over 9 years
    It just doesn't inflate the menu. You can reinflate it and change the icons inside the menu XML file
  • AJW
    AJW over 9 years
    sorry, I don't understand. If I reinflate it then I back to where I started with the two default icons that I don't want. I don't know how to hide or remove them. I am able to add the icons that I want to show.
  • Eugene H
    Eugene H over 9 years
    @user3796660 I just posted an update of a working example and customization of the toolbar
  • Eric Aya
    Eric Aya almost 3 years
    This looks more like a comment to this answer rather than a new answer. (More precisely, a response to a code comment in the code block in the answer)