-1

in this program I use radiobuttons. Currently I've some problems with the button number 2 (button2), in fact when it is pressed nothing happens!

When I press button2 it should show the name of each radio button in "TextView" (while button1 is used to clear choosing radio buttons). Can anyone please help me or give me any kind of tips? Thanks.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;    

public class RadiobuttonActivity extends Activity {     

    Button      button1;
    Button      button2;
    TextView    textView1;
    RadioButton r1;
    RadioButton r2;
    RadioButton r3;     

    RadioGroup  radioGroup1;     

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        textView1 = (TextView) findViewById(R.id.textView1);
        radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
        r1 = (RadioButton) findViewById(R.id.r1);
        r2 = (RadioButton) findViewById(R.id.r2);
        r3 = (RadioButton) findViewById(R.id.r3);     

        button1.setOnClickListener(new OnClickListener() {     

            @Override
            public void onClick(View arg0) {
                radioGroup1.clearCheck();
                textView1.setText("AllUnChecked");     
            }
        });     

        button2.setOnClickListener(new OnClickListener() {     

            @Override
            public void onClick(View arg0) {
                int selectedid = radioGroup1.getCheckedRadioButtonId();
                switch (selectedid) {
                    case 0:
                        textView1.setText("Red");
                        break;
                    case 1:
                        textView1.setText("Green");
                        break;
                    case 2:
                        textView1.setText("Black");
                        break;
                }
            }
        });     
    }
}

this is the main.xml :

    <?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" >

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >



            <RadioButton
                android:id="@+id/r1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Red" />



            <RadioButton
                android:id="@+id/r2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Green" />



            <RadioButton
                android:id="@+id/r3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Black" />

        </RadioGroup>


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AllUnchecked" />


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Witch radio button is ckecked?" />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text View"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

and this is main.xml file:

<?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" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >



        <RadioButton
            android:id="@+id/r1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Red" />



        <RadioButton
            android:id="@+id/r2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Green" />



        <RadioButton
            android:id="@+id/r3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Black" />

    </RadioGroup>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AllUnchecked" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Witch radio button is ckecked?" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text View"
        android:textAppearance="?android:attr/textAppearanceLarge" />
Alien_x
  • 27
  • 4

1 Answers1

1

As explained in this SO post

Instead of use:

int selectedid = radioGroup1.getCheckedRadioButtonId();

Use this:

 int selectedid = radioGroup1.indexOfChild(findViewById(radioGroup1.getCheckedRadioButtonId()));
Community
  • 1
  • 1
Luigi Cerone
  • 362
  • 1
  • 6
  • 18