RadioButtons in RadioGroup arent working as intended - android

I have a RadioGroup with two RadioButton's:
<RadioGroup
android:id="#+id/main_radiogroup_lock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/main_layout_lock_dummy"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/main_radiobutton_lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="110dp"
android:background="#drawable/custom_radiobutton_lock"
android:button="#null"
android:contentDescription="#string/imageview_description" />
<RadioButton
android:id="#+id/main_radiobutton_unlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="230dp"
android:background="#drawable/custom_radiobutton_unlock"
android:button="#null"
android:checked="true"
android:contentDescription="#string/imageview_description" />
</RadioGroup>
I initialize the RadioButton's and set an OnCheckedChangeListener:
...
this.radioButtonLock = (RadioButton) view.findViewById(R.id.main_radiobutton_lock);
this.radioButtonLock.setOnCheckedChangeListener(this);
this.radioButtonUnlock = (RadioButton) view.findViewById(R.id.main_radiobutton_unlock);
this.radioButtonUnlock.setOnCheckedChangeListener(this);
...
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
if(view.getId() == R.id.main_radiobutton_lock) {
Log.v("MainFragment", "Lock");
this.imageButtonLanguage.setEnabled(false);
this.buttonOpen.setEnabled(false);
this.buttonAutomatic.setEnabled(false);
this.buttonClose.setEnabled(false);
this.buttonFog.setEnabled(false);
this.checkBoxRed.setEnabled(false);
this.checkBoxGreen.setEnabled(false);
this.checkBoxBlue.setEnabled(false);
return;
} else if(view.getId() == R.id.main_radiobutton_unlock) {
Log.v("MainFragment", "Unlock");
this.imageButtonLanguage.setEnabled(true);
this.buttonOpen.setEnabled(true);
this.buttonAutomatic.setEnabled(true);
this.buttonClose.setEnabled(true);
this.buttonFog.setEnabled(true);
this.checkBoxRed.setEnabled(true);
this.checkBoxGreen.setEnabled(true);
this.checkBoxBlue.setEnabled(true);
return;
}
}
So when i now start the app my radioButtonUnlock is checked. When i know chec the radioButtonLock nothing happens and no log output but the checked image is set so the checking is working. When i after that again check the radioButtonUnlock the following output appears:
02-04 09:59:31.355: V/MainFragment(533): Lock
02-04 09:59:31.355: V/MainFragment(533): Unlock
So both RadioButton's are fired. After that everytime i press one of the RadioButton's all events are fired. After pressing the radioButtonLock the following happens:
02-04 09:59:32.850: V/MainFragment(533): Unlock
02-04 09:59:32.850: V/MainFragment(533): Lock
Whats the point of that? I set different ID's so why are always both RadioButtons's fired?

Try this hope will work -
1)set check change listener to radio group
RedioGroup rg =(RadioGroup)findviewByid(R.id.main_radiogroup_lock);
rg.setcheckchangedlistener(true);
2)handle on check change listener event
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rdb1:
radio_selected = R.id.rdb1;
Toast.makeText(getApplicationContext(),"first",Toast.LENGTH_LONG).show();
case R.id.rdb2:
radio_selected = R.id.rdb2;
Toast.makeText(getApplicationContext(),"second",Toast.LENGTH_LONG).show();
}
}

Related

How to handle dynamically added radiobutton check and uncheck in android

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.
:-)

Reset a Radio button inside a Radio group

How I reset a radio button inside a radio group like the image example below
All my radio groups and its radio buttons are created programmatically.
I tried to to set OnClickListener to get the radio button value before getting changed but, it didn't help.
Edit: I posted the answer below, please check it.
try this :
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId) {
if (checkedId == R.id.radioButton){
//do something
}
else if(checkedId == R.id.radioButton2){
}
else{
}
});
xml layout
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_below="#+id/imageView"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="#+id/radioGroup"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="#+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
You have to uncheck one radiobutton while checking the other.
Simple way.
public void clearRadioChecked() {
alt1.setChecked(false);
blt2.setChecked(false);
}
if you want to select alt1 then on click of alt1 do as below.
clearRadioChecked()
alt1.setChecked(true);
This is how I did it:
It is in C# but I think it is easy to convert it to Java.
I used tag to know if this radio button checked before or not.
False => It is not checked
True => It is checked
radiobutton.Tag = false;
radiobutton.Click += SingleChoiceQuestionAlternativeClick;
Then:
private void SingleChoiceQuestionAlternativeClick(object sender, EventArgs e)
{
RadioButton questionAlternative = sender as RadioButton;
if (questionAlternative != null)
{
RadioGroup questionAlternatives = questionAlternative.Parent as RadioGroup;
if (questionAlternatives != null)
{
if (questionAlternative.Tag.Equals(false))
{
for (int i = 0; i < questionAlternatives.ChildCount; i++)
{
RadioButton childRadioButton = questionAlternatives.GetChildAt(i) as RadioButton;
if (childRadioButton != null)
{
childRadioButton.Tag = false;
}
}
questionAlternative.Tag = true;
}
else
{
questionAlternative.Tag = false;
questionAlternatives.ClearCheck();
}
}
}
}

How to set value of a radio button inside a radio group android

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;
}

RadioGroup not responding in onCheckedChanged

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.

How to do something when a checkbox change state?

This is my code:
<CheckBox
android:id="#+id/sprint_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sprint_game" />
<CheckBox
android:id="#+id/marathon_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/marathon" />
<CheckBox
android:id="#+id/never_ending_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/never_ending" />
What i want to do is "detect" when one of these is checked and then set the other two to "disable", so the user can select only one at time.
I tried to use the ".setOnCheckedChangeListener", but i can't do that, can someone help me with some code?
Thanks a lot guys!
This is the way you are notified about checked changes:
CheckBox check = findViewById(R.id.sprint_checkbox);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});
You can also let your activity implements the OnCheckedChangeListener interface and then:
CheckBox check1 = findViewById(R.id.sprint_checkbox);
CheckBox check2 = findViewById(R.id.marathon_checkbox);
CheckBox check3 = findViewById(R.id.never_ending_checkbox);
check1.setOnCheckedChangeListener(this);
check2.setOnCheckedChangeListener(this);
check3.setOnCheckedChangeListener(this);
Overriding the interface method:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.sprint_checkbox:
//do stuff
break;
case R.id.marathon_checkbox:
//do stuff
break;
case R.id.never_ending_checkbox:
//do stuff
break;
}
}
I believe a RadioButton would be more suitable for your aims.
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radio_group1">
<RadioButton
android:id="#+id/sprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option1"
android:checked="true"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/marathon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option2"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/neverending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option3"
android:onClick="onRadioButtonClick"
/>
</RadioGroup>
Then in your code:
public void onRadioButtonClick(View v) {
RadioButton button = (RadioButton) v;
// DO YOUR THING HERE DEPENDING ON WHICH RADIOBUTTON IS PICKED
}
In this case, you won't have to deal with disabling other options. The user will have only one option to pick by default.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
switch(id){
case R.id.sprint:
//add to database
...
}
}
else{
switch(id){
case R.id.sprint:
//remove from database
...
}
}
}
This should allow you to with ID's. Hope it works.

Categories

Resources