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.
Related
I am developing a voting app in Android . am new in android and don't understand how to store my radio button response on per click .
if i have 10 voter in one polling station then
my count=10
if i have 2 party's
party 1
party2
on each time when voter click on radio button then how to calculate and collect the response from the both radio button and how to show the result who is won
election .
any suggestion is welcome.
As per your given code in comment, Declare two global variables which stores count (say party1 and party2) and initialise is to 0. Increase the count when isChecked() is true and reduce it when it is false according to party.
if (view.getId()==R.id.rdb_1)
{
if(rdb_1.isChecked()) {
party1++;
Toast.makeText(this,"you vote for party1",Toast.LENGTH_LONG).show();
}
else party1--;
}
if (view.getId()==R.id.rdb_2)
{
if(rdb_2.isChecked()) {
Toast.makeText(this,"you vote for party2",Toast.LENGTH_LONG).show();
party2++;
}
else party2--;
}
// store values of `party1` & `party2` in `SharedPreference`
Update party1 and party2 variable values in SharedPreference every time. And get values back from SharedPreference before checking your condition (isChecked)
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 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.
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);
}