king_below_my_lord
king_below_my_lord

Reputation: 682

How to save and apply user settings using ListPreferences?

What I am actually trying to do is change the layout and text background color once the user select the appropriate option from my ListPreference

public class PrefFragment extends PreferenceFragment implements OnPreferenceChangeListener {

private static final String PREF_THEME="themeset";
ListPreference theme_selector;
  @Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
       addPreferencesFromResource(R.xml.preferences);

       theme_selector = (ListPreference) findPreference(PREF_THEME); 
       theme_selector.setOnPreferenceChangeListener(this);      
        };
    public boolean onPreferenceChange(Preference preference, Object newValue) {
               return true;
             }
        }

that is my Prefernce Fragment

this is my preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<ListPreference
        android:key="themeset"
        android:entries="@array/themeselection"
        android:summary="@string/pref_theme_summary" 
        android:entryValues="@array/themeValues"
        android:title="@string/pref_themes" 
        android:defaultValue="1"/>

this is my array.xml

<resources>
   <string-array name="themeselection">
        <item name="Black">Black</item>
        <item name="White7">White</item>
   </string-array> 
   <string-array name="themeValues">
        <item name="Black">1</item>
        <item name="White">2</item>
   </string-array>
</resources>

the below is my Main Activity

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {

    private int theme_setting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout theme =(RelativeLayout) findViewById(R.id.layout);
        TextView txtcolor = (TextView) findViewById(R.id.txt);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);

        theme_setting = Integer.parseInt((prefs.getString("themeset", "1")));

        if (theme_setting == 1)
        {
            theme.setBackgroundColor(Color.parseColor("#000000"));
            txtcolor.setTextColor(Color.parseColor("#FFFFFF"));
            this.recreate();
        }
          else
          {
            theme.setBackgroundColor(Color.parseColor("#FFFFFF"));
            txtcolor.setTextColor(Color.parseColor("#000000"));
            this.recreate();  
          }
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        if (arg1.equals("themeValues")) 
        {
            theme_setting = Integer.parseInt((arg0.getString("themeset", "1")));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.action_settings:
            Intent i = new Intent(this, PrefSummary.class);
            startActivityForResult(i,1);
            break;
        }
        return true;
        }
        }

So as I said before all I want to do is just change the theme by programatically setting/changing the layout background color and font color, and for the above code I am getting a frozen white app screen as if the layout isn't loading. I realize that i have done something horribly wrong mainly in the main activity and I would like some help in getting this to work.

Upvotes: 1

Views: 528

Answers (1)

king_below_my_lord
king_below_my_lord

Reputation: 682

I realized that the "this.recreate" code was causing an infinite loop and because of that I wrote a lot of crap code, thinking it was the fault with the Preference Settings, Below I will show a little more cleaner and working code:

Modified Preference Fragment:

public class PrefFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
           addPreferencesFromResource(R.xml.preferences);};}

In the main activity

1.Removed or Commented "this.recreate()" in if...else.

2.Modified

public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        if (arg1.equals("themeValues")) 
        {
            theme_setting = Integer.parseInt((arg0.getString("themeset", "1")));
        }

to

public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        this.recreate();//So the recreate function will be called only once and only when a change is detected.
    }

Finally the answer here is not for the asked question(because there was no problem there in the beginnning), the culprit was the recreate() function and not in the usage of SharedPreferences or ListPreferences.

Upvotes: 2

Related Questions