0

I’m a new developer, so my question maybe too much basic. I look for example of defining preferences of sound. User can choose what kind of sound will start an application, for example. There can be such a RingtonPreference widjet, so user can choose a sound. As I know, preferences support the primitive types: Boolean, string, float, long and integer. What way is the best to design preferences: store in entryValues the names if sounds (strings), the address of files from Resourse class (integer), or other way. Please provide a short example of code. Thanks in advance!

First of all thank you for the quick and detailed answer! I want to arrange list of sounds: there must be one “None”, list of sounds that contains folder “raw”, option to add a new sound from different locations and two buttons: “set” and “cancel”. When user touches one item from the list – sound starts to play. There is a little problem with standard widget that provide android library. “ListPreference” isn’t appropriate because on touch on one of the items – item is chosen and list closes, “there are not buttons set and cancel”. “RingtonPreference” isn’t appropriate as well – I didn’t succeed to add something to list. How is possible to build a custom preference layout and that is options that were chosen will be saved as well as on standard widgets. Please provide a short code example. Thanks in advance!

Dmitry Toder
  • 119
  • 1
  • 5
  • I found some good explenation to my question. http://stackoverflow.com/questions/4505845/concise-way-of-writing-new-dialogpreference-classes – Dmitry Toder Dec 10 '11 at 23:28

1 Answers1

1

I think the best way to store the Resource are by integers. or you could do names.

I think integer is more reliable.

So to use SharedPreference with this you will need to get access to the apps SharedPreference

public class PreferencesDemo extends Activity {

SharedPreferences app_preferences;
private int resourceNumber;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get the app's shared preferences

   SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

   resourceNumber = app_preferences.getInt("resourceNumber", 0);

   if(resourceNumber == 0){

   //This means the user hasnt selected a song and you must act accordingly. Or put a resource number where the 0 is do set it to a default song

    }

You will probably want to create a method to put the songs in the SharedPreference such as;

  private void createSongResouces(){

   SharedPreferences.Editor editor = app_preferences.edit();
    //Here you can put as many songs as you want just make sure you call editor.commit(); as i do.

    editor.putInt("resourceNumber", resourceNumber);
    editor.commit(); // Very important
}
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118