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);
}
});
}
}
Related
I am developing a game on Android, I have a background music that stop with a switch and that is in an other activity, that of the parameters. The music is in the main menu.
I would like to know how to save the state of the switch (checked or not checked) by going from the main menu to the parameters
This the code for my switch :
buttonmusique = (Switch) findViewById(R.id.switchMusique);
buttonmusique.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("bouton", String.valueOf(buttonmusique.isChecked()));
// If the music is playing
if (isChecked) {
Intent music = new Intent();
music.setClass(Parametres.this, BackgroundMusic.class);
startService(music);
musicBoolean = true;
buttonmusique.setText("Musique On"); //To change the text near to switch
Log.d("You are :", "Checked");
} else {
Intent music = new Intent();
music.setClass(Parametres.this, BackgroundMusic.class);
stopService(music);
musicBoolean = false;
// Resume the music player
buttonmusique.setText("Musique OFF"); //To change the text near to switch
Log.d("You are :", " Not Checked");
}
}
});
Put it in your create method: (+adapt to your situation)
final SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
CheckBox checkbox = (CheckBox) findViewById(R.id.myCheckBox);
checkbox.setChecked(sharedPreferences.getBoolean("mycheckbox", true));
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
sharedPreferences.edit().putBoolean("mycheckbox", b).apply();
}
});
You can save it in SharedPreferences.
SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
Save sound settings
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(getString(R.string.sound_enabled), isChecked);
editor.commit();
To get sound settings use:
boolean soundEnabled = prefs.getBoolean(getString(R.string.sound_enabled), false);
// Set switch state
buttonmusique.setChecked(soundEnabled);
Load status:
boolean switch_status = PreferenceManager.getDefaultSharedPreferences(ctx).getBoolean("switch_status", false);
Save status:
SharedPreferences.Editor sped = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
sped.putBoolean("switch_status", value);
sped.commit();
Use shared preferences or a database to store the state of your switch. It is essential that you depend on the lifecycle methods of Activity/fragment.
The following might help you:
......
.....
#Override
public void onClick(View v)
{
if (toggle.isChecked())
{
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("state_to_save", true);
editor.commit();
}
else
{
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("state_to_save", false);
editor.commit();
}
}
The final Code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
.......
......
SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
toggle.setChecked(sharedPrefs.getBoolean("state_to_save", true));
}
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 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...
I am able to save the state of a Single CheckBox but how do i save All the Five CheckBoxes in my Activity?
checkBox = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
Settings.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
}
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
For second checkbox do the following
boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
checkBox2.setChecked(isCheckedTwo );
checkBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
}
});
and similarly for the rest
Use a Arraylist to store the state of all the checkboxes
Put each one with different name. and remember the key comes first not the value. Call your save method each time with different name
I have implemented an application for save the properties of android content view.
I have used check box for enable and disable a editText.When user check the the check box then i am enabling edit text view and when user unchecked the check box then disabling edit text.Here if user check the check box then edit text view will be enable for enter data then the user click on save button then the edit field propertie as enabling will save if user re lunch the app
I have implemented code for it as follows:
((EditText)findViewById(R.id.editText1)).setEnabled(false);
((CheckBox)findViewById(R.id.checkBox1)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
((EditText)findViewById(R.id.editText1)).setEnabled(true);
}
else{
((EditText)findViewById(R.id.editText1)).setEnabled(false);
}
}
});
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//here how to save the properties of editText1 and checkBox1 upto re-launch
}
});
If user re open the application the modified content only will appear.
Please any body help me....
Try something like this:
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("editTextState", editText.isEnabled());
outState.putBoolean("checkBoxState", checkBox.isEnabled());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
editText.setEnabled(savedInstanceState.getBoolean("editTextState"));
checkBox.setEnabled(savedInstanceState.getBoolean("checkBoxState"));
}
}
Or you can use SharredPreferences.
In your Activity:
private EditText editText;
private CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText1);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editText.setEnabled(true);
} else {
editText.setEnabled(false);
}
}
});
loadProperties();
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveProperties();
}
});
}
private void saveProperties() {
SharedPreferences.Editor editor = getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE).edit();
editor.putBoolean("CHECK_BOX_STATE", checkBox.isChecked());
editor.putBoolean("EDITEXT_STATE_STATE", editText.isEnabled());
editor.commit();
}
private void loadProperties() {
SharedPreferences prefs = getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE);
checkBox.setChecked( prefs.getBoolean("CHECK_BOX_STATE", false));
editText.setEnabled( prefs.getBoolean("EDITEXT_STATE_STATE", false));
}
What you need to use is:onSaveInstanceState(android.os.Bundle)
This method makes it possibile to save for example all information of your controls.
Another possibility would be to use SharedPreferences
SharedPreferences settings = this.context.getSharedPreferences("myPreferences", 0);
// read stored value
String storedValue = settings.getString("aKey", null);
// store/override value
SharedPreferences.Editor editor = settings.edit();
editor.putString("aKey", "aValue");
editor.commit();