I want to change actionbar icon size
Asked Answered
H

5

24

I tried to use this code

<item
        android:id="@+id/male_button"
        android:layout_width="46dp"
        android:layout_height="56dp"
        android:layout_gravity="right"
        android:icon="@drawable/icon_male"
        android:showAsAction="always"
        android:title="i"/>
    <item
        android:id="@+id/female_button"
        android:layout_width="46dp"
        android:layout_height="56dp"
        android:layout_gravity="right"
        android:icon="@drawable/icon_female"
        android:showAsAction="always"
        android:title="i"/>

and I changed android:layout_width="46dp" to android:layout_width="30dp" but I still have the same size the desired image is

enter image description here

and I now have this

enter image description here

How can I change the icons to be as the first picture ?

Hornbeam answered 8/3, 2013 at 17:9 Comment(4)
Please refer to [this answer][1]. android:minWidth meets your needs. [1]: https://mcmap.net/q/369862/-is-there-a-way-to-reduce-the-spacing-between-the-action-item-icons-on-action-barFootloose
did you find any solution for this problem?Paternity
Did someone found anything on this topic?Party
Will it work for moving the icon from high res to low resolution folders? for example: from xhdpi to sdpiMoore
D
24

To reduce the space between actions icons, in your styles.xml you need to create a actionButtonStyle and referencing it your main theme (ex: "AppTheme").

1st step (put the two sentences):

<style name="AppTheme" parent="Theme.AppCompat.Light">
  ...
  <item name="android:actionButtonStyle">@style/myActionButtonStyle</item>
  <item name="actionButtonStyle">@style/myActionButtonStyle</item>
  ...
</style>

2nd step (the parameter "android:width" is the great secret):

<style name="myActionButtonStyle" parent="Widget.AppCompat.ActionButton">
    <item name="android:minWidth">30dp</item>
    <item name="android:maxWidth">48dp</item>
    <item name="android:width">38dp</item>
</style>

enjoy it

Dapper answered 18/12, 2015 at 18:31 Comment(2)
this did not change the size of my iconPenzance
This worked perfectly for my action bar icons. Thanks!Overmodest
D
2
private String[] tabnames;
final int[] ICONS = new int[] {
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,}
tabnames = getResources().getStringArray(R.array.tabnames);

Those are the arrays made up here and in res/values/strings of your drawable names and your tab names

actionBar = getActionBar();
    for (int i=0; i < tabnames.length; i++)
    {

Every time we get a tabname we add an icon for it

        Drawable dr = getResources().getDrawable(ICONS[i]);
        Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
        Drawable d = new BitmapDrawable(getResources(), 
        Bitmap.createScaledBitmap(bitmap, 50, 50, true));

Then we change get icon from array and make it a drawable with whatever size we want and add it to the actionbar.

    actionBar.addTab(actionBar.newTab().setText(tabnames[i])
                             .setIcon(d)
                             .setTabListener(this));
    }
Disembogue answered 23/12, 2015 at 14:55 Comment(0)
A
0

I have solved by moving png files to drawable-xhdpi folder.

Assisi answered 28/4, 2016 at 20:38 Comment(0)
P
0

You may find my solution a bit odd. But it actually worked for me that's why I'm sharing it.

Instead of changing the style attribute just increase the padding of the image using some editing software (like Photoshop or something).

By doing so Actionbar/Toolbar icon size reduces according to your requirement.

Pouched answered 2/8, 2018 at 6:29 Comment(1)
(This post does not seem to provide a quality answer to the question. Please either edit your answer, or just post it as a comment to the question).Catiline
C
0

Here's how I set an icon to 24x24px:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(menuResId, menu)
    val actionSearch = menu.findItem(R.id.actionSearch)
    val searchBitmap = ContextCompat.getDrawable(this, R.drawable.ic_search)?.toBitmap()
    searchBitmap?.let {
        actionSearch?.icon = BitmapDrawable(resources, Bitmap.createScaledBitmap(it, 24, 24, true))
    }
    return true
}

Since I'm setting the icon here, I don't need to set it in menu_option.xml, but of course it's better to set it there too just in case. Also the dimensions here are in pixels so be sure to convert them to dips if you use that.

Celebrant answered 6/4, 2023 at 5:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.