Change ToolBar default icon on the left
Asked Answered
V

4

8

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

Verbal answered 15/12, 2015 at 15:10 Comment(0)
S
15

Set as NavigationIcon:

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable)
toolbar.setNavigationIcon(drawable);
setSupportActionBar(toolbar);
Steerageway answered 15/12, 2015 at 15:19 Comment(6)
It works but the image size is not properly resized unlike when I am adding items. Anyway around this? Added image for reference.Verbal
It's because your drawable has a lower resolution (in size) that you expect. Menu itens in material is recommended to be 24dp. You can resize it programmatically, or simple put some ImageView inside the <Toolbar> in XMLSteerageway
I see your image. Your icon is too big for the resolution. You need to create a lower size icon. You can do that using this tool: romannurik.github.io/AndroidAssetStudio/icons-actionbar.htmlSteerageway
Did the link helps? Create a new actionbar icon and replace, it should workSteerageway
So I should resize it to 48dp x 48dp?Verbal
It's the recommended size for material design... it's up to you =)Steerageway
U
2

You can also:

mToolbar.setLogo( yourLogoID );

or you can:

mToolbar.setBackground( ContextCompat.getDrawable( yourContext, urBackgroundID );

where

mToolbar = yourToolbar;
mToolbar = findViewById(R.id.your_toolbar);
// or, if your toolbar get setted by a superclass
mToolbar = (Toolbar) getSupportActionBar();

your style should be: Blablabla.NoActionBar then in xml layout include your toolbar

If you are using a Drawer to show left Menu when Left Swiped or Home Button Clicked then you should use one of above methods (If you don't want to superclass all DrawerLayout lul)

bb

Unspotted answered 3/9, 2019 at 12:52 Comment(0)
S
1

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.

Sugared answered 15/12, 2015 at 15:17 Comment(1)
It works but the image size is not properly resized unlike when I am adding items. Anyway around this? Added image for reference.Verbal
A
0

Add your drawable asset with size as per Material design guide and call below method in Activity onCreate

private void setUpToolBar() {
            setSupportActionBar(toolbar);
            //Set Home screen icon
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.home_screen_icon);

            //Enable Home icon
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            //Toolbar title
            getSupportActionBar().setTitle(getString(R.string.app_name));

           //Set toolbar as action bar
            setSupportActionBar(toolbar);
        }
Assr answered 11/5, 2018 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.