List from SQLite in a preferences.xml - android

i'm a new android developer and i hope that you guys could help me.
I'm developing a preference screen like above:
<PreferenceCategory android:title="#string/security_title" >
<EditTextPreference
android:name="Password"
android:defaultValue=""
android:key="passwordPref"
android:password="true"
android:summary="#string/security_sumary"
android:title="#string/security_addPassword" />
<PreferenceScreen
android:key="secondPrefScreenPref"
android:summary="Click here to go to the second Preference Screen"
android:title="Second Preference Screen" >
<ListPreference android:entries="#android:id/list" android:key="listPasswordKey" />
</PreferenceScreen>
</PreferenceCategory>
In a password field,the user are going to type his passw and when he clicks OK button, the program are gonna save it in my database. This part is ok!
But when he clicks on a second pref screen i want to list, from database, all your passwords saved.
I know how to load the data, but i don't know how to call my method when the user clicks on a SecondPrefScreen and how to show them in a preferences.xml
can anyone help me?
thx

You will have to create a widget that will load the data and list it for you. Essentially, create an object that extends DialogPreference and give it a child list view that will list your security credentials.

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

Android SetingActivity ListPreferences Load Values from Array

Is it possible to load an String-list or HashMap into a ListPreference inside a SettingsActivity or do I need to start a whole knew activity?
And if, how can I register a click onto an Item inside a SettingsActivity?
note: I want to load a HashMap, the List should show the key value and if the user clicks on the Item, he should edit the value behind the key value. There also will be an "add" button so the user can add new keys and values by himself.
I figured out, that I need to start a new activity from my SettingsActivity.
I did this by creating a new Activity and adding this to my preferencex.xml:
<PreferenceCategory
android:title="#string/pref_color_picker"
android:key="pref_key_color_picker">
<Preference android:title="#string/default_colors">
<intent android:action="android.intent.action.VIEW"
android:targetPackage="my.package.app"
android:targetClass="my.package.app.theActivity"/>
</Preference>
</PreferenceCategory>

Clear preference item values

I'm creating a PreferencesActivity where I have 3 preference items. When I click on each of them, I load contacts to pick one. After selecting a contact, I save it's name and phone number and I show it in the preference item.
But now I have to add the option to delete a contact. This would be done deleting the content of the preference, this is, clearing the strings. But my real question is, how to do this?
As there is not a normal layout, I cannot add a element for deleting that data, nor I can't do a long click on it to provide this option.
So my question is, working with preferences, what would be the way to provide the user at UI level the option to delete data from selected preference?
PD: I'm not asking about how to clear preferences data, but how to provide the user a option to clear selected preference data in the user interface (button, long click, etc...).
preferences.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="#string/contact">
<Preference
android:key="Contact1"
android:title="Contact 1"/>
<Preference
android:key="Contact2"
android:title="Contact 2"/>
<Preference
android:key="Contact3"
android:title="Contact 3"/>
</PreferenceCategory>
</PreferenceScreen>
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.edit().clear().commit(); //remove all your prefs :)
if you want to clear a specific data..then do it with the given key
eg. if you are saving a password of the user..and on click of logout button you want to clear it.just do this way
pref.edit().putString("password","").commit();
if you want to remove it then
pref.edit().remove("password").commit();

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>

Why doesn't my preferences page show the current values?

I have a preferences page which is defined by XML - including some default values. I use a PreferenceActivity to display and handle this page. Whenever I use this page to set the preferences the preference file on the file system is updated properly - I can see this via adb.
However, whenever I go back to the settings page after have changed some of the settings, it's the defaults that are shown. Worse than that, if I press back without changing any settings, it then sets the, all back to the default.
Any ideas on how I can get the prefs to actually show the current settings?
My PreferenceActivityis created thus:
#Override
protected void onCreate(Bundle savedInstanceState) {
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
setDefaults(this);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
The XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:id="#+id/numberOfYearsList"
android:key="numberOfYears"
android:title="Number of Years to Read the Bible"
android:summary="How many years would you like to take to read through the reading plan?"
android:entries="#array/numberOfYears"
android:entryValues="#array/numberOfYears"
android:dialogTitle="How Many Years?"
android:defaultValue="1"
/>
<CheckBoxPreference android:key="ignoreDates"
android:id="#+id/ignoreDatesCheckbox"
android:title="Ignore Dates"
android:summary="Would you like to use the dates in the plan?"
android:defaultValue="false"
/>
</PreferenceScreen>
Nevermind - I did a mistake. As you can see from above, when creating the Preference Activity I'm making a call to a method that sets all the prefs to defaults... Duh!

Categories

Resources