Android Default Preference xml file format - android

I appear to be the only person on the internet who can't figure this out, but when I use
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
I have tried the normal android string resource format, but nothing seems to work. Thanks ahead of time.

Use android:defaultValue like so:
<CheckBoxPreference
android:key="my_pref"
android:defaultValue="true"
android:title="#string/pref_title_my_pref"
android:summary="#string/pref_summary_my_pref"/>

You are not the only person who doesn't know this. Even am struggling to know the file format of the XML. Finally i found the answer! There you go!
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:key="CLIENT_HOMESCREEN_TITLE"
android:defaultValue="Home Screen"/>
<EditTextPreference android:key="CLIENT_ADMIN_BUTTON"
android:defaultValue="Admin"/>
<EditTextPreference android:key="CLIENT_PLAYER_BUTTON"
android:defaultValue="Player"/>
<EditTextPreference android:key="CLIENT_SAVE_BUTTON"
android:defaultValue="Save"/>
<EditTextPreference android:key="CLIENT_CANCEL_BUTTON"
android:defaultValue="Cancel"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_IP"
android:defaultValue="IP:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_PORT"
android:defaultValue="Port:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_TITLE"
android:defaultValue="Server Setup"/>
</PreferenceScreen>

what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
For this just set default value to true
Whatever default values you have provided in preference xml file, that will be set.

Related

how to display preference values

I'm trying to migrate the preferences part of my app because of warning
default constructor in android.preference.preferenceactivity is deprecated
But, even after reading a lot of ressources, I don't understand why I can't have 2 preference values (string and integer) displayed in front of (or below) the EditTextPreference as I had before.
"Compte" (in the capture) (String) is enabled="false" because I don't want the user can edit it. But I want the user can see the account name in preferences.
"Nom de jours" (Integer) can be edited, if the user click on it, but I want the value is shown without opening editing mode.
prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="#string/prefsCatGlobalTitle">
<EditTextPreference
app:enabled="false"
app:key="param_email"
app:title="#string/cnxEmailLabel" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatAlertsTitle">
<EditTextPreference
app:defaultValue="7"
android:inputType="number"
app:key="prefsAlertNbDays"
android:maxLength="2"
app:maxLines="1"
android:selectAllOnFocus="true"
app:singleLine="true"
app:title="#string/prefsAlertNbOfDays" />
<SwitchPreference
app:defaultValue="true"
app:key="prefsAlertNotif"
app:title="#string/prefsAlertNotifTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertEmail"
app:title="#string/prefsAlertEmailTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertCal"
app:title="#string/prefsAlertCalTitle" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatOthersTitle">
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertAutoSync"
app:title="#string/prefsAlertAutoSync" />
</PreferenceCategory>
</PreferenceScreen>
Preference capture
Apart from the visualization problem, everything works fine and my preferences are well saved and usable in my java code.
I can't find what I'm doing wrong or what I'm missing.
Thanks to #cmak
I never found this app:useSimpleSummaryProvider option before.

Android Preferences defaultValue not works

Here is my preferences.xml
<PreferenceCategory android:title="#string/pref_cat_recognition">
<ListPreference
android:entries="#array/unitsArray"
android:entryValues="#array/unitsValues"
android:icon="#null"
android:key="unit"
android:summary="#string/units_summary"
android:title="#string/units"
android:defaultValue="1"/>
</PreferenceCategory>
and my array.xml
<string-array name="unitsArray" translatable="false">
<item>cm</item>
<item>inch</item>
</string-array>
<string-array name="unitsValues" translatable="false">
<item>1</item>
<item>2</item>
</string-array>
So it should be cm checked on start but there is no value checked. Why and how to solve that to have a value cm on start?
Your code is working fine. The reason why you dont see the selected value on the summary is because you have specified a custom summary text. You can use useSimpleSummaryProvider to enable the simple version of summary, also enabling the selected value seen without opening the preference.
Pls check my code below for clarity of things:
`<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="Choose Gender">
<ListPreference
app:defaultValue="m"
app:entries="#array/gender_entries"
app:entryValues="#array/gender_values"
app:key="gender"
app:title="Your Gender"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="StackOverflow">
<ListPreference
app:defaultValue="1"
app:entries="#array/unitsArray"
app:entryValues="#array/unitsValues"
app:key="unit"
app:title="Units"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="1"
app:entries="#array/unitsArray"
app:entryValues="#array/unitsValues"
app:key="unit"
app:summary="Your summary goes here"
app:title="Units" />
</PreferenceCategory>
`
This is the preference screen view
If the summary was provided you'd be seeing the summary text and not the default/selected value like in the picture above.
It turned out in manifest I had android:allowBackup="true"

SharedPreference values form xml

I'm writing preferences page (preferences.xml), and trying to retrieve values from resources file. This code is working;
<PreferenceScreen ...
<ListPreference
android:entries="#array/tips"
android:entryValues="#array/tips"
android:title="Tip rate"
/>
But this is not;
<EditTextPreference
android:defaultValue="#integer/invoiceNumberNext" // this doesn't work
android:key="invoice_number_next"
android:title="Next Invoice #"
android:inputType="numberDecimal"
/>
I have this in my defaults.xml page which is under res/values
<resources>
<integer name="invoiceNumberNext">1001</integer>
</resources>
Any idea why I'm not able to fetch some values?
The problem is that you misplaced your value. If you reference a value with #integer it must be in a xml file called (what a mystery...) integer.xml. This file must be in the folder structure
res/values/integer.xml
So right click on values folder and click "New-->Values resource file" and then enter integer.xml Here, now add your resources like you have done....tataaaa....you can use it.
EDIT
As I hadn´t understand the original question correctly, I have to elaborate this answer. Of course it is possible to create a file defaults.xml and put an <integer> item inside, and this is still available by #integer. So, at the beginning of this question, it seems that you misplaced the file into the wrong directory. But that isn´t the case so your problem must be anywhere else. If the default.xml is in res/values, it should work.

error: Error: String types not allowed (at 'entries' with value 'array/list')

I am new in android development and developing pref.xml (resource type prefrence) using eclipe software. Here is my pref.xml code
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="EditText"
android:key = "name"
android:summary="Enter Your name"
></EditTextPreference>
<CheckBoxPreference
android:title="CheckBox"
android:defaultValue="true"
android:key="checkBox"
android:summary="check this box"
></CheckBoxPreference>
<ListPreference
android:title="List"
android:key="list"
android:summary="This Is A List To Choose From"
android:entries="array/list"
></ListPreference>
and i got this error :
error: Error: String types not allowed (at 'entries' with value 'array/list')
Please help me.. how do i handle this error ?
Find file name strings.xml in res\value folder in your package explorer...
add following line in this file
<string-array name="list"></string-array>
Your Final file look like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Muzafar Khan</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="list"></string-array>
Now save your project... and enjoy :)
Change
android:entries="array/list"
to
android:entries="#array/list"
I am assuming you are trying to load the entries from the array resource file, correct? If so, you need to do "#array/list" instead. All of the resources (string, id, etc) must be prefixed with # in the XML.
This one worked for me
android:entries="#+array/list"
hope it helps :)
android:entries="#array/list"
gives an error, you have to use
android:entries="#+array/list
You need to create resource type 'arrays' in the 'res' folder where the array elements will be defined after declaring the array type in the pref_general.xml file.
In the arrays.xml file you can define as
<string-array name="**array name**">
<item>#string/**array element 1**</item>
<item>#string/**array element 2**</item>
</string-array>
So in your pref general file you will actually define array as
android:entryValues="#array/array name"
So in your string file you define the array elements as strings in normal way
Cheers
go to values package create a new xml file
for ex- array.xml
then
<resources>
<string-array name="list"></string-array>
set the string-array
and change
android:entries="array/list"
to
android:entries="#array/list"
for refering the file
:

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.

Categories

Resources