How do I store the value of a checked checkbox to an array list and display it in a list./..say Checkbox1 = "Apple"......so far I have this
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (checkBox1.isChecked()) {
//my problem is below here: I can't seem to add the value of it not the id and store it in list
pubsList.add(pub3);
pub3=Integer.toString(checkBox1.getSelectedItem());
}
}
For only one check box e.g.selectAll you can have this---
selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Log.e(TAG, "Select all Checkbox enabled");
String ad=(String) selectAll.getText();//get text of selectAll checkbox
SelectionList.add(ad);//add above text to list of strings
}
if(! isChecked)
{
//do something if required
}
}
});
Hope this will help you.
Related
I have check box options in Alert Dialog(android studio).I want the checked value to be stored in array list so that i can use the values in the next activity.So how do i create a array list for the same and store the selected values in it.
You can use with below methods.
ArrayList<String> ids = new ArrayList<>();
ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (ids.contains(yourid)){
ids.remove(yourid);
}else {
ids.add(yourid);
}
}
});
HashMap<String, String> ckList = new HashMap<>();
ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
ckList.put(ckbox.getText().toString(),ckbox.getText().toString());
}
else{
try{
ckList.remove(ckbox.getText().toString());
}catch(NullPointerException e){
e.printStackTrace();
}
}
}
});
I have set setFocusable(false) in my edit text. But when i checked checkbox i want to get focus in my edittext. below is example.
cbIsGap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
edittext1.setFocusable(true);
edittext1.setClickable(true);
}else {
edittext1.setFocusable(false);
edittext1.setClickable(false);
}
}
});
I am trying above example but i am not getting focus when i checked checkbox
I have an activity, which contains 4 checkboxes. So my question is - when all these checkboxes are checked, I want a message to pop-up that should say something like "Good job!", so my question is how do I do it ?
Code of the activity
Code of the checkboxes
Add this code in your onCreate method.
NOTE: You can use a checkbox array to short the code.
But this will help you to understand what to do.
First, you have to create checkbox variables in java file. Then set onCheckChanged listener to each checkbox.
CheckBox cb1=(CheckBox)findViewById(R.id.checkBox);
CheckBox cb2=(CheckBox)findViewById(R.id.checkBox2);
CheckBox cb3=(CheckBox)findViewById(R.id.checkBox3);
CheckBox cb4=(CheckBox)findViewById(R.id.checkBox4);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()&&cb4.isChecked())
Toast.makeText(getApplicationContext(),"Good job." , Toast.LENGTH_SHORT).show();
}
});
Insert this import statements before the class begin (public class flygos2..) . You can see there are other import statements are there also.
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
Add the following updateMessage in your java file.
Add android:onClick="updateMessage" to every checkbox in your XML file. By this, the updateMessage method will be called when we checked/un-checked the check box.
public void updateMessage(View view) {
String message = "";
CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox);
CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox3);
CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
if (cb1.isChecked() && cb2.isChecked() && cb3.isChecked() && cb4.isChecked()) {
message = "Good Job";
} else if (cb1.isChecked() && cb2.isChecked() && cb3.isChecked()) {
message = "One Check box is missed";
} else {
message = "try again";
}
}
I want that when I click on the checkbox "a" for example, then the text view1 displays "a", I have 3 options per text view. Thanks for the help.
a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked) {
text_view1.setText("a");
}
}
};
How do you implement this onTouchEvent? It should fire when the user checks or unchecks the CheckBox widget.
CheckBox checkBox = new CheckBox(activity);
checkBox.setText("Don't present me information again.");
checkBox.onTouchEvent(.....);
The CheckBox widget (and any other widget that extends CompoundButton) has a method setOnCheckedChangeListener, which is the bit you're lacking (you probably don't want to use onTouchEvent in this case).
This example should replace the final line of code in your snippet:
checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked ) {
// do some stuff
}
}
});
what you need in your case is this
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Toast.makeText(yourActivity.this,"CheckBox is Checked ..",3000).show();
}
else{
Toast.makeText(yourActivity.this,"CheckBox is Unchecked ..",3000).show();
}
}
});