cesar
cesar

Reputation: 9074

Make Android Radio Button's `button` Drawable shrink to fit bounds and keep aspect ratio

I'm trying to make a custom radio button. I have a <selector> configured as follows:

<selector xmlns...>
  <item android:drawable="@drawable/image" android:state_checked="true" />
  <item android:drawable="@drawable/image" android:state_checked="false" />
</selector>

This is just a sample right now, so I'm using the same image for simplicity, but I plan on using a compound image that consists of @drawable/image and a slightly bigger rectangle later on. The image is larger than my desired radio button size, so my problem is rather than scaling the image down so that the image retains its aspect ratio in the <RadioButton />, the image is just getting cropped (in half, to be precise). Since this isn't an image view, I've no idea how to make it scale properly.

I've set layout_width & layout_height to wrap_content. I plan on encoding absolute dp values later on in the resource files, but I'd like to figure out how to get the image to scale properly to fit the specified dimensions first. One alternative I see see is to use GIMP to resize my image before-hand, but that doesn't seem too adaptable in the long run, especially if I need to make this work properly on multiple screen densities.

Just now, I tried using an <inset />, hoping that would make it resize, but that didn't work out.

Any suggestions?

Upvotes: 1

Views: 2707

Answers (1)

Marc
Marc

Reputation: 1445

Creating a dummy background worked for me [tested on API8 and 16]:

<RadioButton
 android:id="@+id/stKayaBkRad"
 android:background="@drawable/goban_rad_bkg"
 android:button="@drawable/rad_goban_kaya_sel"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

Apparently wrap_content does wrap the background, which is just a completely transparent png with the same size as the drawable.

Upvotes: 3

Related Questions