Android - Image button size

19,722

If your image is bigger than your button, you will need to use something like android:scaleType or setMaxHeight() to have it resize to fit.

Also, I recommend that you have all images in your <selector> be the same size, at least until you get things working the way you wish.

Share:
19,722
Genevieve
Author by

Genevieve

Biomedical Engineer

Updated on June 04, 2022

Comments

  • Genevieve
    Genevieve almost 2 years

    My imagebutton code wont respond to "fill_parent" and "wrap_content". Instead, it just shows the middle of the image (because it is a pretty big image). I tried setting specific values for the image but it still didn't work! Can somebody help me?

    My button.xml in the res/drawable folder:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">\
    <item android:state_pressed="true" 
    android:drawable="@drawable/largishbutton" /> <!-- pressed -->
    <item android:state_focused="true" 
    android:drawable="@drawable/largerbutton2" /> <!-- focused -->
    <item android:drawable="@drawable/largerbutton" /> <!-- default -->
    </selector>
    

    My main.xml in the res/layout folder:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:background="@android:color/white">
     <ImageButton
     android:src="@drawable/button"
     android:layout_height="fill_parent"
     android:layout_width="wrap_content"
      /> 
     </RelativeLayout>
    

    I tried using a LinearLayout, but the app wouldnt run, and I tried replacing the imagebutton with an imageview, but then the button didn't work.