1

I have set a custom icon for a radio button. I have created its png so that it is correctly centered on Android 4. By "centered" I mean with respect to other radio buttons that do have their stantard icons. However, the custom icon shows off-center on Android 2.3. Please see image. The radio button with the modified icon is the second.

How can I set a left padding to the icon drawable (or whatever other solution) so as to center it horizontally with respect to the standard radio buttons?

enter image description here

The relevant parts of the code are as follows. I'm adding the radio buttons to a radioGroup in Java code, not in XML. The radioGroup has already been initialized. radioButton is an array which contains the radio buttons. i is a loop variable (I ommit the loop). The radio buttons with the custom icon are those that satisfy the if. Those buttons that don't satisfy that condition are left with the standard icon.

    radioButton[i].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height_pixels));
    radioButton[i].setText(newNameArray[i]);
    radioButton[i].setTextColor(MainActivity.colorTextNormal);
    radioButton[i].setOnClickListener(new Button.OnClickListener() {
        public void onClick(View view) {search(false);}
    });
    if (!containsSearchTermMark(baseStringArray[i])) {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.radio_nosearch_icon, typedValue, true); // Resolve the XML: get the drawable it represents 
        radioButton[i].setButtonDrawable(typedValue.resourceId); // 32 dp icon, so that it takes up the same space as the original radio icon
    }
    radioGroup.addView(radioButton[i]);
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147

2 Answers2

0

You can take your custom drawable in xml as below

android:button="@drawable/radio_button_custom"

Or post your xml layout to better understand it.

EDIT:

I am at work right now, but will try to see what is wrong in your code. Till the time, you can take a look at this answer, you might get some idea.

Community
  • 1
  • 1
  • Check the EDIT part in my answer. –  Sep 03 '13 at 11:08
  • I have solved it by redrawing the icons (I had to do it anyway). Android 2.3 seems to require strange size values for the radio buttton icons (see my answer). Thanks for your time! – Luis Mendo Sep 03 '13 at 23:33
0

I ended up redrawing all icons (which is what I wanted to avoid).

In case anyone is interested, appropriate sizes of radio button icons are as follows.

  • For Android 4.x: 32x32 pixels for MDPI, 48x48 for HDPI, 64x64 for XHDPI.
  • For Android 2.3: 38x48 for MDPI, 56x72 for HDPI, 76x96 for XHDPI.

These are the sizes of the radio button icons in the Android "res" folder of Eclipse.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147