Results 1 to 3 of 3
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Why doesn't my PreferenceManager.setDefaultValues() save my preferences?

    I have written this small example application to demonstrate the problem. I am following this guide to learn about using Preferences in Android.

    The problem is that in the following code of MainActivity.java, I have saved the default values of preferences on Line # 17, and then checked it on Line # 18 and Line #19, and it logs, "Preference Value: Prefence was not saved probably!". Then I have checked it on Line # 60 and 61, and it sets the text of TextView to "Preference Value: Prefence not found".

    So what am I missing?

    I am also attaching an Eclipse project in .rar format. This is a small example project to demonstrate the problem.

    MainActivity.java:

    Java Code:
    public class MainActivity extends FragmentActivity implements DrawerLayout.DrawerListener, ListView.OnItemClickListener {
    	private static final String TAG = MainActivity.class.getSimpleName();
    	
    	private final String [] drawerListItems = {"Settings"};
    	
    	private DrawerLayout drawerLayout;
    	private FrameLayout frameLayout;
    	private ListView drawerListView;
    	
    	private TextView textView;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);//check
    		Log.i(TAG, "Preference Value: " + sharedPreferences.getString("preferences_editTextPreference_key", "Prefence was not saved probably!") );//check
    		
    		drawerListView = (ListView) findViewById(R.id.mainActivity_listView);
    		drawerListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drawerListItems));
    		drawerListView.setOnItemClickListener(this);
    		
    		drawerLayout = (DrawerLayout) findViewById(R.id.mainActivity_drawerLayout);
    		drawerLayout.setDrawerListener(this);
    		
    		frameLayout = (FrameLayout) findViewById(R.id.mainActivity_frameLayout);
    		
    		textView = (TextView) findViewById(R.id.mainActivity_textView);
    	}
    
    	
    	/****************************************************************************************************************************************/
    	/************************************************DrawerLayout.DrawerListener IMPLEMENTATION**********************************************/
    	/****************************************************************************************************************************************/
    	@Override
    	public void onDrawerClosed(View arg0) {
    		frameLayout.setBackgroundColor(Color.parseColor("#6A0888")); /* Dark Purple Color */
    	}
    
    	@Override
    	public void onDrawerOpened(View arg0) {
    		frameLayout.setBackgroundColor(Color.parseColor("#FE642E")); /* Orange Color */
    	}
    
    	@Override
    	public void onDrawerSlide(View arg0, float arg1) {}
    
    	@Override
    	public void onDrawerStateChanged(int arg0) {}
    
    	/****************************************************************************************************************************************/
    	/***************************************************OnDrawerClickListener IMPLEMENTATION*************************************************/
    	/****************************************************************************************************************************************/
    	@Override
    	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    		frameLayout.setBackgroundColor(Color.parseColor("#F7FE2E")); /* Yellow Color */
    		
    		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    		textView.setText("Preference Value: " + sharedPreferences.getString("preferences_editTextPreference_key", "Prefence not found!") );
    		
    		drawerListView.setItemChecked(position, true);
    		drawerLayout.closeDrawer(drawerListView);
    		
    	}
    }
    MyPreferenceActivity.java:

    Java Code:
    public class MyPreferenceActivity extends PreferenceActivity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		//Add preferences from the xml file to this activity
    		addPreferencesFromResource(R.xml.preferences);
    	}
    	
    	
    }

    res/layout/activity_main.xml:

    Java Code:
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        android:id="@+id/mainActivity_drawerLayout"
        
        tools:context="${relativePackage}.${activityClass}"
        
        android:background="#FE2E2E" > <!-- Red Color -->
        
        <FrameLayout
        	android:id="@+id/mainActivity_frameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#2E2EFE" > <!-- Electric Blue Color -->
            <TextView 
                android:id="@+id/mainActivity_textView"
                android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="@string/mainActivity_textView"
            	android:background="#00BFFF"/> <!-- Ligter Blue Color -->
        </FrameLayout>
            
        <ListView
        	android:id="@+id/mainActivity_listView"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#868A08" /> <!-- Bottle Green Color -->
    
    </android.support.v4.widget.DrawerLayout>
    res/xml/preferences.xml:

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
        <EditTextPreference 
            android:title="@string/preferences_editTextPreference_title"
            android:summary="@string/preferences_editTextPreference_summary"
            android:key="preferences_editTextPreference_key"
            android_defaultValue="EditText Preference Default" />
    </PreferenceScreen>
    res/values/strings.xml:

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Minimal Reading Preferences</string>
        <string name="mainActivity_textView">I am a Text View.</string>
        <string name="title_activity_my_preference">MyPreferenceActivity</string>
        
        <string name="preferences_editTextPreference_title">CheckBox Preference Title</string>
        <string name="preferences_editTextPreference_summary">CheckBox Preference Summary</string>
    
    </resources>
    Attached Files Attached Files

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why doesn't my PreferenceManager.setDefaultValues() save my preferences?

    I've tried many combinations and am unable to get your code to work.
    Please let us know if you find a solution.

    I use SharedPreferences in several programs without any problems.

    Try writing a very simple Activity that reads the preferences and shows them in a text view and does Nothing more.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Why doesn't my PreferenceManager.setDefaultValues() save my preferences?

    I've tried many combinations and am unable to get your code to work.
    Please let us know if you find a solution.
    I could not find out why this one is not working. Thank you very much for your time though.

    Like
    If you don't understand my response, don't ignore it, ask a question.
    Quick reply to this message Reply Reply With Quote Reply With Quote Multi-Quote This Message Blog this Post
    Thank you for that. I wrote the following application, and it works.

    MainActivity.java

    Java Code:
    public class MainActivity extends Activity {
    	private final String TAG = MainActivity.class.getSimpleName();
    	private TextView textView;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		
    		setContentView(R.layout.activity_main);
    		textView = (TextView) findViewById(R.id.mainActivity_textView);
    		
    		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    		
    		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    		
    		String defaultValue = sharedPreferences.getString("preferences_editTextPreference_key", "Preference not found!");
    		textView.setText(defaultValue);//check 
    		Log.i(TAG, "Default Value: " + defaultValue);//check
    	}
    }
    res/layout/activity_main.xml

    Java Code:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${relativePackage}.${activityClass}" >
    
        <TextView
            android:id="@+id/mainActivity_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    res/xml/preferences.xml

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
        <EditTextPreference
            android:title="@string/preferences_editTextPreference_title"
            android:summary="@string/preferences_editTextPreference_summary"
            android:key="preferences_editTextPreference_key"
            android:defaultValue="preferences_editTextPreference_defaultValue" />
    
    </PreferenceScreen>
    strings.xml

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Minimal Reading Preferences</string>
        <string name="mainActivity_textView">I am a Text View.</string>
        <string name="title_activity_my_preference">MyPreferenceActivity</string>
        
        <string name="preferences_editTextPreference_title">CheckBox Preference Title</string>
        <string name="preferences_editTextPreference_summary">CheckBox Preference Summary</string>
    
    </resources>

Similar Threads

  1. save on exit doesn't work
    By ras_oscar in forum New To Java
    Replies: 13
    Last Post: 10-25-2013, 08:13 PM
  2. Replies: 4
    Last Post: 04-02-2013, 05:01 PM
  3. Storing preferences
    By kiloran in forum New To Java
    Replies: 3
    Last Post: 01-10-2013, 02:51 PM
  4. Shared Preferences
    By johnsonjp34 in forum Android
    Replies: 0
    Last Post: 12-29-2012, 07:23 PM
  5. Failure of Preferences API under OSX
    By JerryAgin in forum Advanced Java
    Replies: 2
    Last Post: 02-29-2012, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •