Why am I getting two RadioButtons selected?
I want only one RadioButton selected at a time.
Here is the layout:
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
and the code:
public void radioButtonClicked(View view) {
// Check that the button is now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.radio_female:
if (checked)
userGender = "female";
break;
case R.id.radio_male:
if (checked)
userGender = "male";
break;
}
}
You need to put the RadioButton inside the RadioGroup. Change your XML like this:
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1">
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
</RadioGroup>
Close RadioGroup tag
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
</RadioGroup>
Related
I'm an Android beginner and I have a little problem.
I have 2 radioButtons titled "Yes" and "No", 4 editTexts which are disabled and a button titled "Reset All".
Now, when I select the "No" radioButton, editTexts 1 and 2 become enabled and 1 gains focus. This is the desired behaviour. But When I select "Reset All" and again repeat the procedure, only editText 2 gets enabled and the "No" radioButton is still unchecked.
Following is my code:
MainActivity.java
package com.example.chris.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
EditText e1;
EditText e2;
EditText e3;
EditText e4;
RadioButton yes;
RadioButton no;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText)findViewById(R.id.e1);
e2 = (EditText)findViewById(R.id.e2);
e3 = (EditText)findViewById(R.id.e3);
e4 = (EditText)findViewById(R.id.e4);
yes = (RadioButton)findViewById(R.id.yes);
no = (RadioButton)findViewById(R.id.no);
}
public void onClickreset(View v){
yes.setChecked(false);
no.setChecked(false);
e1.setEnabled(false);
e2.setEnabled(false);
e3.setEnabled(false);
e1.setText("");
e2.setText("");
e3.setText("");
e4.setText("");
};
public void onRadioButtonClicked(View v)
{
boolean checked = ((RadioButton) v).isChecked();
switch(v.getId()){
case R.id.yes:
if(checked)
e1.setEnabled(false);
e2.setEnabled(false);
e3.setEnabled(true);
e3.requestFocus();
break;
case R.id.no:
if(checked)
e1.setEnabled(true);
e2.setEnabled(true);
e2.requestFocus();
e2.clearFocus();
e3.setEnabled(false);
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.chris.myapplication.MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*">
<!-- Row 1 Starts From Here -->
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:checked="false"
android:onClick="onRadioButtonClicked"
android:text="yes" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:checked="false"
android:onClick="onRadioButtonClicked"
android:text="no" />
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="1:"
android:textColor="#000000" />
<EditText
android:id="#+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="2:"
android:textColor="#000000" />
<EditText
android:id="#+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="3:"
android:textColor="#000000" />
<EditText
android:id="#+id/e3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="4:"
android:textColor="#000000" />
<EditText
android:id="#+id/e4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="30dp" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="3"
android:gravity="center"
android:onClick="onClickreset"
android:text="Reset"
android:textAllCaps="false"
android:textColor="#000000"
android:textStyle="normal" />
</TableLayout>
</LinearLayout>
It's better to use RadioGroup.OnCheckedChangeListener, first of all, get rid of android:onClick attributes and assign an id to RadioGroup in XML:
<RadioGroup
android:id="#+id/yes_no_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:checked="false"
android:text="yes" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:checked="false"
android:text="no" />
</RadioGroup>
Then make your Activity implements RadioGroup.OnCheckedChangeListener and move there code from onRadioButtonClicked(View v) method:
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
//Some activity code
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.yes: //Not needed to
e1.setEnabled(false); //which RadioButton clicked
e2.setEnabled(false);
e3.setEnabled(true);
e3.requestFocus();
break;
case R.id.no:
e1.setEnabled(true);
e2.setEnabled(true);
e2.requestFocus();
e2.clearFocus();
e3.setEnabled(false);
break;
}
}
}
Then, assign OnCheckedChangeListener with RadioGroup:
yesNoGroup = findViewById(R.id.radio_group);
yesNoGroup.setOnCheckedChangeListener(this);
And change:
yes.setChecked(false);
no.setChecked(false);
To
yesNoGroup.clearCheck();
I would like to have my android app have a radio button checked using a switch statement. I have confirmed that the string being passed matches one of the cases and still nothing is checked. What am I doing incorrectly?
radioGroup = (RadioGroup) v.findViewById(R.id.gender);
radioGroup2 = (RadioGroup) v.findViewById(R.id.year);
fre = (RadioButton) v.findViewById(R.id.freshmen);
soph = (RadioButton) v.findViewById(R.id.sophomore);
jun = (RadioButton) v.findViewById(R.id.junior);
sen = (RadioButton) v.findViewById(R.id.senior);
male = (RadioButton) v.findViewById(R.id.radioButton);
female = (RadioButton) v.findViewById(R.id.radioButton2);
lgbtq = (RadioButton) v.findViewById(R.id.radioButton3);
switch (global.getGender()) {
case "Male": male.setChecked(true);
break;
case "Female": female.setChecked(true);
break;
case "LGBTQ": lgbtq.setChecked(true);
break;
}
switch (global.getYear()) {
case "Fresh": fre.setChecked(true);
break;
case "Soph": soph.setChecked(true);
break;
case "Junior": jun.setChecked(true);
break;
case "Senior": sen.setChecked(true);
break;
}
and in XML
<RadioGroup
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#+id/bio"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fresh"
android:id="#+id/freshmen"
android:layout_below="#+id/editText8"
android:buttonTint="#00F2EA"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soph "
android:buttonTint="#009B40"
android:id="#+id/sophomore"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Junior"
android:buttonTint="#FF0000"
android:id="#+id/junior"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Senior"
android:buttonTint="#000"
android:id="#+id/senior"
/>
</RadioGroup>
<RadioGroup
android:id="#+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#+id/year"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male "
android:id="#+id/radioButton"
android:layout_below="#+id/editText8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:buttonTint="#076EB9"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:buttonTint="#FF00AA"
android:id="#+id/radioButton2"
android:layout_alignTop="#+id/radioButton"
android:layout_toRightOf="#+id/radioButton"
android:layout_toEndOf="#+id/radioButton" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LGBTQ"
android:buttonTint="#bf00ff"
android:id="#+id/radioButton3"
android:layout_alignTop="#+id/radioButton2"
android:layout_toRightOf="#+id/radioButton2"
android:layout_toEndOf="#+id/radioButton2" />
</RadioGroup>
My code above was correct. The issue was that the snippet was that I did not understand the architecture of a fragment class.
I have five BadioButton in RadioGroup. Now what I want that if I receive "NDS" from Web Service then its show "NDS" RadioButton Checked or if I receive "DS" then its show "DS" checked. how can I achieve this?
Below is my Radio Group XML Code:
<RadioGroup android:id="#+id/rdoCnsumerCatgory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<RadioButton android:id="#+id/rdoDS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="DS"
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoNDS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="NDS"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoSip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="SIP"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoMip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="20dp"
android:text="MIP"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoMl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="ML"
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
</RadioGroup>
Try This
By if-else method
String mValue="";
RadioButton rdoMl,rdoMip,rdoSip,rdoNDS,rdoDS;
if(mValue.equalsIgnoreCase("NDS"))
{
rdoNDS.setChecked(true);
}
else if(mValue.equalsIgnoreCase("DS"))
{
rdoDS.setChecked(true);
}
else if(mValue.equalsIgnoreCase("MIP"))
{
rdoMip.setChecked(true);
}
else if(mValue.equalsIgnoreCase("SIP"))
{
rdoSip.setChecked(true);
}
else if(mValue.equalsIgnoreCase("ML"))
{
rdoMl.setChecked(true);
}
By switch method
switch(mValue) {
case "NDS":
rdoNDS.setChecked(true);
break;
case "DS":
rdoDS.setChecked(true);
break;
case "MIP":
rdoMip.setChecked(true);
break;
case "SIP":
rdoSip.setChecked(true);
break;
case "ML":
rdoMl.setChecked(true);
break;
// etc...
}
I am having problem in having radioButtons in multiple Rows
this is my xml
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_one0Id"
android:textSize="13sp"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:text="5%"
android:id="#+id/radio_one5Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="10%"
android:textSize="13sp"
android:layout_weight="1"
android:id="#+id/radio_one10Id"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="20%"
android:layout_weight="1"
android:textSize="13sp"
android:onClick="oneRadioButtonClicked"
android:id="#+id/radio_one20Id"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="35%"
android:id="#+id/radio_one35Id"
android:textSize="13sp"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="50%"
android:textSize="13sp"
android:id="#+id/radio_one50Id"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
</RadioGroup>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="65%"
android:textSize="13sp"
android:id="#+id/radio_one65Id"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="75%"
android:textSize="13sp"
android:layout_weight="1"
android:id="#+id/radio_one75Id"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="85%"
android:textSize="13sp"
android:id="#+id/radio_one85Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:text="95%"
android:id="#+id/radio_one95Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="100%"
android:id="#+id/radio_one100Id"
android:textSize="13sp"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
</RadioGroup>
</RadioGroup>
this is code
public void oneRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_one0Id:
if (checked)
one = "0";
break;
case R.id.radio_one5Id:
if (checked)
one = "5";
break;
case R.id.radio_one10Id:
if (checked)
one = "10";
break;
case R.id.radio_one20Id:
if (checked)
one = "20";
break;
case R.id.radio_one35Id:
if (checked)
one = "35";
break;
case R.id.radio_one50Id:
if (checked)
one = "50";
break;
case R.id.radio_one65Id:
if (checked)
one = "65";
break;
case R.id.radio_one75Id:
if (checked)
one = "75";
break;
case R.id.radio_one85Id:
if (checked)
one = "85";
break;
case R.id.radio_one95Id:
if (checked)
one = "95";
break;
case R.id.radio_one100Id:
if (checked)
one = "100";
break;
default:
System.out.println("default");
}
}
this will look like
it will select both the buttons in 2 rows, i want it to select only one button in those rows, thanks for any help
Put one radiogroup with vertical orientation and add two LinearLayouts:
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<RadioButton
android:id="#+id/radio_one0Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one5Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="5%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one10Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="10%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one20Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="20%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one35Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="35%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one50Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="50%"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio_one65Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="65%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one75Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="75%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one85Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="85%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one95Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="95%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one100Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="100%"
android:textSize="13sp" />
</LinearLayout>
</RadioGroup>
From searching around, there doesn't appear to be a way of doing it,
This means you will have to implement this layout behaviour manually. Two possible options are:
Create a copy of RadioGroup to extend a different layout, or at least allow you control it dynamically.
Implement your own custom layout to replace RadioGroup that extends a layout of your choice, and implements OnClickListener. There's a good example How to group a 3x3 grid of radio buttons?.
I was researching this a lot, and I finally found a solution. If you want to have something like this:
First you need to download/create a new class like this: link, since RadioGroup uses LinearLayout by default. Now you have a RadioGroup that uses RelativeLayout. The only thing left is to separate radio buttons by percentage (just like with weightSum, it's just that you don't have weightSum in RelativeLayout, only LinearLayout) by using a neat little hack:
<rs.cdl.attendance.UI.RelativeRadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<View
android:id="#+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<RadioButton
android:id="#+id/start_radio_button"
android:layout_alignRight="#id/strut"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<RadioButton
android:id="#+id/finish_radio_button"
android:layout_alignLeft="#id/strut"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finish" />
<RadioButton
android:id="#+id/pause_radio_button"
android:layout_alignRight="#id/strut"
android:layout_alignParentLeft="true"
android:layout_below="#id/start_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
<RadioButton
android:id="#+id/continue_radio_button"
android:layout_alignLeft="#id/strut"
android:layout_alignParentRight="true"
android:layout_below="#id/finish_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue" />
</rs.cdl.attendance.UI.RelativeRadioGroup>
This worked for me.
The first line (NameRadioGroupe2.clearCheck();) clear the other Radiogroup, and the second line add a checkmark in the button that was checked
public void oneRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_one0Id: {
one = "0";
NameRadioGroupe2.clearCheck();
NameRadioGroupe1.check(view.getId());
break;
}
break;
case R.id.radio_one5Id: {
NameRadioGroupe2.clearCheck();
NameRadioGroupe1.check(view.getId());
one = "5";
break;
}
.
.
.
.
.
case R.id.radio_one65Id: {
NameRadioGroupe1.clearCheck();
NameRadioGroupe2.check(view.getId());
one = "65";
break;
}
case R.id.radio_one75Id: {
NameRadioGroupe1.clearCheck();
NameRadioGroupe2.check(view.getId());
one = "75";
break;
}
.
.
.
.
.
I was trying to work out the same thing.
What I ended up doing was adding multiple RadioGroups within their own LinearLayouts. When selecting a radiobutton from the other RadioGroup to the one that is currently selected to ensure that the first RadioGroup buttons were no longer selected I added .Checked = false to the radiobuttons .Click function. And then because I experienced bugs at first where sometimes the newly clicked radio button wouldnt check, I added a .Checked = true to the actual radio button.
My XML
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:padding="10dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
style="#style/radios"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rad1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="1"
android:checked="true" />
<RadioButton
android:id="#+id/rad2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="2" />
<RadioButton
android:id="#+id/rad3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="3" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:padding="10dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
style="#style/radios"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rad4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="4" />
<RadioButton
android:id="#+id/rad5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="5" />
</RadioGroup>
</LinearLayout>
And then my C#
var radio1 = FindViewById<RadioButton>(Resource.Id.rad1);
var radio2 = FindViewById<RadioButton>(Resource.Id.rad2);
var radio3 = FindViewById<RadioButton>(Resource.Id.rad3);
var radio4 = FindViewById<RadioButton>(Resource.Id.rad4);
var radio5 = FindViewById<RadioButton>(Resource.Id.rad5);
radio1.Click += delegate
{
radio2.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio1.Checked = true;
};
radio2.Click += delegate
{
radio1.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio2.Checked = true;
};
radio3.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio3.Checked = true;
};
radio4.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio3.Checked = false;
radio5.Checked = false;
radio4.Checked = true;
};
radio5.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = true;
};
Primative, but it worked for me.
One simple way to make multiple lines of radio buttons is to use MultiLineRadioGroup library. It supports as many rows and columns as you like.
The use is simple:
In your project's build.gradle file add:
allprojects {
repositories {
...
maven {
url "https://jitpack.io"
}
...
}
}
In your Application's or Module's build.gradle file add:
dependencies {
...
compile 'com.github.Gavras:MultiLineRadioGroup:v1.0.0.6'
...
}
You can use a string array resource in the xml to create your view like that:
In the layout's XML add:
<com.whygraphics.multilineradiogroup.MultiLineRadioGroup xmlns:multi_line_radio_group="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_activity_multi_line_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
multi_line_radio_group:default_button="button_2"
multi_line_radio_group:max_in_row="3"
multi_line_radio_group:radio_buttons="#array/radio_buttons" />
and in arrays.xml add:
<string-array name="radio_buttons">
<item>button_1</item>
<item>button_2</item>
<item>button_3</item>
<item>button_4</item>
<item>button_5</item>
</string-array>
or add them programmatically:
mMultiLineRadioGroup.addButtons("button to add 1", "button to add 2", "button to add 3");
I want to make a new activity, like to confirm what radio-button is checked.
I have 2 radio-groups
Each group has 10 radio-buttons at least
I want to take whatever radio-button is checked value to the next activity
and for sure just one radio-button can be selected from every radio-group
Here is my xml code and I want the java code
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Where Are You" />
<RadioGroup
android:id="#+id/radioGroup0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
<RadioButton
android:id="#+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Where Do You Want To Go" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<RadioButton
android:id="#+id/radio12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
<RadioButton
android:id="#+id/radio15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
</ScrollView>
I have deleted some of the radio-buttons so it doesn't get long
Please note I am a beginner.
First, bind the RadioGroup:
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
Then get the position of the button that was checked:
int checked = rg.getCheckedRadioButtonId();
Then create the intent of your next activity:
Intent intent = new Intent(this,nextActivity.class);
Then put the information you want to pass on the intent:
intent.putExtra("checked",checked);
Then start the next activity:
startActivity(intent);
On the next activity you recover that info like this:
Intent intent = getIntent();
int checked = intent.getIntExtra("checked");
try that;
public void onCreate(Bundle savedInstanceState) {
enter code here
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
String selectedRadioValue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
String selectedRadioValue2 =((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
Button btn = (Button) findViewById(R.id.buttonName);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//enter code here for your control and
Intent intent = new Intent(getApplicationContext(), your.class);
intent.putExtra("radioGroup1Selected", selectedRadioValue);
intent.putExtra("radioGroup2Selected", selectedRadioValue2);
startActivity(intent);
}
});
}
get another activity :
Intent intent = getIntent();
String selectedRadioValue = intent.getStringExtra("selectedRadioValue");
String selectedRadioValue2 = intent.getStringExtra("selectedRadioValue2");