javaandroidsharedpreferencesandroid-preferencesedittextpreference

How To Get Reference of EditTextPreference in my MainActivity?


I am trying to take the value from my edittextpreference and change the font size of the textviews in my mainActivity layout. Please help me with this guys.I must be doing some silly mistake.Help would be appreciated.

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
    String str=sp.getString("editTextPref","50");

    if(sp!=null)
    {
        Toast.makeText(this,"String value is "+str,Toast.LENGTH_LONG).show();
        txt=(TextView) findViewById(R.id.textView);
        txt.setTextSize(Float.parseFloat(str));
    }
    Toast.makeText(this,"String value is :"+str,Toast.LENGTH_LONG).show();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int items=item.getItemId();

    switch(items)
    {
        case R.id.setting:
            Toast.makeText(this,"Settings Clicked",Toast.LENGTH_LONG).show();
            finish();
            Intent i=new Intent(this,Prefs.class);
           startActivity(i);
            break;
    }
    return super.onOptionsItemSelected(item);
}
}

Prefs.java

public class Prefs extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

    // show the current value in the settings screen
    for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++)     {
        initSummary(getPreferenceScreen().getPreference(i));
    }
}

@Override
protected void onPause() {
    super.onPause();
     getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    updatePreference(findPreference(key));
}

private void initSummary(Preference p) {
    if (p instanceof PreferenceCategory) {
        PreferenceCategory cat = (PreferenceCategory) p;
        for (int i = 0; i < cat.getPreferenceCount(); i++) {
            initSummary(cat.getPreference(i));
        }
    } else {
        updatePreference(p);
    }
}

private void updatePreference(Preference p)
{
    if(p instanceof EditTextPreference)
    {
        EditTextPreference editTextPreference=(EditTextPreference) p;
        p.setSummary(editTextPreference.getText());
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
    startActivity(new Intent(Prefs.this,MainActivity.class));
    Toast.makeText(this,"Pref activity finished",Toast.LENGTH_LONG).show();
}
}

Prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
    android:enabled="false"
    android:key="settingCategory"
    android:title="Settings"
    >
</PreferenceCategory>
<EditTextPreference
    android:id="@+id/editTextPref"
    android:dialogMessage="I am Dialog Message"
    android:dialogTitle="Dialog Title"
    android:hint="e.g 25"
    android:icon="@mipmap/ic_launcher"
    android:key="fontSize"
    android:padding="5dp"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:summary="Changes Font Size of Overall App"
    android:title="Change Font Size" />
</PreferenceScreen>

Solution

  • You should be reading value for key fontSize not the name of id field of EditTextPreference. Change your code in your MainActivity like this:

    SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
    String str=sp.getString("fontSize","50");