Android: Where to find the RadioButton Drawable?
Asked Answered
H

3

7

Ok, I am trying to create a custom view called CheckedRelativeLayout.

It's purpose is the same as a CheckedTextView, to be able to use it in a list of items you want selected or in a Spinner.

It's all working fine now, I extended RelativeLayout and implemented Checkable interface.

However, I am stuck on a quite simple problem: Where can I find the Drawable that CheckedTextView and RadioButton use?

I looked at the sourcecode of both, and they seem to use com.android.internal.R. Well... that's internal stuff. So I can't access it.

Any way to get these Drawables or solve the problem somehow?

Hedelman answered 4/5, 2010 at 17:45 Comment(0)
I
13

look under SDK folder /platforms/android-2.0/data/res/ you can access them by either android.R.drawable ( if public ) or need to copy them as drawable to your project

Ibson answered 4/5, 2010 at 18:44 Comment(3)
That's great. I found them. Also found the corresponding R.drawable.btn_radio. But that's just one. There are btn_radio_off.pngfiles etc. How can I access those? Is there something like statemanagement?Hedelman
it's a stateful drawable. It changes the state based on the state of the View. If you look at it in SDK folder it's defined as XML. By looking to the XML you can see which actual png are being used ( they are in drawable-hdpi/mdpi folders. Stateful drawable are simply awesome and you can do same stuff with colors too.Ibson
To use "btn_radio" for current theme you can use "?android:attr/listChoiceIndicatorSingle" as your drawable.Sorilda
H
5

For sake of completeness:

Here some code pieces that show how you I got it working with above accepted answer.

 //Image Setup (Once when creating this view)
 ImageView indicator = (ImageView) findViewById(R.id.RadioStatusImage);
 indicator.setImageDrawable(getResources().getDrawable(android.R.drawable.btn_radio));

 //State Change (In some other method)
  android.R.attr.state_checked
  if (isChecked)
  {
     indicator.setImageState(android.R.attr.state_checked, false);
  }
  else
  {
     indicator.setImageState(View.ENABLED_STATE_SET, false);
  }
  invalidate();
Hedelman answered 4/5, 2010 at 19:32 Comment(0)
E
2

To use the new default animated radio button drawable, the correct answer is in the comment of @sidon:

?android:attr/listChoiceIndicatorSingle

Using this in the custom position relative to text:

<RadioButton
    ...
    android:gravity="center_horizontal|bottom"
    android:drawableTop="?android:attr/listChoiceIndicatorSingle"
    android:button="@null"
    />
Emerick answered 24/8, 2019 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.