Setting StateDrawableList to ImageButton in code have different results from setting via xml?

189 Views Asked by At

I have next result:

result

Right buttons:

android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:scaleType="centerInside"
android:src="@drawable/xml_menu_button" 

xml_menu_button:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="false" android:drawable="@drawable/bottom_button_menu"></item>
    <item android:state_pressed="true" android:drawable="@drawable/bottom_button_menu_pressed"></item>
</selector>

Second button have similar xml as config.

Left buttons added to the panel dynamically. So I set its resources via code:

ImageButton button = new ImageButton(getActivity());
button.setBackgroundColor(Color.TRANSPARENT);
button.setAdjustViewBounds(true);
button.setScaleType(ScaleType.CENTER_INSIDE);

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, 
                new BitmapDrawable(getResources(), PATH_TO_BUTTON_PRESSED_IMAGE));
states.addState(new int[] { },
                new BitmapDrawable(getResources(), UPATH_TO_BUTTON_IMAGE));

button.setImageDrawable(states);

Panel is custom ViewGroup. With layout bounds turned on, I can see, that measuring and layout stage passed as I want. But there is a difference between image sizes, as you can see. All buttons have the same size 70x58 px. In layout buttons have size 77x77. What am doing wrong?

0

There are 0 best solutions below