Not able to find "extra" attribute in jetpack preference in Android - android

Am trying to build Settings kind of App, I choose to use Preferences to build UI .
I am using jet pack preferences (androidx.preference.Preference) ,
If I want to launch fragment with some extras ,can use below with normal preference .
<com.android.xyz.ItemsTextPreference
android:key="xyz"
android:title="Fragment One"
android:visibility="invisible"
android:fragment="com.android.xyz.MyFragment"
<extra android:name="one" android:value="first fragment" />
</com.android.xyz.ItemsTextPreference>
But in Jetpack preference , there is no "extra" attribute .
Any alternative way to send extra from preference view ?

try to wrap your extra attribute inside the intent attribute
something like this:
<com.android.xyz.ItemsTextPreference
android:key="xyz"
android:title="Fragment One"
android:visibility="invisible">
<intent android:action="your_action" >
<extra android:name="one" android:value="first fragment" />
</intent>
</com.android.xyz.ItemsTextPreference>

I found solution , earlier my Preference screen having only below schema, which is from jetpack.
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
After adding one more schema from traditional preference from android, like below .
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
I can use combo to achieve result .
<com.android.xyz.ItemsTextPreference
android:key="xyz"
android:title="Fragment One"
app:isPreferenceVisible="false">
<extra android:name="one" android:value="first fragment" />
</com.android.xyz.ItemsTextPreference>

Related

How to create Master Switch in preferences setting activity

I want to create master switch as shown in image to enable or disable all options using android preference activity.
https://i.stack.imgur.com/fsA8G.png
Something like this :- https://source.android.com/devices/tech/settings/settings-guidelines#master_setting
I didn't find a solution either but I have something similar. Maybe it helps.
I have this on top:
<PreferenceCategory app:title="">
<SwitchPreferenceCompat
app:key="MasterSwitch"
app:summaryOff="Everything is disabled"
app:summaryOn="Everything is enabled"
app:title="This is a master switch (more or less)"
/>
</PreferenceCategory>
And then, every PreferenceCategory depends on the above (app:dependency="MasterSwitch"). So, if the "MasterSwitch" is disabled, all categories below are displayed as disabled. Set the Categories this way:
<PreferenceCategory
app:dependency="MasterSwitch"
app:title="Another category">
<EditTextPreference
app:key="signature"
app:title="Option 1"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
and so on...
The problem is that I cannot set the nice blue or gray background color that you showed here: https://source.android.com/devices/tech/settings/settings-guidelines#master_setting
I cannot have the information box it is shown there either.
You can do this in Settings Activity or Fragment. There are a lot of tutorials on YouTube.
New -> Activity -> Settings Activity

Default value for the preference in the android application is not updating

In my preference xml I have:
<EditTextPreference
android:title="Location"
android:key="#string/pref_location_key"
android:defaultValue="689558"
android:inputType="text"
android:singleLine="true" />
So the default value for this preference supposed to be "689558", but when I run my app it looks like this:
In short, I have mysterious "94043" value instead of "689558".
Where I could make mistake?
Didn't you set your preference before? Try to delete the app data from settings/applications and reinstall your app.
If you set the preference once it will remain the same independently from restart or reinstall.

How create a settings menu with checkbox Android

I want to create a preference screen in which there are three checkboxes; the first one is clickable and the other two are not until the first is checked.
How can I do this? I've seen this tutorial, but there is only one checkbox. Can anyone help me?
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="#string/summary_category"
android:title="#string/title_category">
<CheckBoxPreference
android:key="main"
android:defaultValue="true"
android:summary="#string/summary_main"
android:title="#string/title_main"
/>
<CheckBoxPreference
android:key="firstDependent"
android:summary="#string/summary_firstDependent"
android:title="#string/title_firstDependent"
android:dependancy="main"
/>
<CheckBoxPreference
android:key="secondDependent"
android:summary="#string/summary_secondDependent"
android:title="#string/title_secondDependent"
android:dependancy="main"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
You can do this simply by setting android:dependancy to the key of the check box in which the respective check boxes must depend on.
Now create a folder named xml in res folder and put your preferences xml file inside that.
Then do the following.
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.
Hope this helps.
You have to do it like in that example, but you'll have three checkboxes instead of one. If you want two checkboxes to be disabled until the first one is true you can use the android:dependency property. With this property you need to specify the key of the preference they will depend on.
<PreferenceCategory
android:summary="..."
android:title="..." >
<CheckBoxPreference
android:defaultValue="true"
android:key="first"
android:summary="#string/summary_first"
android:title="#string/title_first" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="second"
android:summary="#string/summary_second"
android:title="#string/title_second" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="third"
android:summary="#string/summary_third"
android:title="#string/title_third" />
</PreferenceCategory>

Setting default values of Shared Preferences when using x Preference Fragments

I have a bit of confusion about setting default values of shared preferences... for example, I have a PreferenceActivity with two or more PreferenceFragment and each of the preference fragmens has its own *preference_fragmentX.xml* file used to build the preference view. When I set the default values with
PreferenceManager.setDefaultValues(getActivity(), R.xml.?????, false);
Which XML should I specify? Should I build another XML preference file containing all the preferences of the *preference_fragmentX.xml* files with the default values and use this one in setDefaultValues? For example, I may build an app that registers for a broadcast event and I'm not sure the user opens the preference activity before the broadcast event is fired for the first time, so I'd like to initialize default preferences in the broadcast event, or in another activity started by it.. so, how do I initialize all the preferences of all the fragment panes?
It works if I build a default_preferences.xml file which contains all the preference keys defined in all the preference fragments XML files (I specify only the a PreferenceScreen root tag and all the preference tags under it, without specifying all the other tags like PreferenceCategory.. furthermore for each preference I specify only the key and the default vale attributes), but I don't know if it is the correct way to do this task, since I must replicate part of the other XML files:
preferences_fragment1.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/my_preference_category">
<RingtonePreference
android:key="preference_ringtone"
android:ringtoneType="alarm|notification"
android:showSilent="true"
android:summary="#string/preference_ringtone_summary"
android:title="#string/preference_ringtone_title" />
<ListPreference
android:key="preference_list"
android:entries="#array/list_items"
android:entryValues="#array/list_values"
android:summary="#string/preference_list_summary"
android:title="#string/preference_list_title" />
</PreferenceCategory>
</PreferenceScreen>
default_preference.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:key="preference_ringtone"/>
<ListPreference
android:defaultValue="5"
android:key="preference_list"/>
</PreferenceScreen>

Multi select ListPreference on android

Any idea on implementing a multi-select (check boxes) ListPreference on Android?
Would I have to extend the ListPreference?
Is there any classes already documented to do this?
Thanks
Multi select ListPreference now comes natively with Android from API level 11 (Honeycomb).
http://developer.android.com/reference/android/preference/MultiSelectListPreference.html
Because it will be quite a while before devices have Honeycomb or later installed I'd recommend people to stick with the http://blog.350nice.com/wp/archives/240 solution.
EDIT: I think at this moment in time (almost 3 years after this answer was originally posted) you are better off using the native version now as the majority of devices have Android 4 and up.
Well , http://blog.350nice.com/wp/archives/240 does provide a solution , but a simpler solution would be just implementing a child preference screen inside the parent , and then the child preference screen can have multiple check boxes . I know , its not the best solution , but gets the job done .
For eg - the Below preference.xml
<PreferenceCategory
android:title="Regular messages"
android:key="regular_messages">
<CheckBoxPreference
android:key="enable_regular_messages"
android:summary="Enable or disable regular messages"
android:title="Send regular messages"
android:defaultValue="true"
/>
<ListPreference
android:key="send_interval"
android:title="Send interval"
android:summary="Define how often you want to send messages"
android:defaultValue="60000"
android:entries="#array/send_interval"
android:entryValues="#array/send_interval_values"
android:dependency="enable_regular_messages"
/>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Messages type"
android:key="messages_type"
android:summary="Select the type of messages to be sent"
android:dependency="enable_regular_messages">
<CheckBoxPreference
android:key="enable_status_messages"
android:summary="Enable or disable status messages"
android:title="Send status messages"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="enable_event_messages"
android:summary="Enable or disable event messages"
android:title="Send event messages"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="enable_critical_messages"
android:summary="Enable or disable critical messages"
android:title="Send critical messages"
android:defaultValue="true"
/>
</PreferenceScreen>
</PreferenceCategory>
Found a very useful link:
http://blog.350nice.com/wp/archives/240
Here's a single-class implementation with defaultValue support:
https://github.com/yanchenko/droidparts/blob/develop/droidparts/src/org/droidparts/widget/MultiSelectListPreference.java
There is a github project just for this

Categories

Resources