I have a toggle that change the brightness in my device from manual to authomatic. It works but the state of button doesn't save.. There are two things i need right now.
1) Save the button state using sharedpreferences
2) Check when i open the application which kind of brightness there is in the phone.
This is the toggle in my onCreate:
autoBrightToggle = (ToggleButton)v.findViewById(R.id.luminosita);
autoBrightToggle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (autoBrightToggle.isChecked()) {
setAutoBrightness(true);
} else {
setAutoBrightness(false);
}
}
});
and the method:
void setAutoBrightness(boolean value) {
if (value) {
Settings.System.putInt(getActivity().getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {
Settings.System.putInt(getActivity().getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
}
}
i tryied in this way but not works:
sPrefdata = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.brightoggle); //Dichiaro il toggle
boolean togglebrightness = sPrefdata.getBoolean("DATA", false); a
if (togglebrightness ) //if (tgpref) may be enough, not sure
{
autoBrightToggle .setChecked(true);
}
else
{
autoBrightToggle .setChecked(false);
}
and so in the onClick
SharedPreferences sPref = getSharedPreferences(PREFS_NAME, 0);
Editor editor = sPref.edit();
editor.putBoolean("DATA", true); //or false
editor.apply();
but doesn't work. Doesn't save the state and the method stops works. How can i solve? And how can i check which is the actual brightness?
Try the snippet given below, I've used it to save strings in shared preferences.
SharedPreferences.Editor ed = getSharedPreferences("DATA", 0).edit();
ed.putBoolean("DATA", true);
ed.commit();
Related
I am unable to save a boolean value using SharedPreferences. The value is always true for some reason. This is how the value is saved:
settings = getSharedPreferences("UserConfigs", MODE_PRIVATE);
b1 = settings.getBoolean("Gravity", false);
editor = settings.edit();
action_G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!b1) {
action_G.setImageResource(R.drawable.ic_align_right);
txt.setGravity(Gravity.RIGHT);
editor.putBoolean("Gravity", true);
editor.commit();
} else {
action_G.setImageResource(R.drawable.ic_align_center);
txt.setGravity(Gravity.CENTER);
editor.putBoolean("Gravity", false);
editor.commit();
}
b1 = !b1;
}
});
What is wrong?
EDIT:
change code but pref not saving ?
settings = getSharedPreferences("UserConfigs", MODE_PRIVATE);
editor = settings.edit();
action_G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (settings.getBoolean("Gravity", false)) {
editor.putBoolean("Gravity", false).apply();
action_G.setImageResource(R.drawable.ic_align_center);
txt.setGravity(Gravity.CENTER);
} else {
editor.putBoolean("Gravity", true).apply();
action_G.setImageResource(R.drawable.ic_align_right);
txt.setGravity(Gravity.RIGHT);
}
}
});
If you need to use a variable from the context of your activity in an anonymous class it needs to be declared final.
Therefore, the b1 variable that is initialized before the onClickListener is not changed after the onClickListener is called.Since, the reference to the inner b1 is not the same as the global b1.
So, Instead of using b1 use settings.getBoolean("Gravity", false); directly.
It happens because on every click in the end you convert your b1 into !b1(b1 != b1) means on every click in there are "Gravity" is saved only as "true".
I'm using DefaultSharedPrefeneces to save and load the state of the checkboxes in my navigation drawer.
when the navigation drawer is created and the state of the checkboxes are loaded from the DefaultPreferences, if the booleans for each checkbox are true then the checkboxes are set to checked.
when the user clicks on a checkbox the state then get saved to the defaultsharedPrefences.
I have no problem with all this.
the problem is that then i try to put 2 booleans into the defaultsharedprefences before the commit it doesn't work and appears to overwrite the first boolean.
Here is my code
//Navigation Drawer
private void addDrawerItems() {
String[] osArray = {"item1", "item2", "item3"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);
if (mIsPremiumUser) {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
} else {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
mDrawerList.setAdapter(mAdapter);
Boolean isCheckedValue;
// *** THIS IS WHERE I LOAD THE CHECKBOX STATE FROM DEFAULTSHAREDPREFERENCES ***
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
isCheckedValue = preferences.getBoolean("cbox1", false);
mDrawerList.setItemChecked(0, isCheckedValue);
isCheckedValue = preferences.getBoolean("cbox2", true);
mDrawerList.setItemChecked(1, isCheckedValue);
isCheckedValue = preferences.getBoolean("cbox3", true);
mDrawerList.setItemChecked(2, isCheckedValue);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
if (!mIsPremiumUser) {
Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
return;
}
switch (position) {
case 0:
if (ctv.isChecked()) {
requestActivityUpdates();
Toast.makeText(getApplicationContext(), "item1ON", Toast.LENGTH_SHORT).show();
// THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox1", ctv.isChecked());
editor.commit();
} else {
removeActivityUpdates();
Toast.makeText(getApplicationContext(), "item1OFF", Toast.LENGTH_SHORT).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox1", false);
editor.commit();
}
break;
case 1:
if (ctv.isChecked()) {
Toast.makeText(getApplicationContext(), "item2 ON", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
// THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
editor.putBoolean("cbox2", ctv.isChecked());
editor.putBoolean("callStatus", ctv.isChecked()); // << THIS LINE IS CAUSING ME PROBLEMS
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "item2 OFF", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox2", ctv.isChecked());
editor.putBoolean("callStatus", ctv.isChecked());
editor.commit();
}
break;
case 2:
if (ctv.isChecked()) {
Toast.makeText(getApplicationContext(), "item3 ON", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox3", ctv.isChecked());
editor.putBoolean("smsStatus", ctv.isChecked());
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "item3 OFF", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox3", ctv.isChecked());
editor.putBoolean("smsStatus", ctv.isChecked());
editor.commit();
}
break;
}
}
});
}
I know for sure that it works for one putBoolean on each item clicked, but doesn't when I've two putBoolean within the same if/else before a editor.commit .
any suggestions on whats causing the problem and how to fix it ? would be greatly appreciated it.
I figured it out!
as the boolean were being stored in an external defaultSharedPrefenences file,
outside of the app the values were always being read in from that.
the state would be pulled the last time i clicked on the check boxes.
I was thinking that each time I i force closed the app it would go back to the default selections states that i wanted which was false,true,true. but as the default Sharedpreferences was not destroyed when the app was it would load in the most recent state.
I had to clear the app data in the application manager in order for it set the boolean back to null, and display false,true,true.
I have a toggleButton in my custom Dialog. It shows "Sound On" on default. If I press the Button it changes to "Sound Off". If i close dialog and reopen it shows "Sound On".I just want to save the pressed state. I used sharedPreferences there but it still does not work as expected. I allready read some threads about this but im still not able to fix this by myself. Could someone have a quick look and show me my mistake.
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String name = sharedpreferences.getString("sound", "no");
if (name.equals("no")) {
final ToggleButton buttonSound = (ToggleButton) dialog.findViewById(R.id.button3);
buttonSound.isChecked();
}
final ToggleButton buttonSound = (ToggleButton) dialog.findViewById(R.id.button3);
buttonSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(buttonSound.isChecked()){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("sound", "no");
editor.commit();
}else if(!buttonSound.isChecked()){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("sound", "yes");
editor.commit();
}
}
});
I have a code... Its excellent save data and load data, but... When i reset application, my score loading, but when i click button for +5 score, my score reset and set 5. I am want that addition +5, but its dont work...
I understand that the problem of addition, because save and load working excellent, but addition doesnt work.
Sorry for my bad English :)
int mCounts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appli);
Settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
score = (TextView) findViewById(R.id.score);
score.setText(String.valueOf(mCounts));
}
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
}
public void onPause() {
super.onPause();
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
Try this code in your button:
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
Problem is here:
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
Remove int. it will work
I am trying to make an android app that allows users to block apps for a specific period of time. So I have a listview of installed apps in a fragment with a switch button next to it.
I want it to stay checked when the user checks its or unchecks it after they press back and exit the app.
I am trying to achieve this using setOnCheckedChangeListener and Shared Preferences; however; I am having trouble saving the button state in my BaseAdapter class.
holder.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (holder.ck1.isChecked()) {
//itemChecked[position] = true;
b = true;
holder.ck1.setChecked(b);
Log.i("This is", " checked: " + position);
SharedPreferences.Editor editor = context.getSharedPreferences("com.ibc.android.demo.appslist.app", Context.MODE_PRIVATE).edit();
editor.putBoolean("checkBox1", b);
editor.commit();
} else {
//itemChecked[position] = false;
b= false;
holder.ck1.setChecked(b);
Log.i("This is", " not checked: " + position);
SharedPreferences.Editor editor = context.getSharedPreferences("com.ibc.android.demo.appslist.app", Context.MODE_PRIVATE).edit();
editor.putBoolean("checkBox1", b);
editor.commit();
}
}
});
sharedPrefs = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE);
holder.ck1.setChecked(sharedPrefs.getBoolean("checkBox1",false));
// I think that maybe instead of false I should put the boolean b I defined in the method but I am not sure how to get it .
return convertView;
}
How I do I modify this to reach to desired result?
I think whenever the activity created it automatically calls onCheckedChange and your SharedPreferences overwrite by default values. so I think you can use onStop to update the SharedPreferences.
holder.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
{
SharedPreferences.Editor editor = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE).edit();
editor.putBoolean(pakagename, b);
editor.commit();
}
});
sharedPrefs = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE);
holder.ck1.setChecked(sharedPrefs.getBoolean(pakagename,false));
return convertView;
}
pakagename holds the package name of that particular listitem. Otherwise if you are targeting above api level 11, you can use a set in sharedpreferences to store all the checked package names. This is the easy way. Otherwise you can save the list of checked applications in a database.
I have achieved the requested feature. Check my code:
final SharedPreferences sharedPreferences = getSharedPreferences("conditionCheck", MODE_PRIVATE);
switchCompat.setChecked(sharedPreferences.getBoolean("switchCondition",false));
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final SharedPreferences sharedPreferences = getSharedPreferences("conditionCheck", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (switchCompat.isChecked()) {
Toast.makeText(getApplicationContext(), "Checked Condition True", Toast.LENGTH_LONG).show();
editor.putBoolean("switchCondition",switchCompat.isChecked());
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "Checked Condition False", Toast.LENGTH_LONG).show();
editor.putBoolean("switchCondition",switchCompat.isChecked());
editor.commit();
}
}
});