I am working on android project. I have 3 pages in which each page contains some decription and below the desciption 5 to 8 checkboxes. I kept the next button at the top of each page. I am storing the selected check box id's in an arraylist. i.e if I check the checkbox the id will be stored in the arraylist and if I uncheck the checkbox the id will be deleted from the arraylist. (I need the selected checkboxes fo further pages and hence this task). If none of the checkbox is selected and next button is clicked i want to display an alert saying that "Please select atleast one". How to check if none of the checkbox is selected and how to display an alert box? Please help me with this issue.
Thanks in advance
Loop through your ArrayList of IDs and check if each box is checked.
boolean isChecked = false;
for (int id : yourArrayOfIds) {
if (((CheckBox) findViewById(id)).isChecked()) {
isChecked = true;
break;
}
}
// If isChecked==true, a box is checked.
You may also want to check if the CheckBox object is null before using isChecked(). This is just a rudimentary example.
To show your alert, use something like this. There are lots of other tutorials and documentation as well.
Related
as indicated in the title I want the spinner to proceed in the next item upon clicking the OK button.
BTW the spinner items was fetched in mysql Database.
For example I have these items
"Jeffrey Oliveras"
"Wendy Lixardo"
"Mc Salinas"
and the first selection is " Jeffrey Oliveras" then upon clicking the okay button it will automatically proceed to "Wendy Lizardo"
any help will be appreciated.
Use this code in your Button Onclicklistener
if(spinner.getSelectedItemPosition() < spinner.getAdapter().getCount()){
spinner.setSelection(spinner.getSelectedItemPosition()+1);
}
Hope it work.
I have 20 checkbox and want to check is all checkbox is checked.
I use the below case to check all using OR operator.
if (((CheckBox) v).isChecked()) {
Toast.makeText(MyAndroidAppActivity.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
But is there any optimized way to check whether all Checkbox is selected
An optimised way :
Maintain an int field in your activity which has a default value of 0.
In your checkbox's onChecked listener, increment or decrement the value of the int field by 1 depending on whether it is checked or unchecked.
If the value of the int field is equal to 20, that means all of the checkboxes are checked.
Alternatively, you could also run a loop with the same code you mentioned as the body.
I have 12 CheckBox in an array and I have to check if more than 1 CheckBox is checked I have to get toast message "cannot select more than 1 checkbox".
My code:
chkArray = new CheckBox[12];
chkArray[0]=(CheckBox) findViewById(R.id.checkBox1);
chkArray[1]=(CheckBox) findViewById(R.id.checkBox2);
chkArray[2]=(CheckBox) findViewById(R.id.checkBox3);
chkArray[3]=(CheckBox) findViewById(R.id.checkBox4);
chkArray[4]=(CheckBox) findViewById(R.id.checkBox5);
and so on
chkArray[0].setOnClickListener(mListener);
chkArray[1].setOnClickListener(mListener);
chkArray[2].setOnClickListener(mListener);
A simple solution is adding a loop in the onCheckedChanged listener where you check each checkbox for true or false. If more than one checkbox is checked (including the last checked checkbox), set that checkbox to false and display the message.
try this
http://developer.android.com/reference/android/util/SparseBooleanArray.html
Store array of boolean value with index as a key.
put this on click listener of checkbox
if (mSparseBooleanArray.get(i)) {
//here value with true state
//here you can know any one item is selected
Toast.makeText(getContext(),"cannot select more than 1 checkbox",
Toast.LENGTH_LONG).show();
return;
}
I'm not sure what you are trying to accomplish, but it sounds like you want the functionality of radiobuttons instead of checkboxes.
RadioButton:
Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive.
More information at http://developer.android.com/guide/topics/ui/controls/radiobutton.html
I am using checkbox for the purpose of checking "Term & Condications".
if user had selected only checkbox (Square icon) that means terms are accepted
and if not then terms are not accepted, but if user clicks on that checkbox's text then i should be able to view "terms & condications" page.
Thanks.
take one checkbox and on Textview(better to use button )
setOnClickListener on textView to show T&C .
check ckeckBox status throgh checkbox.ischecked() and work accordingly.
I don't know exactly what you want but you can do it with the checkbox. You need to create your checkbox and then you can check if checkbox is checked or not.
For example:
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
Toast.makeText(getApplicationContext(), "You accepted the Terms", Toast.LENGTH_SHORT);
}
else {
Toast.makeText(getApplicationContext(), "You haven't accepted the Terms", Toast.LENGTH_SHORT);
}
Here is one example, which can help to understand it:
http://developer.android.com/reference/android/widget/CheckBox.html
Hope it helps.
Hello I am New to Android, My requirement is allow the user to check maximum 3 number of checkboxes. when user is trying to check fourth one should populate dailogue.
Iam trying but the problem is fourth one is not unchecking...
can any one help me...
Regards
Shiva.M
Just uncheck the checkbox using the following code block:
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}