Android EditTextPreference default value not shown - android

I have a XML PreferenceScreen that holds an EditTextPreference like below
<EditTextPreference
app:key="preference_key"
app:title="test preference"
app:useSimpleSummaryProvider="true"/>
If the user has not set anything "Not set" is shown as expected. However I want this to have a default value which is also shown, but the value is dynamic based on some other settings. I already have the code to get said setting, but I just don't know how to set it in the EditTextPrefrence (preference_key) programmatically?
If i try with Preference.setDefaultValue it still showns "Not set"

Related

Android Preference: CheckBoxPreference - Difference between defaultValue and checked

In CheckBoxPreference, what is the difference between "android:defaultValue" and "android:checked"? Both seems to be doing the same thing
<CheckBoxPreference
android:key="pref_"
android:title=""
android:summary=""
android:defaultValue="true"
android:checked="true">
</CheckBoxPreference>
Sets the checked state and saves it to the SharedPreferences. And Sets the default value for this Preference, which will be set either if persistence is off or persistence is on and the preference is not found in the persistent storage.
#andychen the default value tag defines what value should be fetched from preference if the user hasn't explicitly stored any value for checkbox in preference. And tag checked defines what state of checkbox should be shown to user on screen initialization

Getting value from edittext preference in preference screen

I'm new to android development. I just heard about preference screen in android. So I just made one. This is just to enter name in a dialog.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="Name"
android:summary="To enter name click on Name"
android:dialogTitle="Name Dialog"
android:dialogMessage="Enter Your Name"
android:key="name"/>
This is my created preference screen. I just need to know how can I access the entered name 'tony' in my mainactivity. I don't know the way to access the name from the dialog. Do I need to add any seperate activity for accessing the name in the name dialog. Can someone please explain it to me.
Try this:
PreferenceManager.getDefaultSharedPreferences(this).getString("name", null);
Where this is a Context like an Activity.
name is the key defined in your xml file.

Android preference inputType phone default value is a decimal value instead of a string

I have an Android application using default values from the Preferences Android framework.
It all works fine except for phone numbers (defined with android:inputType="phone" in preferences.xml).
The phone numbers get treated as numeric value so if I go to the preferences screen to see the default values I see
3.3631241E10
for the value defined in preferences.xml as
android:defaultValue="+33631241234"
To avoid this problem, I have used values from strings.xml defining the default values in preferences.xml like this :
android:defaultValue="+33631241234
It works ... but I don't like it: that's a source of problem as I need to redefine the same phone number for each language used ! !
I must be doing something wrong as I haven't found anyone else with the same problem on internet however I don't see what I am doing wrong ! !
Any help would be really appreciated.
that's a source of problem as I need to redefine the same phone number for each language used
you don't - when the text is absent for a language the default is used - docs :
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml.

Using SharedPreferences for application settings. Android

I'd like to know what is the best way to use shared preferences for application settings, namely to change text size and text colour. The tutorials I am finding are all confusing and most of them are using deprecated methods. What is the best way to proceed for API 17?
Create a preferences screen using this so your users have a place to change values. In your code, check the value of the keys you used in this preference screen and do whatever you need to do.
By the way, the example in the Android doc I linked shows hard-coded key string literals. The best practice way to do this is to create string keys in strings.xml resource file and reference the string key in your preference screen xml file and in your java code.
For example, in strings.xml:
<string name="wifiEnabled">wifi enabled</string>
In you preference screen xml file:
<CheckBoxPreference
android:key="#string/wifiEnabled"
android:title="WiFi" />
In your java code:
String wifiEnabledStringKey = getString(R.string.wifiEnabled);
//this will give you just 'wifi enabled'; you can then use this to retrieve the value of this key from SharedPreferences.

How to display the value of a SharedPreference without entering the dialog

I have some Shared Preferences in my application defined in an XML file. They are EditTextPreferences. I want to display the existing values on the Preference screen so that the user doesn't have to enter the Preference Edit Dialog to see the current value of the Preference. The defaultValue is only visible in the Dialog screen. I want to see the defaultValue in the main preferences screen - maybe in the place of android:summary. How can I do this?
<EditTextPreference
android:key="PREF_LT_500"
android:title="Step 1"
android:summary="I dont want a summary - I want to show current value"
android:inputType="numberDecimal"
android:defaultValue="0.8952"
android:dialogTitle="Enter Blah " />
Thanks in advance
You could try:
yourPrefrence.getText();
Or a more generic solution:
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
String ans = p.getString(keyStr, defaultValue);
When this is a Context, keyStr is the key & defaultValue is a default value in case the key is not found (usually "")

Categories

Resources