Mia Mia - 1 year ago 76
Android Question

Android - Image button size

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 -->

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"

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.

Answer Source

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.