Change ToolBar default icon on the left

12,679

Solution 1

Set as NavigationIcon:

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable)
toolbar.setNavigationIcon(drawable);
setSupportActionBar(toolbar);

Solution 2

Try this

toolbar.setNavigationIcon(R.drawable.your_icon);

Use it before setting support action bar. And don't call this

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Or at least do

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

Try it out. Some of this will work.

Share:
12,679
cubeb
Author by

cubeb

Updated on June 22, 2022

Comments

  • cubeb
    cubeb almost 2 years

    I am using a ToolBar at the top at present and want to replace the default back button with a home button. But when I add item, it always adds to the right. I don't see any layout_gravity options to choose either. Is there a way to do this?

    MainActivity

    Toolbar toolbar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_delete);
    
        toolbar = (Toolbar)findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("                Testing");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_icon_empty, menu);
        return true;
    }
    

    home_icon_empty.xml

    <item
        android:id="@+id/homepage"
        android:orderInCategory="100"
        android:title="HOME"
        android:icon="@drawable/home_icon"
        app:showAsAction="always"
        />
    
    <item
        android:id="@+id/homepage1"
        android:orderInCategory="200"
        android:title="HOME"
        android:icon="@drawable/home_icon"
        app:showAsAction="always"
        />
    

    activity_main.xml

    <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    

    app_bar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">
    
    </android.support.v7.widget.Toolbar>
    

    Current Toolbar output:

    enter image description here

    Expected Toolbar output:

    enter image description here

    Image after new edit:

    enter image description here