Can't retrieve state of radio button - java

I wan retrieve radio buttons, created in XML. It's OK. But when, I check if this radio button is checked, Android Studio tell me than the radio button retrieve is empty.
XML :
<RadioGroup
android:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/RadioGroup_MODE">
<!-- android:buttonTint="#color/BouttonsRadio"-->
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_J"
android:id="#+id/BouttonRADIO_JOUR"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_N"
android:id="#+id/BouttonRADIO_NUIT"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
</RadioGroup>
JAVA :
final RadioButton radiobutton_mode_j = (RadioButton) findViewById(R.id.BouttonRADIO_JOUR);
final RadioButton radiobutton_mode_n = (RadioButton) findViewById(R.id.BouttonRADIO_NUIT);
I use this code for check if it's this radiobutton which is checked :
radiobutton_mode_j.isChecked()
radiobutton_mode_n.isChecked()

Try below code:
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
Edit
Here is the full code as per your request:
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:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/RadioGroup_MODE">
<!-- android:buttonTint="#color/BouttonsRadio"-->
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_J"
android:id="#+id/BouttonRADIO_JOUR"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
<RadioButton
android:layout_width="137dp"
android:layout_height="wrap_content"
android:text="#string/Mode_N"
android:id="#+id/BouttonRADIO_NUIT"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />
</RadioGroup>
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
Activity:
private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.RadioGroup_MODE);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}

Try this:
RadioGroup rg = (RadioGroup) v.findViewById(R.id.RadioGroup_MODE);
if(rg.getCheckedRadioButtonId() != -1){
//one of the Radiobuttons is checked
}else {
//none of the Radiobuttons is checked
}

Related

getCheckedRadioButtonId() returns -1 even after the radio button is checked

I'm trying to get text from selected radiobutton. this code was working fine yesterday but now it's returning null and I don't understand why or what's suddenly causing it.
Renaming variable, rewriting the code, re define variable, re initializing it does nothing to solve this.
the XML
<RadioGroup
android:id="#+id/radioGroupMurid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtKelaminMurid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jenis Kelamin"
android:textSize="20sp" />
<RadioButton
android:id="#+id/radioMuridLaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Laki-Laki" />
<RadioButton
android:id="#+id/radioMuridPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Perempuan" />
</LinearLayout>
</RadioGroup>
Activity
private RadioGroup radioGroup;
protected void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(R.layout.murid_detail_activity);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupMurid);
getRadioChecked();
}
public void getRadioChecked(){
int sexMurid = radioGroup.getCheckedRadioButtonId();
RadioButton rbSelected = (RadioButton) findViewById(sexMurid);
jenisKelamin = rbSelected.getText().toString();
}
This is really fine yesterday, I just don't know what's causing it? can anyone help?
This occurs when RadioButton is not a direct child of RadioGroup. In your code the direct child of RadioGroup is LinearLayout. Try with the RadioGroup tag inside of LinearLayout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtKelaminMurid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jenis Kelamin"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/radioGroupMurid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/radioMuridLaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Laki-Laki" />
<RadioButton
android:id="#+id/radioMuridPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Perempuan" />
</RadioGroup>
</LinearLayout>
The return type of radioGroup.getCheckedRadioButtonId() (as in the docs) is an int. An int cannot be null, so I think you should rely on -1.
Edit: If you're not sure how an Android component works, just check the source code :) For the RadioGroup, see for example: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/RadioGroup.java#RadioGroup.clearCheck%28%29
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroupMurid);
RadioButton rbSelected = (RadioButton) findViewById(sexMurid);
RadioButton radioMuridPerempuan = (RadioButton) findViewById(radioMuridPerempuan);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.sexMurid) {
jenisKelamin = rbSelected.getText().toString();
else if (checkedId == R.id.radioMuridPerempuan) {
jenisKelamin = radioMuridPerempuan.getText().toString();
}
}
});

Button View is not Visible even after setting its visibility true

I am making a quiz app hence I wanted my MenuButton and NextButton view Invisible at the beginning but after satisfying a particular condition I wanted the View to be visible again hence I used findViewById(R.id.MenuButton).setVisibility(View.VISIBLE); and same for the NextButton but still these view were Invisible.What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="#+id/Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="TextView"
android:gravity="center"
android:textSize="25dp"
android:textColor="#000000"/>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:text="RadioButton1"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="54dp"
android:layout_below="#+id/Question"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="On_RadioButton1_Click"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="horizontal"
android:layout_marginTop="55dp"
android:layout_below="#+id/radioButton4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="#+id/MenuButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:text="menu" />
<Button
android:id="#+id/NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginLeft="125dp"
android:text="next" />
</LinearLayout>
</RelativeLayout>
//Java file
private int Question_no=0;
private Boolean Boolean_Var=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
String[] Question_Array = getResources().getStringArray(R.array.Question1);
TextView Questions = (TextView) findViewById(R.id.Question);
Questions.setText(Question_Array[Question_no]);
String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1);
RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1);
Radio_Button1.setText(Radio_Button1_Array[Question_no]);
findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE);
findViewById(R.id.NextButton).setVisibility(View.INVISIBLE);
}
public void On_RadioButton1_Click(View view)
{
if(Boolean_Var==false)
{
String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
String CorrectAns = CorrectAns_Array[Question_no];
String[] Answer_Array = getResources().getStringArray(R.array.Option_1);
String Answer = Answer_Array[Question_no];
if(Answer.equals(CorrectAns))
{
RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1);
Right_Ans.setTextColor(Color.GREEN);
AnswerSubmitted();
}
else
{
RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1);
Wrong_Ans.setTextColor(Color.RED);
GreenTick();
AnswerSubmitted();
}
}
Boolean_Var=true;
}
public void AnswerSubmitted()
{
findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
findViewById(R.id.NextButton).setVisibility(View.VISIBLE);
}
You should get a reference to your buttons and call setVisibility on the reference. What you're currently doing is setting visibility INVISIBLE on one reference and setting it VISIBLE on another.
Button nextButton;
//in onCreate
nextButton = (Button)findViewById(R.id.nextButton);
nextButton.setVisibility(View.INVISIBLE);
//in AnswerSubmitted
nextButton.setVisibility(View.VISIBLE);
I think you should tell android that you want to execute that method when your radiobutton is clicked. by adding :
Radio_Button1.setOnClickListener(new OnClickListener (){
public void onClick(View v) {
On_RadioButton1_Click(v);
}
});
So you can get that method executed everytime you click.
Add it at the end of OnCreate

radiobutton in android doesn't work

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" />
As explained in this SO post
Instead of use:
int selectedid = radioGroup1.getCheckedRadioButtonId();
Use this:
int selectedid = radioGroup1.indexOfChild(findViewById(radioGroup1.getCheckedRadioButtonId()));

Android Radio Button Group with EditText imbedded

my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below.
The three choices are:
1. [] Male
2. [] Female
3. [] Custom: (self-described identity)
However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.
So the following code is from my XML page, with some elements blocked out by "####".
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="#+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_male"
android:checked="true" />
<RadioButton android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
As you can see, I have tried to use a LinearLayout to isolate the custom option.
However, there are unintended and undesired side effects.
1. The custom option can be selected in addition to the other 2 predefined genders.
2. The custom option cannot be selected on its own.
In the actual Java file for the activity, I have the following code:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.
Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.
EDIT: Here's a picture of what I want it to look like:
(http://i68.tinypic.com/ao2oow.png)
Unfortunately I need 10 reputation to post images. :(
Mohit's answer gives the EditText on a different line than the custom input.
(http://i63.tinypic.com/68u88x.png)
Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.
Because selectedId will not be 1,2 or 3....debug it you will get value..
The custom option cannot be selected on its own.
Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..
On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...
I dont no what is your task but here is how can get all three RadioButton working and get custom text too....
xml...
UPDATE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ex.MainActivity" >
<RadioGroup
android:id="#+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/male_female_custom_choice"
android:layout_toRightOf="#+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="#+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="#+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java file..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
you can also set visibility of LinearLayout so that it only visible when custom in checked....
Hope it help..
put radio group in RelativeLayout
set third radiobutton text as empty/null
add EditText as layout_alignParentBottom="true"
now programmatically call EditText's onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b)
thirdRadio.setCheched(true);
}
});

passing string from RadioButton android:text="#string/blue" to the next activity in android

i have
<RadioGroup
android:id="#+id/sign_up_select_colour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
<RadioButton
android:id="#+id/rdbtn_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/red" />
<RadioButton
android:id="#+id/rdbtn_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/blue" />
</RadioGroup>
i want to send the string blue to the next activity the same way i did with EditText here (with your help earlier);
EditText editTextSignUpUserName = (EditText) findViewById(R.id.sign_up_user_name);
String signUpUserName = editTextSignUpUserName.getText().toString();
intent.putExtra("username", signUpUserName);
i've tried this;
RadioButton selectedRadioButton = (RadioButton) findViewById(R.id.sign_up_select_colour);
String signUpColour = selectedRadioButton.getCheckedRadioButtonText().toString();
intent.putExtra("colour", signUpColour);
please and thanks.
Ok, you're almost there. But a RadioGroup is not a RadioButton. Try this:
RadioGroup colourGroup = (RadioGroup) findViewById(R.id.sign_up_select_colour);
RadioButton button = (RadioButton) colourGroup.findViewById(colourGroup.getCheckedRadioButtonId());
String signUpColour = button.getText().toString();
intent.putExtra("gender", signUpColour);

Categories