Geddon
Geddon

Reputation: 1306

Layout issue with my android preferences

Started messing around with Android recently and wanted to create a preference screen within my app. For this I am using the android UI settings guide. However when I get the SettingsFragment to load on my screen, the first PreferenceCategory is behind my action bar. I can't see to figure out what is causing this. I know this only because I hid the action bar and saw it behind. If I can get this shifted down a little, I would be happy with that. But don't understand much on how these preferences are formatted.

Here is what I have done to create this. Do I require a layout for this fragment? If so how do I add that?

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);

        ((SettingsActivity) getActivity()).getActionBar().show();
        ((SettingsActivity) getActivity()).getActionBar().setTitle(" Settings");

        //Loading the preference XML for the user profile settings
        addPreferencesFromResource(R.xml.preferences);

    }

}

SettingsActivity.java

public class SettingsActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {

    //@TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        //setContentView(R.layout.fragment_settings);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();

    }

preferences.xml

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

    <PreferenceCategory
        android:title="PROFILE SETTINGS"
        android:key="pref_user_settings">
        <Preference
            android:key="perf_username"
            android:summary="geddon"
            android:title="@string/perf_username">
        </Preference>
        <Preference
            android:key="perf_gender"
            android:summary="Female"
            android:title="Gender">
        </Preference>
        <Preference
            android:key="perf_email"
            android:summary="[email protected]"
            android:title="Email">
        </Preference>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/perf_user_settings"
        android:key="pref_key_preferences_settings">

        <ListPreference
            android:key="perf_country"
            android:title="Country"
            android:dialogTitle="Country"
            android:defaultValue="USA">
        </ListPreference>

        <Preference
            android:key="perf_password"
            android:summary="password"
            android:title="Change Password">
        </Preference>

    </PreferenceCategory>

</PreferenceScreen>

ScreenShot enter image description here

Upvotes: 0

Views: 149

Answers (1)

dabluck
dabluck

Reputation: 1181

try setting a layout in the activity.

setContentView(R.layout.activity_layout);

where activity_layout is just an empty framelayout or something. then show the action bar in the activity. and i think that will do it.

Upvotes: 1

Related Questions