I'm trying to use SharedPreferences to store a preference, then change the default checked state of the menu item based on the SharedPreference. But it does not seem to be working. The menu choice stays until the app is closed. When I reload the app, the setting is back to the default, instead of the new SharedPreference setting.
public class MainActivity extends Activity {
boolean prefs = true;
String FILENAME = "settings";
String string;
public static final String LOG_TAG = "dbryant423";
public static final String PREFS_NAME = "settings";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(LOG_TAG, "prefs value: " +prefs);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String preference = settings.getString("preference", string);
if (preference == "us")
menu.findItem(R.id.menu_us).setChecked(true);
else if (preference == "metric")
menu.findItem(R.id.menu_metric).setChecked(true);
return true;
}
// called when an item is selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) { // decide which MenuItem was pressed based on it's id
case R.id.menu_us:
menuUS();
case R.id.menu_metric:
menuMetric();
}
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (prefs == true)
menu.findItem(R.id.menu_us).setChecked(true);
else if (prefs == false)
menu.findItem(R.id.menu_metric).setChecked(true);
return true;
}
public void menuUS() {
prefs = true;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("preference", "us");
editor.commit();
}
public void menuMetric() {
prefs = false;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("preference", "metric");
editor.commit();
}
public void calculateCylinder(View v) {
Intent calculateCylinderUS = new Intent(this, CalculateCylinder.class);
Intent calculateCylinderMetric = new Intent(this, CalculateCylinderMetric.class);
Log.v(LOG_TAG, "prefs value: " +prefs);
if (prefs == true)
startActivity(calculateCylinderUS);
else if (prefs == false)
startActivity(calculateCylinderMetric);
}
Please note that onPrepareOptionsMenu gets called just before the menu is displayed to the user.
Looking at your code you are calling setChecked(true) on R.id.menu_us and R.id.menu_metric based on local variable prefs whose value will always be true whenever the activity is created. So in order to persist these states we must update menu-items based on preference values and not based on values of local variable.
I think you'd rather modify your onPrepareOptionsMenuand onCreateOptionsMenu functions as below and give it a try:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String preference = settings.getString("preference", string);
if (preference == "us")
menu.findItem(R.id.menu_us).setChecked(true);
else if (preference == "metric")
menu.findItem(R.id.menu_metric).setChecked(true);
return true;
}
Don't compare string variables by == sign
here
if (preference == "us")
menu.findItem(R.id.menu_us).setChecked(true);
else if (preference == "metric")
menu.findItem(R.id.menu_metric).setChecked(true);
instead use .equals()
if (preference.equals("us"))
menu.findItem(R.id.menu_us).setChecked(true);
else if (preference.equals("metric"))
menu.findItem(R.id.menu_metric).setChecked(true);
I used a little bit from both answers, and I found some missing pieces myself. One of the main problems I figured out, is that I did not have a "break;" in my switch statement, and it was running both cases, no matter which one I selected. The other final fix was instead of calling
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String preference = settings.getString("preference", string);
if (preference.equals("us"))
menu.findItem(R.id.menu_us).setChecked(true);
else if (preference.equals("metric"))
menu.findItem(R.id.menu_metric).setChecked(true);
return true;
}
in which my if/else if statements are trying to modify the setChecked attribute, I realized all I needed to do was modify the value of "prefs" instead in the onCreateOptionsMenu. like so:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String preference = settings.getString("preference", string);
if (preference.equals("us"))
prefs = true;
else if (preference.equals("metric"))
prefs = false;
return true;
}
The onPrepareOptionsMenu still needs the "setChecked" in order to switch which choice has been selected.
here is the updated working code:
public class MainActivity extends Activity {
boolean prefs = true;
String FILENAME = "settings";
String string;
public static final String LOG_TAG = "dbryant423";
public static final String PREFS_NAME = "settings";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(LOG_TAG, "prefs value onCreate: " +prefs);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String preference = settings.getString("preference", string);
if (preference.equals("us"))
prefs = true;
else if (preference.equals("metric"))
prefs=false;
return true;
}
// called when an item is selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) { // decide which MenuItem was pressed based on it's id
case R.id.menu_us:
menuUS();
break;
case R.id.menu_metric:
menuMetric();
break;
}
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
String preference = settings.getString("preference", string);
if (preference.equals("us"))
menu.findItem(R.id.menu_us).setChecked(true);
else if (preference.equals("metric"))
menu.findItem(R.id.menu_metric).setChecked(true);
return true;
}
public void menuUS() {
prefs = true;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("preference", "us");
editor.commit();
Log.v(LOG_TAG, "prefs value menuUS: " +prefs);
}
public void menuMetric() {
prefs = false;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("preference", "metric");
editor.commit();
Log.v(LOG_TAG, "prefs value menuMetric: " +prefs);
}
public void calculateCylinder(View v) {
Intent calculateCylinderUS = new Intent(this, CalculateCylinder.class);
Intent calculateCylinderMetric = new Intent(this, CalculateCylinderMetric.class);
Log.v(LOG_TAG, "prefs value calculateCylinder: " +prefs);
if (prefs == true)
startActivity(calculateCylinderUS);
else if (prefs == false)
startActivity(calculateCylinderMetric);
}
Related
does not work switching the theme of the application from the settings menu. starts a theme from the "else" block
SharedPreferences sharedPrefs;
final String CURRENT_THEME = "CURRENT_THEME";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String theme = sharedPrefs.getString("CURRENT_THEME",null);
if(theme != null && theme.equals("light"))
{
setTheme(R.style.AppThemeLight);
}
else
{
setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.lightTheme:
SharedPreferences.Editor editor1 = sharedPrefs.edit();
editor1.putString(CURRENT_THEME, "light");
editor1.commit();
return true;
case R.id.darkTheme:
SharedPreferences.Editor editor2 = sharedPrefs.edit();
editor2.putString(CURRENT_THEME, "dark");
editor2.commit();
return true;}
Change the theme after selecting it from the options menu.
And use editor.apply() instead of editor.commit()
final String CURRENT_THEME = "CURRENT_THEME";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
changeTheme();
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.lightTheme:
SharedPreferences.Editor editor1 = sharedPrefs.edit();
editor1.putString(CURRENT_THEME, "light");
editor1.apply();
changeTheme();
return true;
case R.id.darkTheme:
SharedPreferences.Editor editor2 = sharedPrefs.edit();
editor2.putString(CURRENT_THEME, "dark");
editor2.apply();
changeTheme();
return true;}
private void changeTheme(){
String theme = sharedPrefs.getString(CURRENT_THEME,"light");
if(theme != null && theme.equals("light"))
{
setTheme(R.style.AppThemeLight);
}
else
{
setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
SharedPreferences sharedPrefs;
final String CURRENT_THEME = "CURRENT_THEME";
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
changeTheme();
setContentView(R.layout.activity_main);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.lightTheme:
SharedPreferences.Editor editor1 = sharedPrefs.edit();
editor1.putString(CURRENT_THEME, "light");
editor1.apply();
changeTheme();
return true;
case R.id.darkTheme:
SharedPreferences.Editor editor2 = sharedPrefs.edit();
editor2.putString(CURRENT_THEME, "dark");
editor2.apply();
changeTheme();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void changeTheme(){
String theme = sharedPrefs.getString(CURRENT_THEME,"light");
if(theme != null && theme.equals("light"))
{
setTheme(R.style.AppThemeLight);
}
else
{
setTheme(R.style.AppTheme);
}
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
}
So I am creating a simple android application for education purpose. I have a menu item which has 3 check boxes in it. As soon as my app starts, the user can select one or more from the given check boxes.but, I want to save the user selection for further reference. I achieved this for 1 of the check boxes using shared preferences but I am unable to achieve this if more than 1 check boxes are selected. Here is my code:
public class MainActivity extends AppCompatActivity {
static Switch swch;
static TextView onOffBar;
public static boolean red = false;
public static boolean blue = false;
public static boolean green = false;
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.red);
// MenuItem item1 = menu.findItem(R.id.blue);
// MenuItem item2 = menu.findItem(R.id.green);
item.setChecked(isChecked);
// item1.setChecked(isChecked);
// item2.setChecked(isChecked);
//return super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
switch (item.getItemId()) {
case R.id.red:
item.setChecked(!item.isChecked());
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.blue:
item.setChecked(!item.isChecked());
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.green:
item.setChecked(!item.isChecked());
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How do I achieve this for all the switch case options using shared preferences?
Thanks.
If this is wasn't your meant I apologize. But basically you are on the right track and you just need to enter in a different key for each boolean you want saved and to check. Also, its best practice to use a more descriptive key for the preferences, so instead of using something like "checkbox" that could apply to anything try using a descriptor so you can better keep track and remember what its for in the long run.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
switch (item.getItemId()) {
case R.id.red:
item.setChecked(!item.isChecked());
editor.putBoolean("red", item.isChecked());
editor.commit();
return true;
Case R.id.blue:
item.setChecked(!item.isChecked());
Blue1 = item.isChecked();
editor.putBoolean("blue", Blue1);
editor.commit();
return true;
case R.id.green:
item.setChecked(!item.isChecked());
Green1=item.isChecked();
editor.putBoolean("green", Green1);
editor.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can use Gson to store a Model Object.
public class CheckboxData {
boolean personalize_permissions;
boolean add_tanslucent_layer;
boolean integrate_flowInfo;
}
Add following in build.gradle
compile 'com.google.code.gson:gson:2.7'
Create an object of CheckboxData and populate it according to your checkbox selections.
CheckboxData data = new CheckBoxData();
data.setPersonalize_permissions(true);
data.setIntegrate_flowInfo(true);
Gson gson = new Gson();
String json = gson.toJson(data);
prefsEditor.putString("CheckBoxPrefs", json);
When retrieving
Gson gson = new Gson();
String json = mPrefs.getString("CheckBoxPrefs", "");
CheckboxData prefData = gson.fromJson(json, CheckboxData.class);
Set the selected checkboxes as per prefData values.
I am reposting this question since I am still stuck at this.
So I am creating a simple android application. I have a menu item which has 3 check boxes in it. As soon as my app starts, the user can select one or more from the given check boxes.but, I want to save the user selection for further reference. I achieved this for 1 of the check boxes using shared preferences but I am unable to achieve this if more than 1 check boxes are selected. Here is my code:
public class MainActivity extends AppCompatActivity {
static Switch swch;
public static boolean red = false;
public static boolean blue = false;
public static boolean green = false;
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.red);
// MenuItem item1 = menu.findItem(R.id.blue);
// MenuItem item2 = menu.findItem(R.id.green);
item.setChecked(isChecked);
// item1.setChecked(isChecked);
// item2.setChecked(isChecked);
//return super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
switch (item.getItemId()) {
case R.id.red:
item.setChecked(!item.isChecked());
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.blue:
item.setChecked(!item.isChecked());
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.green:
item.setChecked(!item.isChecked());
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How do I achieve this for all the switch case options using shared preferences?
Thanks.
Update your two methods - onCreateOptionsMenu and onOptionsItemSelected as shown below.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean red = settings.getBoolean("red", false);
boolean blue = settings.getBoolean("blue", false);
boolean green = settings.getBoolean("green", false);
MenuItem item = menu.findItem(R.id.color1);
MenuItem item1 = menu.findItem(R.id.color2);
MenuItem item2 = menu.findItem(R.id.color3);
item.setChecked(red);
item1.setChecked(blue);
item2.setChecked(green);
//return super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
switch (item.getItemId()) {
case R.id.color1:
item.setChecked(!item.isChecked());
editor.putBoolean("1", item.isChecked());
editor.commit();
return true;
case R.id.color2:
item.setChecked(!item.isChecked());
editor.putBoolean("2", item.isChecked());
editor.commit();
return true;
case R.id.color3:
item.setChecked(!item.isChecked());
editor.putBoolean("3", item.isChecked());
editor.commit();
return true;
case R.id.item_accessibility_services:
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have a list of data with two different views in my Fragment, my default view is Listview and another is Gridview. I switch between this two views by clicking on an icon on my toolbar.
And I set the switch item icon dynamically inside onOptionsItemSelected method like this:
//global variables
private int currentViewMode = 0 ;
static final int VIEW_MODE_LISTVIEW = 0;
static final int VIEW_MODE_GRIDVIEW = 1;
.
.
.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.switchView:
if (VIEW_MODE_LISTVIEW == currentViewMode){
item.setIcon(R.mipmap.ic_gridview);
currentViewMode = VIEW_MODE_GRIDVIEW;
}else {
item.setIcon(R.mipmap.ic_listview);
currentViewMode = VIEW_MODE_LISTVIEW;
}
switchView();
SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("currentViewMode",currentViewMode);
editor.commit();
return false;
default:
break;
}
return super.onOptionsItemSelected(item);
}
And I get my view from shared preferences like this:
SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",activity.MODE_PRIVATE);
currentViewMode = sharedPreferences.getInt("currentViewMode",VIEW_MODE_LISTVIEW);
But I don't know how can I save the item icon and how to retrive it.
Can you help me please?
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/switchView"
android:title=""
android:icon="#mipmap/ic_listview"
app:showAsAction="always"/>
</menu>
You can add another item inside your menu.xml for your gridView Then create a boolean that carries the visibility of this two items. when you switch to listview, set the visibility of gridItem to false, And when you switch to gridview, set the listItem to false; In this way you'll be able to save and retrieve the visibilities with shared preferences as a boolean.
Change your code like this:
<item
android:id="#+id/switchListView"
android:title=""
android:icon="#mipmap/ic_listview"
app:showAsAction="always"/>
<item
android:id="#+id/switchGridView"
android:title=""
android:icon="#mipmap/ic_gridview"
android:visible="false"
app:showAsAction="always"/>
Inside your Fragment:
boolean isGridView;
Then:
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
isGridView = myPrefs.getBoolean("menu_item", false);
And finally change your onOptionItemSelected method like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
final SharedPreferences.Editor editor = myPrefs.edit();
isGridView = myPrefs.getBoolean("menu_item", false);
switch (item.getItemId()) {
case R.id.switchListView:
if (VIEW_MODE_LISTVIEW == currentViewMode){
isGridView=true;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_gridview);
currentViewMode = VIEW_MODE_GRIDVIEW;
}else {
isGridView=false;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_listview);
currentViewMode = VIEW_MODE_LISTVIEW;
}
switchView();
SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
SharedPreferences.Editor et = sharedPreferences.edit();
et.putInt("currentViewMode",currentViewMode);
et.commit();
return false;
case R.id.switchGridView:
if (VIEW_MODE_LISTVIEW == currentViewMode){
isGridView=true;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_gridview);
currentViewMode = VIEW_MODE_GRIDVIEW;
}else {
isGridView=false;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_listview);
currentViewMode = VIEW_MODE_LISTVIEW;
}
switchView();
sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
et = sharedPreferences.edit();
et.putInt("currentViewMode",currentViewMode);
et.commit();
return false;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
if(isGridView==true){
menu.findItem(R.id.switchListView).setVisible(false);
menu.findItem(R.id.switchGridView).setVisible(true);
}else{
menu.findItem(R.id.switchListView).setVisible(true);
menu.findItem(R.id.switchGridView).setVisible(false);
}
super.onPrepareOptionsMenu(menu);
}
Important note
How to implement optionMenu inside Fragments:
1. If you have implemented optionMenu inside your MainActivity, then you should return false in your MainActivity's onOptionsItemSelected, otherwise your Fragment's optionMenu will not work.
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
//
}
return false;
}
2. you have to setHasOptionsMenu(true); inside your onCreate in your Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
In the preferences I would like to change the preference view according the Unit type(Imprial or metric). On the create method I am checking what was the last value of the
measurement_unit preference, but always return metric on the app startup.
I also have a OnSharedPreferenceChangeListener where I am changing the view according the user entry, which is working. How can I get the saved preference when the onCreate is called?
public class PrefMainActivity extends PreferenceActivity{
String TAG="BlueGlucose";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(R.string.action_settings);
this.init_view();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
PrefMainActivity.this.init_view();
}
// your stuff here
};
private void init_view(){
try
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String metric = getResources().getString(R.string.imperial);
if (prefs.getString("measurement_unit",metric) == metric)
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefMainFragmentImperial()).commit();
else
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefMainFragmentMetric()).commit();
prefs.registerOnSharedPreferenceChangeListener(spChanged);
}
catch(Exception ex)
{
}
}
if (prefs.getString("measurement_unit",metric) == metric)
String in java need to be compared by equals or equalsIgnoreCase