How to implement RingtonePreference in PreferenceFragmentCompat?
For today ringtone preference does not exist in the support library. You need to use the framework's version or create your own.
I think it will appear soon
Not my solution, but I post it anyway, because it works.
In you preferences XML resource, change RingtonePreference to Preference. Then, in your implementation of PreferenceFragment, add:
@Override public boolean onPreferenceTreeClick(Preference preference) { if (preference.getKey().equals(KEY_RINGTONE_PREFERENCE)) { Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI); String existingValue = getRingtonePreferenceValue(); // TODO if (existingValue != null) { if (existingValue.length() == 0) { // Select "Silent" intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null); } else { intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(existingValue)); } } else { // No ringtone has been selected, set to the default intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Settings.System.DEFAULT_NOTIFICATION_URI); } startActivityForResult(intent, REQUEST_CODE_ALERT_RINGTONE); return true; } else { return super.onPreferenceTreeClick(preference); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_ALERT_RINGTONE && data != null) { Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (ringtone != null) { setRingtonPreferenceValue(ringtone.toString()); // TODO } else { // "Silent" was selected setRingtonPreferenceValue(""); // TODO } } else { super.onActivityResult(requestCode, resultCode, data); } }
Source.