I want to use RingtoneManager (or create a custom similar class if not possible) to choose one of my custom list of raw files (I want to show only my files not the default list including my files).
I am used to use this way to pick ringtone How to bring up list of available notification sounds on Android but this time I want to show my custom list of ringtones.
If you want to do this in some kind of settings, you can use ListPreference for letting the user choose a melody. Then you just need to populate your preference in your code; you do this in the following manner:
ListPreference listPreference = (ListPreference) findPreference("yourPreferenceName");
//Now you need to retrieve your melodies from res/raw folder and get their names and id's
ListPreference needs two things to function properly: entries, and entry values. Entries are what the user sees when he opens the list (in your case, names of melodies). And the entry values are the values that will be saved in the default SharedPreference (read a little about using preferences from which ListPreference is derived). All you need to do now is to create entries and entryValues:
CharSequence entries = new CharSequence[numberOfMelodies];
CharSequence entryValues = new CharSequence[numberOfMelodies];
And fill them up manually or inside a loop.
After that just do
listPreference.setEntries(entries);
listPreference.setEntryValues(entryValues);
And get the selected melody's id from SharedPreference wherever you want in your app and use it. (Preference saves the new selection in the default SharedPreference for you automatically every time, you don't need to worry about that).
Related
Iam working on an android application and have trouble making a decision for the architecture saving application data.
Following case:
In the app the user has the possibility to create new general objects and give them properties he want. To support this, i want to give them a list with favorites before creating the input form, for example a car. It has color, weight, speed, horsepower etc.
So the user can choose a often picked object (for example the car) and will get the appropriate fields for the form he has to fill (color, weigth ...).
This list should be smart. The more you pick an item, the higher it appears in the list. And this presets have to be editable in preferences.
And thats the point. Should I implement my idea with the preferences framework from android (save it to xml as different preferences types and simply load due preferencebuilder) or should i create own xml objects and save it to self created user file location?
My second question: if i use the preference framework method .... is this made good for dynamically add entries at runtime? the ressources are in the res folder, but what if there are individual user entries? will they also be saved in the program folder or is there a special user data folder where the files (maybe encrypted) are in?
Thank you
Storing such complex data in SharedPreferences is tricky. What I mean is assuming user created 4 objects and each has 8 properties. You would store 4*8 values in sharedprefs and map them too.
What can be done is maintain an array list of objects created by user. Consecutive to that list maintain counter array list and keep swapping both lists internally as per number of times user has clicked the object. example:
List Name List Counter
ObjA 5
ObjB 3
ObjC 1
ObjD 1
Store these two lists in Shared Prefs.
Now, for the object's properties part (2 possibility arises) :
Maintain a mySQL DB and a table for each object's name. You can store values of each column in it IF you need to store every instance created of the object by user. (every time user clicks the object just show him/her the column names of table and store the values entered)
Example :
ObjA Table :
Color speed horsepower rpm
________________________________
red 20mph 100 3000
black 80mph 500 8000
Consecutively, if you don't want to store every instance value, you can make another sharedPrefs with object as key and an Arraylist of properties as value.
Suppose Type is the name of the array which contains A,B,C,D as elements and is created in strings.xml.Now in the form user can either select any of the elements or add new elements.Suppose user adds E,F,G .Now what i want to achieve is that anyhow the Type array have A,B,C,D,E,F,G in it. Using sqlite is done.But i want to save it in the Type array only and not anywhere else.Is it possible ?
Since anything that you define in res folder acquires a unique id in R.java, which is an array, and size of array cannot be changed at runtime.. So, it is not possible.
You can only save data to persistent storage like SQLite data base, shared preferences or a text file, each of which have different strengths and weaknesses and are suited for different situations, so take your pick.
I have a list Preference that created from resource xml. I added preference that created dialog in which user can add value to listPreference.
using those methods i added new value:
entries = getEntries();
entryValues = getEntryValues();
when user is adding values to listpreference, its displayed. But when preferenceScreen is recreating new value disappearing.
How can i save those new values?
Problem is that when you're reopening your PreferenceScreen, it loads the ListPreference's values from XML. You can change this behavior using the setEntries() and setEntryVaues() methods of ListPreference. Of course you need to somehow store all the values and their indexes that your users enter. You can use databases or SharedPreferences for it. Hope this helps.
EDIT
Saving the value of a ListPreference into the SharedPreferences:
preferences.edit().putString(listPreference.getKey(), listPreference.getValue());
I have one listView in my app, which i is use to list the differnt strings by dynamically adding the strings to ArrayList and using that in list view. I have declared this as below
public static List<String> myList = new ArrayList<String>();
this helps me in retaining the values of list even after exiting app, but i cant be able to retain it after i force close the app or cell boots off and on. Is there any way to store this list as we do in shared preference for strings and int?
Couple of methods.
1) Store the entire list as a Single String (delimiter separated) and split it when you read it back (depends on what kind of strings, because if you have delimiters inside the string, you're screwed, although you could use some kinda encoding (Base64))
2) Store them as separate variables "Key0" is the first element, "Key1" is the second...etc You get the idea...Remember to flush the Keys each time you save though.
3) Don't use Shared Preference, instead use a private file and write the entire ArrayList Object (It's serializable.) using a ObjectOutputStream
Cheers.
My application has a preference file, "settings", which contains 10 key/value pairs.
The keys act as titles for the user, and the values are URL's
Both the key and the value are changeable by the user e.g. the first setting looks something like "example" with the value "example.com", when a user changes that setting, the key also changes. So the first setting would become "different_example" with the value "different_example.com". All stored under the "settings" preference file.
I have been managing this so far, by opening a dialog containing the current key/value pairs in an ArrayList that has an onItemClickListener that pops up a second dialog containing another ArrayList of the possible key/value pairs. When the new item is clicked, I remove the current setting, add the new one, then re-populate the initial ArrayList with the new settings. This works and allows both the key and value to be simultaneously changed and updated, however it looks awkward with the two dialogs.
I'd like to switch this all over to ListPreferences. As in, have ten different ListPreference items, one for each setting, that when clicked opens the listing of all possible entryValues, and when selected, updates the key from the entry name, and the value from the entry value, and saves this under the same "settings" file. I'm not seeing how to save ListPreferences to a specific file, so that I can call
SharedPreferences settings = getSharedPreferences("settings", 0);
anywhere, though
I've also been looking for some kind of click handler for what to fire when an entry is selected so I can manually update the "settings" file, but not having any luck. Does such a thing exist? Or is there another way for me to do this?
Edit: I can use OnPreferenceChange to manually set the new value, but this doesn't return the value name, e.g. the value used in the human-readable list. Any ideas on how to get that?
See if this can give you a jump start: How do I get preferences to work in Android?
If you customize your ListPreference and come across something like this How to make a Preference not save to SharedPreferences?
Ahh, well this seems incredibly backwards, but what I've done is set each ListPreference to have an onPreferenceChangeListener, and each entryValue for the ListPreference to contain the name as well as the value separated by an arbitrary string. Then in the onPreferenceChange, I can reference the new value (which now also contains the new key) turn it into a String[] split at the arbitrary separator, then assign the String[] 0 index as the new key, and the 1 index as the new value by using SharedPreferences.Editor after removing the original setting.
Unless there's some way to return the Entry name from the ListPreference's entryValues array, or to specify ListPreference to save to a specific settings file, neither of which is documented, then this will probably be the best solution for me.