Radio button status is always 'checked' - java

Here is my code:
RadioButton daysAsCustomerRadio = (RadioButton)view.findViewById(R.id.days_as_customers_radio);
daysAsCustomerRadio.setOnClickListener(new RadioButtonHandler());
//Action Listener for the Radio Button
private class RadioButtonHandler implements OnClickListener
{
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
RadioButton checkedButton = (RadioButton)view.findViewById(view.getId());
Log.d("CHECKED_STAT", "Button stat: "+daysAsCustomerRadio.isChecked());
Log.d("CHECKED_STAT_1", "Button stat: "+checkedButton .isChecked());
if(checkedButton.isChecked())
{
checkedButton.setChecked(false);
}
else
{
checkedButton.setChecked(true);
}
}
}
What I am trying to do is this.
If the user clicked on a selected radio button, then deselect it.
If the user clicked on a deselected radio button, then select it.
But the case is, the radio button status is always true, even when the radio button is not selected. I even tried to set it to false pragmatically first in onCreateView() (This is a Fragment class), but still, the same result.
Why is this?
PS. There are no RadioGroups and can't add as well.

Finally, I managed to solve this by using a trick. I have boolean variables to check whether the RadioButtons are checked or not.

Related

How to enable button save when all fields and one of the RadioButton and Spinner are checks and fileds in Android Studio?

I have 3 editText , for Gender I have RadioGroup and I have one spinner with 4 options. I want to do enable Save Button when all fields are filed and one of RadioButton is chosen and one of spinner is chosen too.
I made setOnCheckListener for three editText with one Watcher but for RadioButton function isChecked (for example female.isChecked()) doesn't work.
How can I do that ?
Without a code example I can only provide a generic answer.
Radio buttons should be inside a Radio Group. Give the Radio Group an id, let's say the id is gender_radio_group.
Then give each Radio Button an id, let's say the id's are male_radio_button and female_radio_button
Now in the code for that view you need to use the following code:
RadioGroup genderRadioGroup = (RadioGroup) findViewById(R.id.gender_radio_group);
genderRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
//Get the id of the checked radio button in the group
int radioButtonID = group.getCheckedRadioButtonId();
//Get the radio button view from the group using the above id
View radioButton = group.findViewById(radioButtonID);
//Get whether the button is checked or not
boolean checked = ((RadioButton) radioButton).isChecked();
switch(radioButton.getId()) {
case R.id.male_radio_button:
if (checked)
//Do some logic if the male radio button is checked
break;
case R.id.female_radio_button:
if (checked)
//Do some logic if the female radio button is checked
break;
}
}
});
There is another way that this can be done. You can find more details on the Radio Buttons Official Android Documentation.
Assign the radio group checked status an 'int' and use it to in the checking logic. Of course you have to also ensure that the spinner has a listener too. see:
RadioGroup genderRadioGroup = (RadioGroup) findViewById(R.id.gender_radio_group);
genderRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
//Get the id of the checked radio button in the group
int radioButtonID = group.getCheckedRadioButtonId();
//Get the radio button view from the group using the above id
View radioButton = group.findViewById(radioButtonID);
//Get whether the button is checked or not
boolean checked = ((RadioButton) radioButton).isChecked();
switch(radioButton.getId()) {
case R.id.male_radio_button:
if (checked)
int checkedChecker=1;
//Do some logic if the male radio button is checked
break;
case R.id.female_radio_button:
if (checked)
int checkedChecker=1;
//Do some logic if the female radio button is checked
break;
}
}
});
if(editText1.getText.toString.isempty&&
editText1.getText.toString.isempty&&
editText1.getText.toString.isempty&&
spinnerValue.isempty&&
intChecker>0){
saveButton.setEnabled(true);
}else{
saveButton.setEnabled(false);
}

RadioButton not deselecting

Here is my code, I want to unselect a radio button when I click on it again. Since I have some code that only works when none of the radiobuttons are selected.
if (TablesOn == true) {
TablesOn = false;
} else {
TablesOn = true;
}
if (jRadioButton1.isVisible()==true) {
jRadioButton1.setVisible(false);
jRadioButton2.setVisible(false);
jRadioButton3.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
} else {
jRadioButton1.setVisible(true);
jRadioButton2.setVisible(true);
jRadioButton3.setVisible(true);
jLabel3.setVisible(true);
jLabel4.setVisible(true);
jLabel5.setVisible(true);
}
To deselect a RadioButton which is inside of RadioGroup and be able to then select it again, firstly get your RadioGroup:
RadioGroup rg = (RadioGroup) findViewById(R.id.your_radio_group_name_in_layout);
And then do:
rg.check(-1);
Of course, you need some code to save last checked button number, and if it is pressed again, deselect it using the code above.

dynamically selecting radiobutton view inside activity

I am working on an application where my activity has 2 radio buttons.
4 channel
9 channel
Now:
default selection of the screen is on 4channel radio button.
if the user wants to click the (9channel) button, it will open a dialog box prompting user
" if you move, all saved items will be deleted "
In the dialog box i have "Yes" and "No" button. If you click "Yes" button it will select the 9 channel radio button.
The issues that i want to fix are :
when the user clicks on "No" button on the dialog box, i quit the dialog. But selection of radio button still moves to the (9channel) radio button. How to keep the old selection if user click "No" on the dialog box?
Default selection on the activity is on (4channel) radio button. If the user clicks on the same selected button, i dont want the dialog box to open.
The dialog box should open only if i click on the other radio button .
How can i achieve the above 2 scenarios? I don't know how to get the radionbutton view and show to user.
my Code :
rg = (RadioGroup) findViewById(R.id.radioGroup1);
public void addListenerOnButton()
{
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
selection = (String) rb.getText();
mFlag = true;
showDialog(SELECTION_ALERT_ID);
}
};
RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb2.setOnClickListener(listener);
}
Make a code for "Negative" option in your code:
new AlertDialog.Builder(this)
.setTitle("Hello!")
.setMessage("Choose your RadioButton:")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
rb1.setChecked(true);
rb2.setChecked(false);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
rb1.setChecked(false);
rb2.setChecked(true);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
for 1, in your dialog button listener, do:
first, move your radio button to global variable and add following listener to your dialog popup.
then, update the listener for negative button :
.setNegativeButton(getString(R.string.alert_dialog_negative_text),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//not checked, but according to api, should be this method
rb1.setSelected(true);
}
});
for 2, you can do a check in your listener code and if the clicked radio button is checked, do not showDialog.
But personally I would recommend to use the OnCheckedChangeListener for the radio button, refer to this question: radio button listener

Make an action when button clicked only if checkbox is marked

I have a button and a checkbox. How can I do if the user check the box, it make one action (like a toast) and if the box is not checked another toast.
I mean:
If the box is checked and I click the button:
'The box was checked'
If the box is not checked and I click the button
'The box was not checked'
Inside button listener place if statement to check if checkbox is checked. If checked do something else don't do.
A simple algorithm would be:
if(checkbox is checked){
display toast saying is checked
}else{
display toast saying is not checked
}
And put this inside your onClickListener of button
From the Android documentation:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
// display toast1
}
else {
// display toast2
}
}
});
Is that what you're looking for?
https://developer.android.com/reference/android/widget/CheckBox.html

How to avoid consecutive button clik for same button in android

i want to avoid click event again on the same button continuously (i.e) consecutively click on same button
whether its possible or not
yes it is possible in differents ways :
1) First method : use a boolean variable and update it when you want :
boolean isClickable = true;
Button btn;
// other declarations..
// in the onCreate method :
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
onClick() method :
#Override
public void onClick(View v) {
if(isClickable) {
//this code will be executed only when you set the boolean isClickable to true
//your code here
}
}
And when you want to desactivate you click button ; just set the boolean to false :
isClickable = false;
2) Second method :
there is a method on View which will let your button non Clickable ; and then desactivate the listener on your Button so no Object will listen the event click on your button like this :
btn.setClickable(false);
btn.setOnClickListener(null);
NB : when you want to turn your button clickable again , just set the listener to this and make it clickable again ( for the Second Method ) :
btn.setClickable(true);
btn.setOnClickListener(this);
or just update the value of the boolean isClickable (for the first method)
isClickable = true;
you can set the onClickListener to null when you want to disable handling of this event, and set it back to the same listener you've had before in order to re-enable it .
of course , there are many solutions for this as Houcine has written .

Categories