javaandroidxmlpreferencespreferenceactivity

How do I use Android's DropDownPreference?


I'm trying to build a preference screen with a DropDownPreference. Initially I was using the following in my gradle file compile 'com.android.support:preference-v14:25.3.1' but switched to compile 'com.android.support:preference-v7:25.3.1' when I noticed the DropDownPreference was included in v7, not v14 (I thought v14 might also include everything from v7, but I guess not?). My XML looks like this:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/pref_title"
android:layout_height="match_parent"
android:layout_width="match_parent">

    <PreferenceCategory
        android:key="pref_video"
        android:title="@string/pref_video_title">

        <android.support.v7.preference.DropDownPreference
            android:key="pref_video_quality"
            android:title="@string/pref_video_quality"
            android:summary="@string/pref_summary_video_quality"
            android:entries="@array/pref_entries_video_quality"
            android:entryValues="@array/pref_entries_video_quality" />

    </PreferenceCategory>

</PreferenceScreen>

I've also just tried DropDownPreference as the tag. Nothing seems to work. I always get an Error inflating class DropDownPreference error when I try to go to my preference screen in the app.

Any idea how I can use this DropDownPreference? Thanks!

Edit: Adding error message:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.int_a.giantbombforandroid/com.app.int_a.giantbombforandroid.main.SettingsActivity}: java.lang.ClassCastException: android.support.v7.preference.DropDownPreference cannot be cast to android.preference.Preference

Edit: SettingsActivity declaration in AndroidManifest.xml

<activity android:name=".main.SettingsActivity"
  android:configChanges="orientation|screenSize"
  android:theme="@style/PreferenceThemeOverlay.v14.Material">
</activity>

Solution

  • Follow up from my comment. Code is in Kotlin btw :)

    styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
    </style>
    

    activity_settings.xml

    <LinearLayout android:orientation="vertical">
    
        <android.support.v7.widget.Toolbar 
            android:id="@+id/toolbar" />
    
        <FrameLayout android:id="@+id/frame"/>
    
    </LinearLayout>
    

    prefs.xml has exactly what you have in your question

    Settings.kt

    class SettingsActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_settings)
    
            setupActionBar()
    
            if (savedInstanceState == null)
                supportFragmentManager
                        .beginTransaction()
                        .add(R.id.frame, SettingsFragment.newInstance())
                        .commit()
        }
    
        private fun setupActionBar() {
            setSupportActionBar(toolbar)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
    
        class SettingsFragment : PreferenceFragmentCompat() {
    
            override fun onActivityCreated(savedInstanceState: Bundle?) {
                super.onActivityCreated(savedInstanceState)
    
                val actionBar = (activity as AppCompatActivity).supportActionBar
    
                actionBar?.title = preferenceScreen.title
            }
    
            override fun onCreatePreferences(bundle: Bundle?, rootKey: String?) {
    
                // Using this method instead of addPreferencesFromXml so we can specify the root preference screen
                // This way, navigating to a new screen is as simple as calling SettingsFragment.newInstance("new_root")
                setPreferencesFromResource(R.xml.prefs, rootKey)
            }
    
            companion object {
    
                fun newInstance(rootKey: String = "root") = SettingsFragment().apply {
    
                    // When you pass a string argument with key ARG_PREFERENCE_ROOT, 
                    // PreferenceFragmentCompat picks it up and supplies it as an argument to onCreatePreferences
                    arguments = Bundle().apply { putString(ARG_PREFERENCE_ROOT, rootKey) }
                }
            }
        }
    }
    

    This is the end result:

    enter image description here