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);
}
}
Related
I'm here to see if they guide me in this problem
I have a menu, with an item, and what I want is that item to work as a button, if you click on it change the icon and a toast that says Notification active, and if you click again, change the icon and say using toast, Disabled notifications.
All this I have achieved, the detail is that when activating the button and leaving the activity and when entering again, the button is again deactivated, when it should be activated
I hope you understand, here I give you the code, which makes the function perfect, but I think I must do something with shared preferences to save if it is clicked or not, but I do not know where to start, I hope you understand and guide me, I leave the code that I have implemented
----- xml 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:icon="#drawable/ic_notifications_off_black_24dp"
android:title="#string/notificaciones"
android:id="#+id/notificaciones"
android:checkable="true"
app:showAsAction="always"
/>
</menu>
--------- Java---------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menucampeonatos, menu);
MenuItem item = menu.findItem(R.id.notificaciones);
item.setChecked(false);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.notificaciones);
checkable.setChecked(false);
return super.onPrepareOptionsMenu(menu);
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.notificaciones:
if (item.isChecked()){
myRef.update("notificacion", false);
item.setChecked(false);
item.setIcon(R.drawable.ic_notifications_off_black_24dp);
Toast.makeText(this, "Notificaciones Desactivadas para este campeonato", Toast.LENGTH_LONG).show();
}else{
myRef.update("notificacion", true);
item.setChecked(true);
item.setIcon(R.drawable.ic_notifications_active_black_24dp);
Toast.makeText(this, "Notificaciones Activadas para este campeonato", Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
First you need to implement Shared Preferences, as you mentioned
private String sharedPrefFile = "your Shared Preferences Name";
private String NOTIFICATION_KEY = "notification_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE);
...
}
Then, in your onOptionsItemSelected, you need to save user action to preference
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.notificaciones:
if (item.isChecked()){
// Add this line
SharedPreferences.Editor preferencesEditor = mPreferences.edit();
preferencesEditor.putBoolean(NOTIFICATION_KEY, true);
preferencesEditor.apply();
myRef.update("notificacion", false);
item.setChecked(false);
item.setIcon(R.drawable.ic_notifications_off_black_24dp);
Toast.makeText(this, "Notificaciones Desactivadas para este campeonato", Toast.LENGTH_LONG).show();
}else{
// Add this line
SharedPreferences.Editor preferencesEditor = mPreferences.edit();
preferencesEditor.putBoolean(NOTIFICATION_KEY, false);
preferencesEditor.apply();
myRef.update("notificacion", true);
item.setChecked(true);
item.setIcon(R.drawable.ic_notifications_active_black_24dp);
Toast.makeText(this, "Notificaciones Activadas para este campeonato", Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
Lastly, load the preference everytime activity is destroyed in onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.notificaciones);
String isNotificationClicked = mPreferences.getBoolean(NOTIFICATION_KEY, false);
checkable.setChecked(isNotificationClicked);
return super.onPrepareOptionsMenu(menu);
}
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 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);
}
How can I set initial value of checkbox item part of a menu? When I start an Activity I want to set a boolean value saved in Shared Preferences.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity">
<item
android:id="#+id/checkbox"
android:title="#string/checkbox"
android:orderInCategory="10"
android:showAsAction="never"
android:visible="true"
android:checkable="true"
android:checked="true"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.checkbox){
if(item.isChecked()){
item.setChecked(false);
}else{
item.setChecked(true);
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PrefsConsts.CHECKBOX,item.isChecked());
editor.commit();
}
return true;
}
I can always know what was the value, but I donĀ“t know how to set the new one.
First of all, get rid of the checked value in the layout
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
Now you can set it in the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_check) {
item.setChecked(!item.isChecked());
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
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);
}