John Casey
John Casey

Reputation: 27

Android: Changing the contents of a Text View using a radio button

So, I am trying to write code that will show that Radio Button clicks register. Ideally, when a radio button is chosen, the contents of a TextView will change. To start, I have a radio button for 'North'. When North is checked, the contents of the TextView will become 'North'. I know there are action listeners involved, but I am not familiar with Java. This will surely pop my Java cherry. That being said, the code I have written is not working. Can anyone tell me if I am on the right track, or offer some suggestions? Note, this is not for a class assignment. This is for a very open ended class project that I am working on with another person.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public void onRadioButtonClicked(View v){
  TextView text = (TextView)findViewById(R.id.text);

    RadioButton N = (RadioButton)  findViewById(R.id.north);

    //evaluates 'checked' value of radio button
    boolean  checked = ((RadioButton) v).isChecked();

 if(N.isChecked () ){

     text.setText("N");
 }
}

}

Upvotes: 1

Views: 4641

Answers (4)

John Casey
John Casey

Reputation: 27

Okay, so making 'onCheckedChange' the onClick event seemed to do the trick. Thanks for all the help people.

Upvotes: 0

Suhyeon Lee
Suhyeon Lee

Reputation: 569

To use RadioButton properly you'd better group a bunch of RadioButtons into a set, named RadioGroup.

<RadioGroup
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/rg1_rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="North" />

    <RadioButton
        android:id="@+id/rg1_rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="South" />

    <RadioButton
        android:id="@+id/rg1_rb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Whatever" />
</RadioGroup>


The critical part is that you have to set unique android:id for each RadioButtons, or they won't work!

Next, find RadioButtons from your XML.

RadioButton rb1, rb2, rb3;

rb1 = (RadioButton) findViewById(R.id.rg1_rb1);
rb2 = (RadioButton) findViewById(R.id.rg1_rb2);
rb3 = (RadioButton) findViewById(R.id.rg1_rb3);

Finally, prepare a RadioButton.OnClickListener class instance and attach it to RadioButtons.

View.OnClickListener optionOnClickListener
            = new View.OnClickListener() {

    public void onClick(View v) {
        TextView tv = (TextView) findViewById(R.id.textview);
        String str = null;

        // you can simply copy the string of clicked button.            
        str = ((RadioButton)v).getText().toString();
        tv.setText(str);

        // to go further with check state you can manually check each radiobutton and find which one is checked.
        if(rb1.isChecked()) {
            // do something
        }
        if(rb2.isChecked()) {
            // do something
        }
        if(rb3.isChecked()) {
            // do something
        }
    }
};

rb1.setOnClickListener(optionOnClickListener);
rb2.setOnClickListener(optionOnClickListener);
rb3.setOnClickListener(optionOnClickListener);

// check rb1 by default, if you want.
rb1.setChecked(true);


ADDED:

I'm sorry but I couldn't understand the edited version of my answer, since calling setOnClickListener() inside the View.OnClickLister.OnClick() was somewhat weird to me.
So I rolled back to my original answer.

Upvotes: 3

Shahadat Hossain Shaki
Shahadat Hossain Shaki

Reputation: 806

Check your xml file. radio button must be inside the radio group for work perfectly.

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

        <RadioButton
            android:id="@+id/n"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="I am a Blood Donor" />

        <RadioButton
            android:id="@+id/s"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />
    </RadioGroup>

then change in your java file

mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (i == R.id.s) {
                //Your action
            } else if (i == R.id.n) {
                //Your action
            }
        }
    });

it will work.. :) :)

Upvotes: 0

Linh
Linh

Reputation: 60913

Try

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.your_radio_group_id);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.north ;
                // set text North for your textview here
                break;
            case R.id.another_radio_button_id:
                // do something
                break;
        }
    }
});

Upvotes: 2

Related Questions