How save state of checkbox? if is enabled or disabled - android

I'm making an app that have a lot of checkbox. I wanted save the states of these but only could save if these are checked, but now I want save if is enabled or disabled, since some checkbox active states to other checkbox. How can i do that?
...
if (view.equals(contador11)){
if(contador11.isChecked()){
contador14.setEnabled(true);
}else{
contador14.setEnabled(false);
}
}
if (view.equals(contador12)){
if(contador12.isChecked()){
contador13.setEnabled(true);
contador26.setEnabled(true);
}else{
contador13.setEnabled(false);
contador26.setEnabled(false);
}
}
if (view.equals(contador7)){
if(contador7.isChecked()){
contador15.setEnabled(true);
}else{
contador15.setEnabled(false);
}
}
...

I'm not following your code exactly, but assuming you want to save all of them at the same time, you could save code by just putting them in a list or something. Here's an example.
private SharedPreferences prefs;
private CheckBox[] contadors;
private CheckBox contador1, contador2, contador3, contador4, contador5, contador6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
contador1 = (CheckBox) findViewById(R.id.contador1);
contador2 = (CheckBox) findViewById(R.id.contador2);
contador3 = (CheckBox) findViewById(R.id.contador3);
contador4 = (CheckBox) findViewById(R.id.contador4);
contador5 = (CheckBox) findViewById(R.id.contador5);
contador6 = (CheckBox) findViewById(R.id.contador6);
}
public void clickContador(View view) { // Just for testing
CheckBox contador = (CheckBox) view;
view.setEnabled(false);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveContadors();
}
#Override
protected void onResume() {
super.onPostResume();
contadors = new CheckBox[]{contador1, contador2, contador3, contador4, contador5, contador6};
loadContadors();
}
private void saveContadors() {
SharedPreferences.Editor editor = prefs.edit();
for (int i = 0; i < contadors.length; i++)
editor.putBoolean("contador" + i, contadors[i].isEnabled());
editor.apply();
}
private void loadContadors() {
for (int i = 0; i < contadors.length; i++)
contadors[i].setEnabled(prefs.getBoolean("contador" + i, true));
}
<CheckBox
android:id="#+id/contador1"
android:onClick="clickContador"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

To put the state of your CheckBox, we can use SharedPreferences:
CheckBox contador = (CheckBox) findViewById(R.id.contador1);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
if (contador.isChecked()) {
prefs.edit().putBoolean("key", true).commit(); // put true value
}else{
prefs.edit().putBoolean("key", false).commit(); // put false value
}
To get the value you saved back:
boolean myContador = prefs.getBoolean("key", false);
if (myContador == true) {
// do your thing
}else{
// the value is false, do something
}

Related

SharedPreference to remember the checkbox is visible and checked

I have a checkbox that is invisible, however when I click on the menu item the checkbox is visible. What should the SharedPreferences look like if the checkbox is checked, and if I leave the app and return to the app to keep the checkbox visible. Now, when I leave the app and the checkbox is checked, and when I return to the app the checkbox is invisible and did not remember that the checkbox was checked.
<CheckBox
android:id="#+id/my_check"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Save Audio file"
android:textColor="#000000"
android:layout_marginTop="20dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
private CheckBox my_check;
// onCreate
addListenerOnSaveAudio();
switch (item.getItemId()) {
case R.id.save_audio:
saveAudio.setVisibility(View.VISIBLE);
}
public void addListenerOnSaveAudio() {
my_check = (CheckBox) findViewById(R.id.my_check);
my_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
Here is the doc for sharedPreference and how to use it.
You need to save the state of your checkbox when someone clicks your menu to do that you can use:
// in onCreate
Context context = getActivity();
SharedPreferences preferences = context.getSharedPreferences(
"NAME OF YOUR CHOICE", Context.MODE_PRIVATE);
CheckBox checkBox = findViewById(R.id.my_check);
SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("my_check") && preferences.getBoolean("my_check",false) == true) {
checkBox.setChecked(true);
}else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox.isChecked()) {
editor.putBoolean("my_check", true);
editor.apply();
}else{
editor.putBoolean("my_check", false);
editor.apply();
}
}
});
private boolean state;
private SharedPreferences sharedPreferences;
private Checkbox my_check;
public void addListenerOnSaveAudio() {
//Instance for save state of my_check
sharedPreferences = getSharedPreferences("name_preferences",Context.MODE_PRIVATE);
my_check = (CheckBox) findViewById(R.id.my_check);
// If my check not exist by default is false else true
state = sharedPreferences.getBoolean("my_check", false);
my_check.setChecked(state);
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//When do click in my_check will change state in preferences and when open your app is apply changes in view.
sharedPreferences.edit()
.putBoolean("my_check",isChecked);
if (isChecked) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
You can use onCheckedChanged() to register a listener to your checkBox, and then save its check status into shared preference.
And get the shared preference value each time you launch your app, i.e. in onCreate() method
So modify your activity's onCreate() and your addListenerOnSaveAudio() as below
public class MyActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox my_check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
my_check = findViewById(R.id.my_check);
SharedPreferences sharedPrefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
boolean isChecked = sharedPrefs.getBoolean("checkValue", false); // default value is false
my_check.setChecked(isChecked);
addListenerOnSaveAudio();
}
public void addListenerOnSaveAudio() {
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("checkValue", isChecked);
editor.apply();
if (!isChecked)
saveAudio.setVisibility(View.INVISIBLE);
}
});
}
}

Save CheckBox State to SharedPreferences File in Android

I have a couple of check boxes that I need to save so that when the user opens the Application again, they can see the state that they left the application in. I have tried using the preferences but I can't seem to get the result correctly.
MainActivity.java
package com.example.android.documentchecklist;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
CheckBox allotment, sscMarkList, hscMarkList, leaving, profomaCap, jeeScoreCard, gapCer, casteCer, casteVal, nonCreamyL, domicileCertificate, photograph, migration, adhaarCard, nationalityCertificate;
boolean hasAllotment, hasSscMarkList, hasHscMarkList, hasLeaving, hasProfoma, hasJeeScore, hasGapCertificate, hasCasteCertificate, hasCasteValidity, hasNonCreamyLayer, hasDomicileCertificate, hasPhoto, hasMigCertificate, hasadhaarCard, hasNationalityCertificate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allotment = (CheckBox) findViewById(R.id.allot);
sscMarkList = (CheckBox) findViewById(R.id.ssc);
hscMarkList = (CheckBox) findViewById(R.id.hsc);
leaving = (CheckBox) findViewById(R.id.leaving);
profomaCap = (CheckBox) findViewById(R.id.profoma);
jeeScoreCard = (CheckBox) findViewById(R.id.jeeScore);
gapCer = (CheckBox) findViewById(R.id.gapCertificate);
casteCer = (CheckBox) findViewById(R.id.casteCertificate);
casteVal = (CheckBox) findViewById(R.id.casteValidity);
nonCreamyL = (CheckBox) findViewById(R.id.nonCreamyLayer);
adhaarCard = (CheckBox) findViewById(R.id.adhaarCard);
nationalityCertificate = (CheckBox) findViewById(R.id.nationalityCer);
domicileCertificate = (CheckBox) findViewById(R.id.domicile);
photograph = (CheckBox) findViewById(R.id.photo);
migration = (CheckBox) findViewById(R.id.migration);
}
public void checkBoxClicked(View view) {
int id = view.getId();
if (id == R.id.mahaState) {
migration.setVisibility(View.GONE);
allotment.setText(getString(R.string.allot));
allotment.setVisibility(View.VISIBLE);
hasAllotment = allotment.isChecked();
sscMarkList.setText(getString(R.string.ssc));
sscMarkList.setVisibility(View.VISIBLE);
hasSscMarkList = sscMarkList.isChecked();
hscMarkList.setText(getString(R.string.hsc));
hscMarkList.setVisibility(View.VISIBLE);
hasHscMarkList = hscMarkList.isChecked();
leaving.setText(getString(R.string.leaving));
leaving.setVisibility(View.VISIBLE);
hasLeaving = leaving.isChecked();
profomaCap.setText(getString(R.string.proforma));
profomaCap.setVisibility(View.VISIBLE);
hasProfoma = profomaCap.isChecked();
jeeScoreCard.setText(getString(R.string.jee));
jeeScoreCard.setVisibility(View.VISIBLE);
hasJeeScore = jeeScoreCard.isChecked();
gapCer.setText(getString(R.string.gap_cert));
gapCer.setVisibility(View.VISIBLE);
hasGapCertificate = gapCer.isChecked();
casteCer.setText(getString(R.string.caste_cert));
casteCer.setVisibility(View.VISIBLE);
hasCasteCertificate = casteCer.isChecked();
casteVal.setText(getString(R.string.caste_validity));
casteVal.setVisibility(View.VISIBLE);
hasCasteValidity = casteVal.isChecked();
nonCreamyL.setText(getString(R.string.non_creamy));
nonCreamyL.setVisibility(View.VISIBLE);
hasNonCreamyLayer = nonCreamyL.isChecked();
adhaarCard.setText(getString(R.string.aadhar));
adhaarCard.setVisibility(View.VISIBLE);
hasadhaarCard = adhaarCard.isChecked();
nationalityCertificate.setText(getString(R.string.nationality_cert));
nationalityCertificate.setVisibility(View.VISIBLE);
hasNationalityCertificate = nationalityCertificate.isChecked();
domicileCertificate.setText(getString(R.string.domicile));
domicileCertificate.setVisibility(View.VISIBLE);
hasDomicileCertificate = domicileCertificate.isChecked();
photograph.setText(getString(R.string.photos));
photograph.setVisibility(View.VISIBLE);
hasPhoto = photograph.isChecked();
}
}
#Override
public void onPause() {
super.onPause();
save(allotment.isChecked());
save(sscMarkList.isChecked());
save(hscMarkList.isChecked());
save(leaving.isChecked());
save(profomaCap.isChecked());
save(jeeScoreCard.isChecked());
save(gapCer.isChecked());
save(casteCer.isChecked());
save(casteVal.isChecked());
save(nonCreamyL.isChecked());
save(domicileCertificate.isChecked());
save(photograph.isChecked());
save(migration.isChecked());
save(adhaarCard.isChecked());
save(nationalityCertificate.isChecked());
}
#Override
public void onResume() {
super.onResume();
allotment.setChecked(load());
sscMarkList.setChecked(load());
sscMarkList.setChecked(load());
hscMarkList.setChecked(load());
leaving.setChecked(load());
profomaCap.setChecked(load());
jeeScoreCard.setChecked(load());
gapCer.setChecked(load());
casteCer.setChecked(load());
casteVal.setChecked(load());
nonCreamyL.setChecked(load());
domicileCertificate.setChecked(load());
photograph.setChecked(load());
migration.setChecked(load());
adhaarCard.setChecked(load());
nationalityCertificate.setChecked(load());
}
private void save(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.apply();
}
private boolean load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);
}
}
Thank You for your help.
you are using the single key i.e. check to store your all checkbox state so only the state of this call save(nationalityCertificate.isChecked()); will be saved ,so you need to use different keys for different checkboxes
for example
// use different keys to store state of different check boxes
save(allotment.isChecked(),"allotment");
save(sscMarkList.isChecked(),"sscMarkList");
// use same keys to fetch values which were used during save function call
allotment.setChecked(load("allotment"));
sscMarkList.setChecked(load("sscMarkList"));
private void save(final boolean isChecked, String key) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.apply();
}
private boolean load(String key) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key, false);
}
Note: you can also initialize your shared preference like your CheckBox views in onCreate or onStart only once instead of re-initializing it every time in save

How to apply shared preference to checkbox

I am new to android development I created a checkbox and how to save check/uncheck using share preference
final Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
addNewBubble();
add.setEnabled(false);
}
});
CheckBox checkBox = (CheckBox)findViewById(R.id.add_fb);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isCheckedValue = isChecked;
}
});
}
private void addNewBubble() {
BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
#Override
public void onBubbleRemoved(BubbleLayout bubble) {
finish();
System.exit(0);
}
});
bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {
#Override
public void onBubbleClick(BubbleLayout bubble) {
Intent in = new Intent(MainActivity.this, PopUpWindow.class);
in.putExtra("yourBoolName", isCheckedValue );
startActivity(in);
}
});
bubbleView.setShouldStickToWall(true);
bubblesManager.addBubble(bubbleView, 60, 20);
}
There are two Check boxes in this code add and add_fb I want to make the app remember if the checkbox are checked or unchecked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox checkBox = (CheckBox)findViewById(R.id.checkBox);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("checked") && preferences.getBoolean("checked",false) == true) {
checkBox.setChecked(true);
}else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox.isChecked()) {
editor.putBoolean("checked", true);
editor.apply();
}else{
editor.putBoolean("checked", false);
editor.apply();
}
}
});
}
final String FILE_NAME = "com.myproject.prefs";
final Boolean ADD = "add";
final Boolean ADD_FB = "add_fb";
// prefs file..
SharedPreferences prefs = getSharedPreferences(FILE_NAME, Activity.PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// by default both will be unchecked..
checkBoxAdd.setChecked(prefs.getBoolean(ADD, false);
checkBoxAddFb.setChecked(prefs.getBoolean(ADD_FB, false);
// now on checkedchancedEvent update the preferences
checkBoxAdd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(ADD, isChecked);
editor.apply();
}
});
// similary the second one...
checkBoxAddFb.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(ADD_FB, isChecked);
editor.apply();
}
});
Updated
Here we store our preferences in a file of name "com.myproject.prefs". We need to store two preferences values of Boolean data type (one is add, another one is add_fb, as given in the Q).
Now get SharePreferences and its editor objects.
Set values of checkBoxes from stored preferences, if the preferences were not added before then set these to false. So, on the very first time when you run your app, these will get false values as there aren't any values added before.
Whenever you want to get these preferences values
pass your preferences file_name and mode to getSharedPreferences();
// then use those objects to get any specific value like
prefs.getBoolean(your_value_name, default_value); Already mentioned above
Whenever you want to change these preferences values use SharedPreferences.Editor object. Pass your value name and it's new value to
putBoolean().
like
editor.putBoolean(your_value_name, true/false);
then apply the editing.
Hope, it helps

how to deal with multiple buttons in sharedpreferences

I have 21 togglebuttons and was wondering how to save state of each individual one using sharedpreferences?
i've tried with this:
button = (ToggleButton)findViewById(R.id.btn1);
SharedPreferences sharedPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
button.setChecked(sharedPrefs.getBoolean(PREFS_NAME, true));
but how to make it store values for all buttons and can i do it in PREFS_NAME for all of them?
ok so i created xml file with those buttons:
<ToggleButton
android:textOff="1"
android:textOn="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="#+id/tB1" />
and activity with this code:
int [] viewIds = new int [] {R.id.tB1, R.id.tB2, ...
String [] stringIds = new String [] {"R.id.tB1" ...
ToggleButton button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.randd_screen);
for(int i =0; i<viewIds.length;i++)
{
Log.d(TAG, "inside loop");
Log.d(TAG, "Run no.:" + i);
button =(ToggleButton)findViewById(viewIds[i]);
Log.d(TAG, "button = " + button);
Log.d(TAG, "from stringIds[i] " + stringIds[i]);
SharedPreferences sharedPrefs = getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
button.setChecked(sharedPrefs.getBoolean(Integer.toString(viewIds[i]), false));
}
}
now, how to create onClick method which would update each button value as well as save and load it?
Update 2
so that how my activity look like now but it does not preserve checked buttons. am i missing something or done something wrong?
int [] viewIds = new int [] {R.id.tB1, R.id.tB2, ...
String [] stringIds = new String [] {"R.id.tB1", ...
ToggleButton button;
}
button = (ToggleButton)findViewById(R.id.tB2);
SharedPreferences sharedPrefs = getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
button.setChecked(sharedPrefs.getBoolean(stringIds[1], true));
}
ToggleButton.OnCheckedChangeListener listener = new ToggleButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
Log.d(TAG, "in onCheckChanged()");
int [] viewIds = new int[2];
int index;
for(index = 0; index < viewIds.length; index++) {
if (v.getId() == viewIds[index]) {
getSharedPreferences("PREFS_NAME", MODE_PRIVATE)
.edit()
.putBoolean(stringIds[index], isChecked)
.apply();
break;
}
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.randd_screen);
for(int i =0; i<viewIds.length;i++)
{
Log.d(TAG, "inside loop");
Log.d(TAG, "Run no.:" + i);
button =(ToggleButton)findViewById(viewIds[i]);
Log.d(TAG, "button = " + button);
Log.d(TAG, "from stringIds[i] " + stringIds[i]);
SharedPreferences sharedPrefs = getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
button.setChecked(sharedPrefs.getBoolean(stringIds[i], false));
button.setOnCheckedChangeListener(listener);
}
}
You will need to store state for all of them.
SharedPreferences sharedPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
button = (ToggleButton)findViewById(R.id.btn1);
button.setChecked(sharedPrefs.getBoolean(PREFS_BUTTON1, true));
button = (ToggleButton)findViewById(R.id.btn2);
button.setChecked(sharedPrefs.getBoolean(PREFS_BUTTON2, true));
button = (ToggleButton)findViewById(R.id.btn3);
button.setChecked(sharedPrefs.getBoolean(PREFS_BUTTON3, true));
//...
You could make it much cleaner by implementing a list of IDs and their preference key. You could then loop through them, setting buttons as required.
Updated Question
Do not use the generated integer resource as a SharedPreferences identifier Integer.toString(viewIds[i]). You creating a String array for that purpose. Use that instead stringIds[i].
You can create a listener and apply the same listener to every ToggleButton. In that listener you check which button recieved the call and set the appropriate preference.
ToggleButton.OnCheckedChangeListener listener = new ToggleButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
int index;
for(index = 0; index < viewIds.length; index++) {
if (v.getId() == viewIds[index]) {
getSharedPreferences("PREFS_NAME", MODE_PRIVATE)
.edit()
.putBoolean(stringIds[index], isChecked)
.apply();
break;
}
}
}
};

How I know CheckBox is clicked or not clicked?

How I know CheckBox is clicked or not clicked?
Thank all
xml file:
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
java file:
public class Setting extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
}
//declaring the checked variable at the top
checked=false;
if(checkbox.isChecked())
{
checked=true;
}
else
{
checked=false;
}
try this hope it will help you:
checkbox mchk1=(CheckBox)findViewById(R.id.chk11);
Boolean cBox1;
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
cBox1=pref.getBoolean("check1", false);
if(cBox1==true){
mchk1.setChecked(true);
//Rb1.setChecked(true);
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
if(mchk1.isChecked() ){
editor.putBoolean("check1", true);
}
else if(!mchk1.isChecked() )
{
editor.putBoolean("check1", false);
}
editor.commit();
Try this
CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
if (cb.isChecked()) {
// code to run when checked
} else {
// code to run when unchecked
}
Make changes in your activity as below,
public class Setting extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox myCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
myCheckBox = findViewById(R.id.checkBox);
myCheckBox.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//here the checkBox is checked
}else {
//here the checkBox is not checked
}
}
}
Let me know if it works...

Categories

Resources