I have set android:defaultValue="\n", but when I open the PreferenceScreen, I find the third item is selected, I think the first item should be selected.
And more, I find more surprising thing. even if I select the third item, but the value String newLineValue = prefs.getString("SetNewLine", "\n") is "\n", it should be "\r\n".
Why? Thanks!
private String GetNewLine(){
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String newLineValue = prefs.getString("SetNewLine", "\n");
if (newLineValue.equals("\n")){
Toast.makeText(getApplicationContext(),"OK" ,Toast.LENGTH_SHORT).show();
}
return newLineValue;
}
array.xml
<string-array name="NewLine">
<item>Make new line with \\n</item>
<item>Make new line with <br/></item>
<item>Make new line with \\r\\n</item>
</string-array>
<string-array name="NewLine_values">
<item>\n</item>
<item><br/></item>
<item>\r\n</item>
</string-array>
mypreference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="@string/Preferencesummary"
android:title="@string/Preference" >
<ListPreference
android:defaultValue="\n"
android:dialogTitle="@string/NewLineFormat"
android:entries="@array/NewLine"
android:entryValues="@array/NewLine_values"
android:key="SetNewLine"
android:summary="@string/NewLineFormatsummary"
android:title="@string/NewLineFormat"
android:layout="@layout/mypreference_layout"
/>
</PreferenceScreen>