I have tried to do some codes in my onPreferenceClick but it didn't work.
I made a SettingActivity Calss and I extends the PreferenceActivity.
Then I implements the OnPreferenceClickListener.
Here is my Setting Activity Class
DecimalFormat df = new DecimalFormat("@##,###,###,###");
private Calculation mCalculation;
private TextView mCalculatorDisplay;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@SuppressWarnings("deprecation")
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals("Point")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(",", "."));
} else if (preference.getKey().equals("Comma")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(".", ","));
} else if (preference.getKey().equals("Space")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(",", " "));
} else if (preference.getKey().equals("Off")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(" ", ""));
getPreferenceManager().getSharedPreferences().edit()
.putInt("unreadcount", 0).commit();
}
return true;
}
Here is my arrays.xml
<string-array name="decimalNumber">
<item name="P">Point</item>
<item name="C">Comma</item>
<item name="S">Space</item>
<item name="O">Off</item>
</string-array>
<string-array name="decimalNumberValues">
<item name="P">P</item>
<item name="C">C</item>
<item name="S">S</item>
<item name="O">O</item>
</string-array>
And settings.xml in xml folder:
<PreferenceCategory android:title="@string/pref_user_settings" >
<ListPreference
android:entries="@array/decimalNumber"
android:entryValues="@array/decimalNumberValues"
android:key="prefdecimalNumber"
android:summary="@string/number_format_summary"
android:title="@string/number_format" />
</PreferenceCategory>
And here is a photo of my User Settings:
What I want to do is by clicking on each one of them change the way of decimal number formats.
Thanks in Advance :)
Update: I did this to register the PreferenceClickListener.
@SuppressWarnings("deprecation")
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(
(OnSharedPreferenceChangeListener) this);
}
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(
(OnSharedPreferenceChangeListener) this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("Point")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult())
.replaceAll(",", "."));
} else if (key.equals("Comma")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult())
.replaceAll(".", ","));
} else if (key.equals("Space")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult())
.replaceAll(",", " "));
} else if (key.equals("Off")) {
mCalculatorDisplay.setText(df.format(mCalculation.getResult())
.replaceAll(" ", ""));
}
}
Its giving me this error:
05-24 11:10:24.127: E/AndroidRuntime(1025): java.lang.RuntimeException: Unable to resume activity {com.com.ecalc/com.com.ecalc.Z_SettingsActivity}: java.lang.ClassCastException: com.com.ecalc.Z_SettingsActivity cannot be cast to android.content.SharedPreferences$OnSharedPreferenceChangeListener
i think you have to register your object on the sharedPreferenceChangeListener in your onPause() and onStart()-methode
look here: https://stackoverflow.com/a/16449611/2924941