Search code examples
javaandroidpreferenceringtone

can I make ringtone in preferences


How can I make ringtone activity (that always appear in setting) so the user can choose her ringtone from system ringTones I googled it but I didn't find complete tutorial, I am really confused, please give me tutorial or some codes.

Also, if I want the user to choose the special ringtone to Notification in my application should i use Shared preference or preference?

I already did the Menu:

      // Menu Code Part#2
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.about:
  startActivity(new Intent(this, About.class));
    return true;
    case R.id.help:
  startActivity(new Intent(this, Help.class));
    return true;
    case R.id.setting:
   startActivity(new Intent(this, Setting.class));
        return true;
    default:
    return super.onOptionsItemSelected(item);
   }

Solution

  • Full code:

    res/xml/preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
          <PreferenceCategory
                    android:title="Second Category">
    
            <RingtonePreference
                    android:name="Ringtone Preference"
                    android:summary="Select a ringtone"
                    android:title="Ringtones"
                    android:key="ringtonePref" />
    
    </PreferenceCategory>
    </PreferenceScreen>
    

    Preferences.class

    public class Preferences extends PreferenceActivity {
    @Override
            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    addPreferencesFromResource(R.xml.preferences); 
    
     }
    }
    

    Your code go here:

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about:
       // startActivity(new Intent(this, About.class));
        return true;
        case R.id.help:
        startActivity(new Intent(this, Help.class));
        return true;
        case R.id.setting:
        Intent settingsActivity = new Intent(getBaseContext(),
                                                    Preferences.class);
                                    startActivity(settingsActivity);
    
    
            return true;
        default:
        return super.onOptionsItemSelected(item);
        } 
    

    To read these preferences from code, we should create a getPrefs() method, which we can call in the onStart() method. When we call it in the onStart() method instead of onCreate(), we can be sure that the preferences load when we have set them and returned to our main activity, The getPrefs() method could look like this:

    String ringtonePreference;
    // Get the xml/preferences.xml preferences
                    SharedPreferences prefs = PreferenceManager
                                    .getDefaultSharedPreferences(getBaseContext());
     ringtonePreference = prefs.getString("ringtonePref",
                                    "DEFAULT_RINGTONE_URI");
    

    androidmanifest.xml

    <activity
            android:name=".Preferences"
            android:label="@string/set_preferences">
    </activity>