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.
Related
I've an application UI with
some TextView inside a ViewHolder
some buttons
If the user change the text inside one of the TextView I need to check the value to be sure that is between some ranges. The check must be done immediately after the user change focus (as example because click on one button, this is needed because I had to do some actions in response to some specific values).
I implemented the check setOnFocusChangeListener() but seems that the event is not fired on button click but only if I click on another TextView
How can I be sure that the value is checked on any kind of event like back button, other UI button click and so on?
For performance reason I prefer that the check is performed only on focus change.
Below my current code for one of the TextView that are inside the ViewHolder:
holder.articleQuantity.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
EditText articleQuantity = (EditText) v;
String currentQuantity = articleQuantity.getText().toString();
if(ArticleModelArrayList.size()>0) {
int adapterPos = holder.getAdapterPosition();
if(adapterPos > 0) {
ArticleModel article = ArticleModelArrayList.get(adapterPos);
if (!currentQuantity.equals("") && (Integer.parseInt(currentQuantity) > 0)) {
article.setQuantity(Integer.parseInt(currentQuantity));
}
articleQuantity.setText(article.getQuantity().toString());
ArticleModelArrayList.set(adapterPos, article);
}
}
}
});
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);
}
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
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.
I'm teaching myself to write an Android app. I use the following code to successfully choose the different database for my app:
public void manageDB()
{
setContentView(R.layout.dbmanager);
ScrollView ll = (ScrollView) findViewById(R.id.lstDb);
final RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT));
rg.setOrientation(RadioGroup.VERTICAL);
for (int i=0; i < mDBList.items.size(); i++)
{
RadioButton rb = new RadioButton(this);
rb.setId(VIEW_RADIO_ID + i);
rb.setText(mDBList.items.get(i).dictionaryName);
rb.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rg.addView(rb,i);
if (mDBFile != null && mDBFile.fileName.equals(mDBList.items.get(i).fileName))
{
rg.check(VIEW_RADIO_ID + i);
}
}
ll.addView(rg);
Button btnOk = (Button) findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.main);
int selectedIndex = rg.getCheckedRadioButtonId();
if (selectedIndex < 0)
{
selectedIndex = 0;
}
mDBFile = mDBList.items.get(selectedIndex - VIEW_RADIO_ID);
savePreferences();
setContentView(R.layout.main);
menuMain();
}
});
}
With this code, I can click on a radio button to choose a database, and the click on the OK button to confirm and return to the main screen. Now I want to assign a button on the main screen to do this task so that I don't have to leave the main screen when choosing a database.
Since I have two pieces of database, ideally I want a two-way button to do this task. I mean when I click on the button, db_1 is chosen. When the button is clicked again db_2 is chosen, and vice versa.
I have no idea how to adapt this code to meet this requirement of mine. I wonder if you guys can give me a little help. Thank you very much in advance.
If your need is to change database in one click instead of two, you could use a radiogroup containing your 2 radiobuttons, and listen for checked radiobutton change.
Then in onCheckedChange() you load the right database.
Why don't you use "Switch" I think it will solve your purpose. Look at the link [here:][1]
[1]: http://developer.android.com/reference/android/widget/Switch.html Or you can also use the toggle buttons. Hope it helps.