joshas
joshas

Reputation: 1474

How to set RadioButton properties in code?

I'm using this code to dynamically create new RadioButton in Android:

RadioButton radioButton = new RadioButton(getBaseContext());
radioButton.setText("test text");
radioGroup.addView(radioButton);

I need to set RadioButton properties "android:button", "android:minWidth" and "android:textAppearance" in code (not in xml), before adding radioButton to radio group.

Upvotes: 1

Views: 693

Answers (2)

Mr. Black
Mr. Black

Reputation: 12122

Yes, you can use this.

<RadioButton android:id="@id/button_one"
    android:minWidth="40dip"
    android:minHeight="30dip"
    android:text="test"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:button="@null"
    android:gravity="center"
    android:background="@drawable/segment_radio_left" 
    android:textColor="@color/radio_colors" />

Upvotes: 0

jcxavier
jcxavier

Reputation: 2232

You should be able to use setButtonDrawable(), setMinWidth() and setTextAppearance(). Refer to http://developer.android.com/reference/android/widget/CompoundButton.html and http://developer.android.com/reference/android/widget/TextView.html.

Upvotes: 1

Related Questions