Android Toolbar - Change height and width of Navigation Icon Programmatically
Asked Answered
N

3

10

enter image description hereI want to change height and width of Navigation Icon(in black circle in screen shot) in Android Toolbar programmatically. Is there any way to do so. This is not toolbar logo. I can't update toolbar theme in Styles xml as I want it to be dynamic. Please help.

Nichollenicholls answered 5/7, 2016 at 10:51 Comment(5)
Did you try to change it? Post your code so that we can help you moreNumismatics
It's changing through xml but I can't update xml dynamically. I want it to be done dynamically.Nichollenicholls
@anuja_k which icon you want to change, can you provide any screenshot, then i will help you?Kesselring
Added a reference image @YasinKaçmazNichollenicholls
@anuja_k updated my answer both custom toolbar view, and originalKesselring
P
22

I did this way :

toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null){
    Drawable drawable= getResources().getDrawable(R.drawable.ic_sync_white_36dp);
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    Drawable newdrawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 250, 250, true));
    newdrawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(newdrawable);

}

You can use calculate-dp-from-pixels-in-android-programmatically and converting-pixels-to-dp when creating scaled bitmap .

My Toolbar:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

And screenshot :

enter image description here

Also another way is using custom layout in Toolbar like that:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" >
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:id="@+id/custom_toolbar_layout"
        android:layout_height="wrap_content">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:tint="@android:color/holo_purple"
        android:id="@+id/imageView"
        android:gravity="center_vertical"
        android:src="@drawable/ic_clear_black_36dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="My Custom Toolbar"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:layout_gravity="center_horizontal" />
    </LinearLayout>
</android.support.v7.widget.Toolbar>

If you want to access any View in Toolbar see @Aleksandar Stefanović's answer. And you can take a look at Material design guidelines for creating custom Toolbar

Then screenshot :

enter image description here

Icons from : Material Icons

Photoflood answered 5/7, 2016 at 11:45 Comment(1)
@anuja_k good, i posted both ways and maybe anyone will use this in futureKesselring
R
2

try this in AppCompatActivity

if(getSupportActionBar()!=null){
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  getSupportActionBar().setHomeButtonEnabled(true);
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
    getSupportFragmentManager().popBackStack();
  }});

in xml

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:background="#434343"
        android:elevation="4dp"
        android:minHeight="90dp"
        app:theme="@style/tool_bar_style" />

in styles.xml

    <style name="tool_bar_style">
    <item name="colorControlNormal">@color/white</item>
    <item name="android:textColor">@color/text_color_main</item>
    <item name="android:textColorPrimary">@color/text_color_main</item>
    <item name="android:textColorSecondary">@color/text_color_main</item>
    <item name="actionMenuTextColor">@color/text_color_main</item>
    <item name="android:textSize">@dimen/text_size_default</item>
    <item name="android:colorBackground">@color/toolbar_bg_color</item>
    <item name="android:listPreferredItemHeightSmall">@dimen/button_default_height</item>

</style>
<style name="Toolbar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">">
    <item name="android:textSize">@dimen/text_size_big</item>
</style>

in dimens.xml

    <dimen name="text_size_big">14sp</dimen>
    <dimen name="text_size_default">22sp</dimen>
    <dimen name="button_default_height">90dp</dimen>

in colors

    <color name="text_color_main">@color/clean_white</color>
    <color name="toolbar_bg_color">#434343</color>
Rosenberry answered 8/5, 2019 at 7:10 Comment(0)
K
1

If you're using an appcompat Toolbar, it actually behaves like a regular ViewGroup. So, you can access the Views inside of it, easily, like with any other ViewGroup.

For example, if you have an ImageView inside you toolbar, you can simply access it by:

        getSupportActionBar().getCustomView().findViewById(R.id.imageView)

From there, you just resize it as a regular ImageView.

Koodoo answered 5/7, 2016 at 11:53 Comment(1)
Yes I am using AppCompat Toolbar. is there any pre-declared id for navigation Icon?Nichollenicholls

© 2022 - 2024 — McMap. All rights reserved.