Search code examples
android-preferencesandroid-studio-3.0

Android Studio - PreferenceScreen not 'previewing'


I have a fresh new install of Android Studio 3.1.4 I wish to add a PreferenceScreen to my app (I'm just following a tutorial to add Settings panel). I read the docs step by step and I'm coding accordingly but I cannot understand why Android Studio is not rendering the PreferenceScreen. This's the code of my PrefenceScreen:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:defaultValue="false"
        android:key="switch_preference_1"
        android:title="Switch preference" />
    <EditTextPreference
        android:defaultValue="Default value"
        android:key="edit_text_preference_1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="Edit text preference" />
</PreferenceScreen>

The layout preview is blank:

enter image description here

the preferences.xml file is located in \res\xml directory.

No errors/warning on build.


Solution

  • I ran into the same problem today but with a newer version of Android Studio.

    I am using Android Studio 4.0.1 and the Android X libraries. I found an answer to a related question here: https://stackoverflow.com/a/53615855/5152586 which indicates that using the preferences screen layout requires the addition of preference dependency to the build.gradle file.

    implementation 'androidx.preference:preference:1.1.1'
    

    Adding that dependency to my build.gradle file worked for me and I can now see the rendered preview for my preference screen in the Layout Designer mode.

    If you are not using the newest stuff yet, another answer (https://stackoverflow.com/a/54738703/5152586) to that same question indicated that the dependencies you'll need would be both the AppCompat and Preference dependencies from the support library.

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:preference-v7:28.0.0'