PreferenceFragment does not show up in Activity

10,195

I found the solution.

The screen wasn't blank, my theme made all the text white on white background.

So I just added a custom theme for settings activity and added this to android manifest:

        android:theme="@style/SettingsTheme"
Share:
10,195
Oleg Filimonov
Author by

Oleg Filimonov

Updated on June 24, 2022

Comments

  • Oleg Filimonov
    Oleg Filimonov almost 2 years

    I have followed the guide "settings" from android developers. But when I launch my SettingsActivity all I get is white screen. What am I doing wrong?

    Here is my SettingActivity.java

    public class SettingsActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            getFragmentManager().beginTransaction()
                     .replace(android.R.id.content, new SettingsFragment())
                     .commit();
        }
    }
    

    Here is SettingFragment

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

    Here is preferences file

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <EditTextPreference android:title="Your Name"
            android:key="username"
            android:summary="Please provide your username"></EditTextPreference>
        <CheckBoxPreference android:title="Application Updates"
            android:defaultValue="false"
            android:summary="This option if selected will allow the application to check for latest versions."
            android:key="applicationUpdates" />
        <ListPreference     android:title="Download Details"
            android:summary="Select the kind of data that you would like to download"
            android:key="downloadType"
            android:defaultValue="1"
            android:entries="@array/listArray"
            android:entryValues="@array/listValues" />
    </PreferenceScreen>
    

    And that's how I switch to SettingsActivity

    Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                                startActivity(intent);
    

    Is white screen is supposed to happed? I'm expecting a default screen with settings

  • IntelliJ Amiya
    IntelliJ Amiya over 8 years
    where "is" SettingsTheme ?
  • Oleg Filimonov
    Oleg Filimonov over 8 years
    you can set it to whatever you want, IntelliJ Amiya, mine is<style name="SettingsTheme" parent="Theme.AppCompat.Light"> </style>