I have 3 radiogroups, with 2 radiobuttons in each. Because I need them in vertical and horizontal position.
XML Layout:
<RadioGroup
android:id="#+id/radiogroup_Mapeamento1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_movimentada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/cardview_dark_background"
android:text="Rua Movimentada"
android:textColor="#color/cardview_dark_background" />
<RadioButton
android:id="#+id/rb_iluminacao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:buttonTint="#color/cardview_dark_background"
android:text="Má Iluminação"
android:textColor="#color/cardview_dark_background" />
</RadioGroup>
<RadioGroup
android:id="#+id/radiogroup_Mapeamento2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_comercio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/cardview_dark_background"
android:text="Comércio Aberto"
android:textColor="#color/cardview_dark_background" />
<RadioButton
android:id="#+id/rb_assedio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:buttonTint="#color/cardview_dark_background"
android:text="Assédio Recorrente"
android:textColor="#color/cardview_dark_background" />
</RadioGroup>
<RadioGroup
android:id="#+id/radiogroup_Mapeamento3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_policia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/cardview_dark_background"
android:text="Posto Policial"
android:textColor="#color/cardview_dark_background" />
<RadioButton
android:id="#+id/rb_porteiro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:buttonTint="#color/cardview_dark_background"
android:text="Porteiro/Segurança"
android:textColor="#color/cardview_dark_background" />
</RadioGroup>
How can I setchecked only 1 radiobutton? Or, is there a way the layout is only with 1 radiogroup?
Something like this:
rg1 = (RadioGroup)findViewById(R.id.radiogroup_Map1);
rg2 = (RadioGroup)findViewById(R.id.radiogroup_Map2);
rg3 = (RadioGroup)findViewById(R.id.radiogroup_Map3);
rg1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
op1 = rg1.getCheckedRadioButtonId();
switch (op1){
case R.id.rb_1:
rb_1.setChecked(true);
rb_2.setChecked(false);
rb_3.setChecked(false);
rb_4.setChecked(false);
rb_5.setChecked(false);
rb_6.setChecked(false);
break;
case R.id.rb_2:
rb_1.setChecked(false);
rb_2.setChecked(true);
rb_3.setChecked(false);
rb_4.setChecked(false);
rb_5.setChecked(false);
rb_6.setChecked(false);
break;
}
}
});
rg2.setOnClick.....
Instead of using multiple RadioGroup, you can use single RadioGroup with inner LinearLayout.
Here is an sample layout activity_radio_group.xml:
<?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:id="#+id/activity_radio_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<RadioButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<RadioButton
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<RadioButton
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 3"/>
<RadioButton
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 4"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<RadioButton
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 5"/>
<RadioButton
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 6"/>
</LinearLayout>
</LinearLayout>
</RadioGroup>
</RelativeLayout>
Change RadioButton states programmatically from your Activity:
public class RadioGroupActivity extends AppCompatActivity implements RadioButton.OnCheckedChangeListener {
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
RadioButton radioButton4;
RadioButton radioButton5;
RadioButton radioButton6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_group);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.button1);
radioButton2 = (RadioButton) findViewById(R.id.button2);
radioButton3 = (RadioButton) findViewById(R.id.button3);
radioButton4 = (RadioButton) findViewById(R.id.button4);
radioButton5 = (RadioButton) findViewById(R.id.button5);
radioButton6 = (RadioButton) findViewById(R.id.button6);
radioButton1.setOnCheckedChangeListener(this);
radioButton2.setOnCheckedChangeListener(this);
radioButton3.setOnCheckedChangeListener(this);
radioButton4.setOnCheckedChangeListener(this);
radioButton5.setOnCheckedChangeListener(this);
radioButton6.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean status) {
if (status)
{
switch (compoundButton.getId()) {
case R.id.button1:
radioButton2.setChecked(false); radioButton3.setChecked(false); radioButton4.setChecked(false);
radioButton5.setChecked(false); radioButton6.setChecked(false);
// Do something
Toast.makeText(getApplicationContext(), radioButton1.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
radioButton1.setChecked(false); radioButton3.setChecked(false); radioButton4.setChecked(false);
radioButton5.setChecked(false); radioButton6.setChecked(false);
// Do something
Toast.makeText(getApplicationContext(), radioButton2.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton4.setChecked(false);
radioButton5.setChecked(false); radioButton6.setChecked(false);
// Do something
Toast.makeText(getApplicationContext(), radioButton3.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton3.setChecked(false);
radioButton5.setChecked(false); radioButton6.setChecked(false);
// Do something
Toast.makeText(getApplicationContext(), radioButton4.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button5:
radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton3.setChecked(false);
radioButton4.setChecked(false); radioButton6.setChecked(false);
// Do something
Toast.makeText(getApplicationContext(), radioButton5.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button6:
radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton3.setChecked(false);
radioButton4.setChecked(false); radioButton5.setChecked(false);
// Do something
Toast.makeText(getApplicationContext(), radioButton6.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
OUTPUT:
Hope this will help~
Related
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.
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>
In Android My requirement is :-
I have multiple checkboxes and one button..after selection when You click the button it will display as text which boxes you have selected.
So I have activity_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/check"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay1">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apple"
android:tag="apple"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="banana"
android:tag="banana"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="water_milon"
android:tag="water_milon"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay2">
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="guava"
android:tag="guava"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="panir"
android:tag="panir"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chatni"
android:tag="chatni"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay3">
<CheckBox
android:id="#+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="college"
android:tag="college"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="#+id/checkBox8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="school"
android:tag="school"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="primary"
android:tag="primary"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
and the java class file is:--
public class MainActivity_test extends Activity {
CheckBox chk1,chk2,chk3,chk4,chk5,chk6,chk7,chk8,chk9;
Button btn;
TextView txt;
ArrayList<String> list ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
list = new ArrayList<String>();
chk1=(CheckBox)findViewById(R.id.checkBox1);
chk2=(CheckBox)findViewById(R.id.checkBox2);
chk3=(CheckBox)findViewById(R.id.checkBox3);
chk4=(CheckBox)findViewById(R.id.checkBox4);
chk5=(CheckBox)findViewById(R.id.checkBox5);
chk6=(CheckBox)findViewById(R.id.checkBox6);
chk7=(CheckBox)findViewById(R.id.checkBox7);
chk8=(CheckBox)findViewById(R.id.checkBox8);
chk9=(CheckBox)findViewById(R.id.checkBox9);
txt = (TextView)findViewById(R.id.textView1);
btn =(Button)findViewById(R.id.button1);
addListenerOnButton();
}
public void addListenerOnButton() {
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (String str : list) {
txt.setText("you have selected:--"+str);
}
}
});
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()) {
case R.id.checkBox1:
list.add(chk1.getTag().toString());
break;
case R.id.checkBox2:
list.add(chk2.getTag().toString());
break;
case R.id.checkBox3:
list.add(chk3.getTag().toString());
break;
case R.id.checkBox4:
list.add(chk4.getTag().toString());
break;
case R.id.checkBox5:
list.add(chk5.getTag().toString());
break;
case R.id.checkBox6:
list.add(chk6.getTag().toString());
break;
case R.id.checkBox7:
list.add(chk7.getTag().toString());
break;
case R.id.checkBox8:
list.add(chk8.getTag().toString());
break;
case R.id.checkBox9:
list.add(chk9.getTag().toString());
break;
}
}
}
But here the problem is when I click the button it is displaying only the last selected item..Not the all selected items...where is the problem??
Change txt.setText("you have selected:--"); in your for loop implement as
for (String str : list) {
txt.setText(txt.getText().toString() + " , " + str);
}
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 add radio buttons such that only one of them is selected at a time among 4 buttons and I want to place them as:
RadioButton1 RadioButton2
RadioButton3 RadioButton4
I am trying the following code but 1&2 forms a grp and 3&4 forms a different grp and there are two value selected a time. Can anyone check it and share the correct way to do it?
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/apple" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rdogrp_main"
android:orientation="vertical"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp1"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000"
/>
</RadioGroup>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp2"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000"
/>
</RadioGroup>
</RadioGroup>
</LinearLayout>
Ok I find a solution for you:
I know it is not the best solution but it works! :)
Just copy the xml file and activity code bellow:
Activity:
package com.example.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;
public class MainActivity extends Activity {
private static String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioButton radiobutton1 = (RadioButton) findViewById(R.id.RadioButton1);
final RadioButton radiobutton2 = (RadioButton) findViewById(R.id.RadioButton2);
final RadioButton radiobutton3 = (RadioButton) findViewById(R.id.RadioButton3);
final RadioButton radiobutton4 = (RadioButton) findViewById(R.id.RadioButton4);
radiobutton1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton1.setChecked(true);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton2.setChecked(true);
radiobutton1.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton3.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton3.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton4.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton4.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
return true;
}
});
}
}
activity_main.xml :
<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=".MainActivity" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FFDDDDDD" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
</TableLayout>
</RelativeLayout>
For that you need to create only one RadioGroup and add four different RadioButton.
And for adding UI refer this url : Manage Layout Inside a Horizontal Oriented Radiogroup .
I hope this will help you.
Try Like this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/rdogrp_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="India"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:layout_weight="0.57"
android:text="Austraila"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Srilanka "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.48"
android:text="SouthAfrica"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
Let me know if you solve the problem...