How can I retrieve radio group of the radio button that is checked? I have two radio groups with some radio buttons and I would like to know which group they belong to.
void populateSpinner(#ArrayRes int radioId, RadioGroup radioGroup) {
adapter = ArrayAdapter.createFromResource(SecondActivity.this,
radioId, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_districts.setAdapter(adapter);
}
public void onRadioButtonClick(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.radio_1:
if (checked)
populateSpinner(R.array.radio_1_array, <RADIO BUTTON GROUP>);
break;
// so on
How can I get the Radio Button Group?
You can set tag for each of your RadioGroup in your xml code , and when you click any RadioButton, you can get the Parent RadioGroup's Tag and easily you can identify the parent RadioGroup
Your activity_layout XML code
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="FirstRadioGroup"
android:id="#+id/firstRG">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:id="#+id/firstRB"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:id="#+id/secondRB"/>
</RadioGroup>
JAVA Activity Code
RadioGroup firstRG = (RadioGroup) findViewById(R.id.firstRG);
firstRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i){
case R.id.firstRB:
Toast.makeText(MainActivity.this, "Parent RadioGroup is --"+radioGroup.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.secondRB:
break;
}
}
});
Like this when you will check the first RadioButton it will show you the Prent RadioGroup Tag Value
Related
I have crated 4 radiobutton in recyclerview but when i check one it checked but when i checked another one radiobutton then first one can not be unchecked
holder.cb_votes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i < pollItems.size(); i++) {
if (isChecked) {
holder.cb_votes.setChecked(true);
} else {
holder.cb_votes.setChecked(false);
}
}
}
});
this is my xml
<RadioGroup
android:id="#+id/rg_cb_votes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<RadioButton
android:id="#+id/cb_votes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="Yes"
android:textSize="12dp" />
</RadioGroup>
Radiobutton in wikipedia says:
A radio button or option button is a type of graphical user interface element that allows the user to choose only one of a predefined set of options.
You would have to just use checkboxes instead of radio button for allowing multiple options to be selected,else you are doing something which is inherently wrong in a UI be it mobile/web.
:-)
I am creating a set of radio options where the radio buttons are removed, and I would like to know which option is selected.
Here is the code
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:id="#+id/languages"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RadioButton
android:id="#+id/english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:text="#string/langtext_english"
android:textSize="16dp"
android:checked="true"
android:button="#null" />
<RadioButton
android:id="#+id/bahasaIndonesia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/langtext_indonesian"
android:textSize="16dp"
android:button="hide"/>
</RadioGroup>
I don't want to show the radio button, but I would like to know which option is selected. onCheckedChangedListener doesn't work as there is no button. How to do this?
When the option is touched or clicked, I would like to show a Toast.
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
langChosen = (RadioGroup) rootView.findViewById(R.id.languages);
langChosen.setOnClickListener(new RadioGroup.OnClickListener() {
#Override
public void onClick(View v) {
Toast abc = new Toast(getContext());
abc.makeText(getContext(), v.toString(),Toast.LENGTH_LONG);
}
You mentioned
I don't want to show the radio button, but I would like to know which option is selected. onCheckedChangedListener doesn't work as there is no button.
I would not say that is true. The RadioGroup still registers changes of the checked radio button. The android:button attribute of RadioButtonjust represents the button you see next to the text/content of the radio button. You still can register to click events of the radio button element itself.
Could it be that you just forgot to call show() on your toast, so you think it's not working?
I supply a tested solution for you here. Let me know if it works for you too.
RadioGroup group = (RadioGroup)this.findViewById(R.id.languages);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton b = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
Toast.makeText(MainActivity.this, b.getText(), Toast.LENGTH_SHORT).show();
}
});
Note that MainActivity.this must be replaced with your corresponding context.
I would try the OnClick method with the two buttons, then the method is called, use the ischecked() method to detremine which of the buttons is checked.
I'm trying to use radio groups where it has 2 radio buttons inside. I wanted to set a value for each radio buttons like for example the value is male and female. After that when I have already click on of my radio button it will display the value selected. How can you achieve this? Thank you for the help.
i have tried something like this just to test on how will i set a value
public void onRadioButtonClicked(View radioButton) {
int count = radioGroup.getChildCount();
for (int i = 0; i < count; i++) {
View o = radioGroup.getChildAt(i);
if (o instanceof RadioButton) {
RadioButton radioBtn = (RadioButton)o;
// get the state
boolean isChecked = radioBtn.isChecked();
// to set the check
radioBtn.setChecked(true);
}
}
}
but i think its not what i'm looking for.
i have already tried radioGroup.setId() assuming that the radio buttons have values already but it just display nonsense number.
You should use a RadioGroup and an OnCheckedChangeListener in your code to detect when a RadioButton is selected. In your layout XML:
<RadioGroup
android:id="#+id/radioGroup"
... >
<RadioButton
android:id="#+id/radioButton1"
... />
<RadioButton
android:id="#+id/radioButton2"
... />
</RadioGroup>
In your Activity/Fragment, set up like so:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangedListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
<type> value = <default value>;
switch (checkedId) {
case R.id.radioButton1:
value = ...;
break;
case R.id.radioButton2:
value = ...;
break;
}
// do something with value
}
});
Make sure that your radio buttons are wrapped in a radio group like below:
<RadioGroup
android:id="#+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioGroupButtonMale"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radioGroupButtonFemale"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</RadioGroup>
And in code, to set the value,
RadioGroup mySelection = (RadioGroup)findViewById(R.id.myRadioGroup);
int radioButtonId = mySelection.getCheckedRadioButtonId();
String selectedValue;
switch (radioButtonId)
{
case R.id.radioGroupButtonMale:
selectedValue = "Value for Male";
break;
case R.id.radioGroupButtonFemale:
selectedValue = "Value for Female";
break;
}
I have the following code :
private void inflateCustomView()
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customNav = inflater.inflate(R.layout.custom_view, null);
//Bind to its state change
RadioGroup radioGrp = ((RadioGroup)customNav.findViewById(R.id.radio_nav));
radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.moneyRadioBtn:
isInCashMode = true;
break;
case R.id.unitRadioBtn:
isInCashMode = false;
break;
}
setCashOrUnits(isInCashMode);
}
});
//Attach to the action bar
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
The custom view seems to be correctly added but onCheckedChanged is never called. I tried inserting a breakpoint inside onCheckedChanged , but it was never reached. What am I doing wrong?
edit:
Here's the XML of the custom_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center_vertical"
android:orientation="horizontal"
>
<RadioGroup
android:id="#+id/radio_nav"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="#+id/moneyRadioBtn"
android:text="Kr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
/>
<RadioButton
android:id="#+id/unitRadioBtn"
android:text="Enheder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
/>
</RadioGroup>
</LinearLayout>
Check with below code...
RadioGroup radioGrp = ((RadioGroup)customNav.findViewById(R.id.radio_nav));
int checkedRadioButtonID = radioGrp.getCheckedRadioButtonId();
radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.moneyRadioBtn:
isInCashMode = true;
break;
case R.id.unitRadioBtn:
isInCashMode = false;
break;
}
setCashOrUnits(isInCashMode);
}
});
Notice,
radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
Hope, this helps..
You can check like this also.
radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
RadioButton radioButton = (RadioButton) findViewById(radioGroup
.getCheckedRadioButtonId());
if (radioButton.getText().equals("radiobutton name")) {
isInCashMode = true;
} else {
isInCashMode = false;
}
}
});
Just try. it may help you.
You should probably paste in your XML layout, sometimes there can be problems there that stop the Radio Group from picking up the buttons within.
It's been a while since I worked with them, but I believe in the XML that Buttons have to be directly under the parent RadioGroup in XML, so if you have for example, a RadioButton within a LinearLayout within a RadioGroup, the RadioGroup won't pick up the button because it's not a direct child.
Edit: An alternative is to get a reference to each individual RadioButton in your code, define a CompoundButton.OnCheckedChangeListener to handle the event, and then call
myRadioButton.setOnCheckedChangeListener(myOnCheckChangeListener);
for each of them. A bit clunky (you'll have to manually get the id of the checked view in the onCheckedChanged() method) but it works!
I ran into this problem today and as nonsensical as it seems:
It requires CheckBox.OnCheckedChangeListener. RadioGroup blatantly refuses to accept RadioGroup.OnCheckedChangeListener but works fine with the CheckBox one.
I don't even KNOW what is going on but it works like this. Barely.
Hello Everybody
How do I get validate my form specially for "gender", where I have used RadioButton, so that when "male" is selected "female" will automatically get unselected. Again for that will I have to take RadioButton or it'll be better to take RadioGroup?
Thanks.
Put your gender RadioButtons inside a RadioGroup
Use RadioGroup.
You can see the full documentation RadioGroup
You can refer Example from developer.android.com -> http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons
For Example in your xml file :
<RadioGroup android:layout_height="wrap_content" android:id="#+id/gender_group"
android:layout_width="match_parent">
<RadioButton android:text="Male"
android:layout_width="wrap_content" android:id="#+id/radio0"
android:layout_height="wrap_content" android:checked="true"></RadioButton>
<RadioButton android:text="Female"
android:layout_width="wrap_content" android:id="#+id/radio1"
android:layout_height="wrap_content"></RadioButton>
</RadioGroup>
try this
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}