Yamna
Yamna

Reputation: 52

Display the content of checked RadioButton by default, android

In my Activity, i have three RadioButtons set horizontally. For each radioButton , i have set some checkboxes and a button,that are different for each radio button.

 <RadioGroup
                    android:id="@+id/selection"
            android:layout_width="fill_parent"
            android:weightSum="3"
            android:layout_height="fill_parent" android:orientation="horizontal">
        <RadioButton

                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Sensation"
                android:id="@+id/sens" android:checked="true"/>
        <RadioButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Location"
                  android:layout_weight="1"
                android:id="@+id/loc" android:checked="false"/>
        <RadioButton
                android:layout_width="fill_parent"
                  android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Modalation"
                android:id="@+id/mod" android:checked="false"/>
    </RadioGroup>

Now, i want my first radiobutton to be checked by default and show its related content, when this activity starts. But i'm unable to show the related content by default. It only show up when i select a different radio button and re select it. I guess its because of "OnCheckChangeListener" , Do we have some other event of radioButton through which the content of the checked radioButton can display by default?

Upvotes: 0

Views: 1491

Answers (1)

Stephen Lin
Stephen Lin

Reputation: 4912

What do you use to show related content? TextView?

I think you should set a default content to the view and whenever radio button is checked, you can update content of the view through onRadioButtonClicked function.

EDIT:

1. Set default content to textView and make one of three radio buttons checked as default. I think you have done this.

2. Set onclick event for each radio button like this:

...
<RadioButton android:id="@+id/radio_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="radio_btn_1"
        android:onClick="onRadioButtonClicked"/>
...

3. Put radio button check event handler in your activity, and use setText() to update textview accordingly:

...
public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    TextView textView = (TextView) findViewById(R.id.textView);

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_btn_1:
            if (checked)
                textView.setText('your content for btn 1');
            break;
        case R.id.radio_btn_2:
            if (checked)
                textView.setText('your content for btn 2');
            break;
    }
}
...

Upvotes: 0

Related Questions