Skip to content
Advertisement

Regarding the checked state of a Radio button

I currently have a layout where if the user presses the option 1 button a new layout is set where the radio button to its right is checked. Then the “Checker” button gets a new onClick value. How can I change this within the java code so that the option1clicked method sets the first radio buttons checked value to “true”.

Also, after I’ve done that, how to i make it so that when I click the button the system checks what radiobutton is checked?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Checker"  
        android:onClicked="nobuttonpressed"    />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    >

    <Button
        android:text="Option 1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="option1clicked"    />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
             android:checked="false"
        android:layout_alignParentRight="true"  />

</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:text="Option 2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="option2clicked"    />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
             android:checked="false"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

</LinearLayout>

Advertisement

Answer

Try this,

In the Button’s onClickListener() use radioButton1.setChecked(true).

eg:

button1.setOnClickListener(new onClickListener(){

      public void onClick(View v){

      radioButton1.setChecked(true);

      }

 }
Advertisement