title: How do I get preferences to work in Android?
body: <p>I've really been struggling through this. New to Java/Android. I'm writing my first app and this is the first thing that has taken me longer than a couple days of searching to figure out. Here's the setup: It's a BAC calculator / drink counter:</p>
  
  <p><img src="https://i.imgur.com/MSSf0.jpg" alt="alt text"></p>
  
  <p>A formula is used to calculate the BAC. Here's the forumla:</p>
  
  <pre><code>Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
  </code></pre>
  
  <p>So as you can see, being able to modify the gender and weight will produce more accurate and personalized results. So I have them as doubles:</p>
  
  <pre><code>double GenderConstant = 7.5; //9 for female
  double Weight = 180;
  </code></pre>
  
  <p>To change these variables I would like the person to be able to go into the settings and choose different values. I have these things set up, but not linked to the variables shown above because I cannot for the life of me figure out how. Here they are:</p>
  
  <p><img src="https://i.imgur.com/YapQF.jpg" alt="alt text"></p>
  
  <p>I press the menu button and this pops up. Great. I'll click Settings.</p>
  
  <p><img src="https://i.imgur.com/xftjv.jpg" alt="alt text"></p>
  
  <p>Now the preferences pops up. Here is my preferences.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
   &lt;PreferenceCategory android:title="Personal Settings"&gt;
  
  
   &lt;ListPreference
   android:title="Gender"
   android:summary="Verify or deny the presence of a Y chromosome."
   android:key="genderPref"
   android:defaultValue="male"
   android:entries="@array/genderArray"
   android:entryValues="@array/genderValues" /&gt;
  
   &lt;ListPreference
   android:title="Weight"
   android:summary="How much the planet pulls on you, in pounds."
   android:key="weightPref"
   android:defaultValue="180"
   android:entries="@array/weightArray"
   android:entryValues="@array/weightValues" /&gt;
  
   &lt;/PreferenceCategory&gt;
  
  &lt;PreferenceCategory android:title="Drink Settings"&gt;
  
   &lt;ListPreference
   android:title="Beer Size"
   android:summary="The volume of your beer, in ounces."
   android:key="beerPref"
   android:defaultValue="12"
   android:entries="@array/beerArray"
   android:entryValues="@array/beerValues" /&gt;
  
   &lt;ListPreference
   android:title="Shot Size"
   android:summary="The volume of your shot, in ounces."
   android:key="shotPref"
   android:defaultValue="1.5"
   android:entries="@array/shotArray"
   android:entryValues="@array/shotValues" /&gt;
  
   &lt;ListPreference
   android:title="Wine Size"
   android:summary="The volume of your wine, in ounces."
   android:key="winePref"
   android:defaultValue="5"
   android:entries="@array/wineArray"
   android:entryValues="@array/wineValues" /&gt;
  
  
   &lt;/PreferenceCategory&gt;
   &lt;/PreferenceScreen&gt;
  </code></pre>
  
  <p>Onward to the weight ListPreference:</p>
  
  <p><img src="https://i.imgur.com/sEQfk.jpg" alt="alt text"></p>
  
  <p>And that shows up. The values are stored as string-arrays in res/values/arrays.xml. Here's a sample, of just the weight ones:</p>
  
  <pre><code>&lt;string-array name="weightArray"&gt;
  &lt;item&gt;120 lbs&lt;/item&gt;
  &lt;item&gt;150 lbs&lt;/item&gt;
  &lt;item&gt;180 lbs&lt;/item&gt;
  &lt;item&gt;210 lbs&lt;/item&gt;
  &lt;item&gt;240 lbs&lt;/item&gt;
  &lt;item&gt;270 lbs&lt;/item&gt;
   &lt;/string-array&gt;
   &lt;string-array name="weightValues"&gt;
  &lt;item&gt;120&lt;/item&gt;
  &lt;item&gt;150&lt;/item&gt;
  &lt;item&gt;180&lt;/item&gt;
  &lt;item&gt;210&lt;/item&gt;
  &lt;item&gt;240&lt;/item&gt;
  &lt;item&gt;270&lt;/item&gt;
   &lt;/string-array&gt;
  </code></pre>
  
  <p>This is basically as far as I've gotten. I can click a value, sure, but it doesn't change the formula because it's not linked with the doubles I created in DrinkingBuddy.java. All of the stuff displayed in the settings are just empty shells for now, including the spinner on the main layout (the default time is just set to 1 hour)</p>
  
  <p>I did create a Preferences.java and have tried implementing various combinations of code found in tutorials and resources around the web, but to no avail. Here it is anyway, filled with failed attempts to make beerPref (the settings option to change how many ounces in the beer) correlate with a variable in my main class:</p>
  
  <pre><code>package com.dantoth.drinkingbuddy;
  
  
   import android.app.Activity;
   import android.content.SharedPreferences;
   import android.os.Bundle;
   import android.preference.Preference;
   import android.preference.PreferenceActivity;
   import android.preference.Preference.OnPreferenceClickListener;
  
  
   public class Preferences extends PreferenceActivity {
  
  public static final String PREF_BEER_SIZE = "PREF_BEER_SIZE";
  
  @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   addPreferencesFromResource(R.xml.preferences);
  
   //Get the custom preference
  
  
  
   Preference beerPref = (Preference) findPreference("beerPref");
   beerPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  
   public boolean onPreferenceClick(Preference preference) {
  
   SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
   SharedPreferences.Editor editor = customSharedPreference.edit();
  
   editor.commit();
   return true;
   }}
  
   );}
   }
  </code></pre>
  
  <p>A full on tutorial and sample code would be AWESOME as I've yet to find any reliable guides out there. </p>