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?
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]);