user3171583
user3171583

Reputation: 91

How to change actionBar icon size?

The actionBar icon should like imageenter image description here

When the device resolution is 1920x1080 and android 4.2 above, the icon size looks very small: imageenter image description here

(android 4.1 is correct)

My problem seems like i-want-to-change-actionbar-icon-size.

The icon image size is 113x40, then I use getActionBar().getHeight() to get ActionBar height is 56.

menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item 
    android:id="@+id/menu_buyer"
    android:icon="@drawable/buyer_bts"
    android:showAsAction="ifRoom"/>
<item
    android:id="@+id/menu_exhibition_bt"
    android:icon="@drawable/exhibition_bt"
    android:showAsAction="ifRoom"/>
<item
    android:id="@+id/menu_oj"
    android:icon="@drawable/download_bt"
    android:showAsAction="ifRoom"/>

I tried following method but doesn't work:

  1. create bigger icon image (ex.170x60) then put into drawable-hdpi(xhdpi, xxhdpi...etc.) folder

  2. use menu.findItem(R.id.menu_buyer).setIcon(resizeImage(int resId, int w, int h)); to resize icon

  3. added a style < item name="android:actionBarSize" > 200dp < /item > in style.xml then set theme in Manifest file

Is there a way can show the icon correctly?

Upvotes: 9

Views: 13482

Answers (2)

Mark Yoon
Mark Yoon

Reputation: 380

Too late to answer but, I found the answer

It's quite simple. First, convert drawable into a bitmap, resize it and re-convert into drawable. Then get the item of the menu and set the icon as the new drawable.

Please add this re-size method on your activity as follow.

private Bitmap resizeBitmapImageFn(
        Bitmap bmpSource, int maxResolution){
    int iWidth = bmpSource.getWidth();
    int iHeight = bmpSource.getHeight();
    int newWidth = iWidth ;
    int newHeight = iHeight ;
    float rate = 0.0f;

    if(iWidth > iHeight ){
        if(maxResolution < iWidth ){
            rate = maxResolution / (float) iWidth ;
            newHeight = (int) (iHeight * rate);
            newWidth = maxResolution;
        }
    }else{
        if(maxResolution < iHeight ){
            rate = maxResolution / (float) iHeight ;
            newWidth = (int) (iWidth * rate);
            newHeight = maxResolution;
        }
    }

    return Bitmap.createScaledBitmap(
            bmpSource, newWidth, newHeight, true);
}

and add the work in the onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_navigation_drawer); //Converting drawable into bitmap
    Bitmap new_icon = resizeBitmapImageFn(icon, 50); //resizing the bitmap
    Drawable d = new BitmapDrawable(getResources(),new_icon); //Converting bitmap into drawable
    menu.getItem(0).setIcon(d); //choose the item number you want to set
    return true;
}

Upvotes: 9

Satyaki Mukherjee
Satyaki Mukherjee

Reputation: 2879

Please keep maintaining image with sizes and resolution . Like:

- drawable-ldpi (it need resolution or ppi is ~120)(keep small size image)
 - drawable-mdpi (it need resolution or ppi is ~160)(keep normal size image)
 - drawable-hdpi (it need resolution or ppi is ~240)(keep larger size image)
 - drawable-xhdpi (it need resolution or ppi is ~320)(keep xlarger size image)

You can also study this reference link http://developer.android.com/guide/practices/screens_support.html

Upvotes: 0

Related Questions