How to handle dynamically added radiobutton check and uncheck in android - 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.
:-)

Related

How to find out selected radio option, when android:radiobutton="#null" in android?

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.

Android How to hide the phone number programmatically

How can I hide or show my phone number during a call without going into the settings?
you can implement this by using a check box :
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cb_Show" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"
/>
_
CheckBox cbShow = (CheckBox) findViewById(R.id.cb_Show);
TextView text=findViewById(R.id.text);
text.setText("some text to hide");
cbShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
text.setVisibility(VIEW.INVISIBLE);
else
text.setVisibility(VIEW.VISIBLE);
}
});
or you can use a toggleButton instead example
good luck

RadioButtons in RadioGroup arent working as intended

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

Set default checked Radio Button

I'm creating a RadioGroup with RadioButtons dynamically and need to have one radio button checked by default.
I've done this by using both radioButton.setChecked(true) and radioButton.toggle();
The problem I have is that when I at runtime select another radio button the first one stays checked so I end up with two checked radio buttons in the radio group.
Has anyone had this problem and know how to solve it?
private RadioButton addRadioButton(String type, String price){
RadioButton radio = new RadioButton(Order.this);
radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
radio.setTag(price);
if(type.toLowerCase().equals("m"))
radio.toggle();
return radio;
}
I know I'm late but for those who search for the answer like me this can be useful.
When you add RadioButton to a RadioGroup, you must set the button checked after adding it to the parent like:
RadioGroup radioGroup = new RadioGroup(getContext())
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText("text");
mRadioGroup.addView(radioButton);
radioButton.setChecked(true);
If the view is checked before adding it to the parent, it will be impossible to uncheck it.
You can do this simply:
JAVA:
radiogroup =(RadioGroup)findViewById(R.id.radiogroup);
radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
radiobutton2 =(RadioButton)findViewById(R.id.radiobutton2);
if(your first assessment){
radiobutton1.setChecked(true);
}else{
radiobutton2.setChecked(true);
}
XML:
<RadioGroup
android:id="#+id/radiogroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radiobutton1"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
Simple, at first time load the screen check the position of In Getview method and then apply condition:
if (position == 0) {
holder.act_delivery_rbtn.setChecked(true);
}
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
When using radios you usually have more than one and choose between them. Like easy, medium, hard.
Replace
radio.toggle();
with
radio.setChecked(true);
Just make checked = "true" in your code
you should check it in radio group...
radiogroup.check(IdOfYourButton)
use the clearCheck () function to clear the already checked buttons.I hope this solves your problem.
Turned out to be because I called the checked() method on the radio group straight after setting the OnCheckedChangeListener() causing it to be called immediately and throw a NPE for some reason.
Moved the checked() method call to just before the OnCheckedChangeListener() and it works now.
radios.check(2);
radios.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
private RadioButton addRadioButton(String type, String price){
RadioButton radio = new RadioButton(Order.this);
radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
radio.setTag(price);
if(type.toLowerCase().equals("m"))
radio.setId(2);
return radio;
}
in Xml add android:checkedButton="#+id/storeRB" to RadioGroup
<RadioGroup
android:id="#+id/storeTypeRG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/storeRB"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/text2"
app:layout_constraintTop_toTopOf="#+id/text2">
<RadioButton
android:id="#+id/storeRB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:button="#drawable/radio_button_bg"
android:paddingStart="8dp"
android:text="#string/store_str"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/text2"
app:layout_constraintTop_toTopOf="#+id/text2" />
<RadioButton
android:id="#+id/hyperRB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:button="#drawable/radio_button_bg"
android:paddingStart="8dp"
android:text="#string/hyper_str"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/storeRB"
app:layout_constraintTop_toTopOf="#+id/text2" />
<RadioButton
android:id="#+id/restaurantRB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:button="#drawable/radio_button_bg"
android:paddingStart="8dp"
android:text="#string/restaurant_str"
app:layout_constraintBottom_toBottomOf="#+id/text2"
app:layout_constraintStart_toEndOf="#+id/hyperRB"
app:layout_constraintTop_toTopOf="#+id/text2" />
</RadioGroup>

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