Jaeger
Jaeger

Reputation: 1716

Insert Preference XML inside a Layout

I know my question is a bit strange but here's the story. I've added a PreferenceFragment in my App, I'm replacing a fragment in the main layout with the SettingsFragment, but i'm having a problem, the XML content is shown in match_parent, and shown above the toolbar, i want it below the toolbar.

Why it's happening ? because the Fragment layout is as the following :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.abohani.ramadantime.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


<FrameLayout
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    />

</RelativeLayout>

I can't change the fragment and set it below the toolbar, or i'm gonna miss most of my app (i added it for reasons).

So my problem in a nutshell, the content is overlaping the toolbar, i've tried the following way :

    <Preference
    android:layout="@layout/space"
    />

Space layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
    android:id="@+id/views"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:visibility="invisible"
    />

</RelativeLayout>

It worked, but when scrolling, it's not working, the content scroll above the toolbar.

So, i need something close to "below:@id/layout" .

Any ideas ?

Upvotes: 3

Views: 2277

Answers (1)

apelsoczi
apelsoczi

Reputation: 1123

The pattern used on the Android Developer site is dated to the days of API 10. Here's an example below, how to implement Preferences using a modern Activity/Fragment design pattern - this also works if you are using a master/detail flow on tablets.

pref_general.xml

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

    <ListPreference
        android:icon="@drawable/ic_sort_black_48dp"
        android:title="@string/pref_sort_label"
        android:key="@string/pref_sort_key"
        android:defaultValue="@string/pref_sort_favorite"
        android:entryValues="@array/pref_sort_values"
        android:entries="@array/pref_sort_options" />

</PreferenceScreen>

arrays.xml

<resources>

    <string-array name="pref_sort_options">
        <item>@string/pref_sort_label_popular</item>
        <item>@string/pref_sort_label_rating</item>
        <item>@string/pref_sort_label_favorite</item>
    </string-array>

    <string-array name="pref_sort_values">
        <item>@string/pref_sort_popular</item>
        <item>@string/pref_sort_rating</item>
        <item>@string/pref_sort_favorite</item>
    </string-array>

</resources>

strings.xml

<string name="pref_sort_label">Sort Order</string>
<string name="pref_sort_label_popular">Most Popular</string>
<string name="pref_sort_label_rating">Top Rated</string>
<string name="pref_sort_label_favorite">Favorites</string>
<string name="pref_sort_key" translatable="false">sort</string>
<string name="pref_sort_popular" translatable="false">popular</string>
<string name="pref_sort_rating" translatable="false">rating</string>
<string name="pref_sort_favorite" translatable="false">favorites</string>

activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingsActivity"
    tools:ignore="MergeRootFrame">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

    <FrameLayout
        android:id="@+id/container_settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/ToolbarTheme"
    android:elevation="4dp"
    app:titleMarginStart="32dp"
    app:popupTheme="@style/PopupTheme">

</android.support.v7.widget.Toolbar>

SettingsActivity.java

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getFragmentManager().beginTransaction()
                .replace(R.id.container_settings, new SettingsFragment())
                .commit();
    }

    public static class SettingsFragment extends PreferenceFragment
            implements Preference.OnPreferenceChangeListener {

        public static String TAG = SettingsFragment.class.getSimpleName();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.pref_general);
            bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_sort_key)));
        }

        private void bindPreferenceSummaryToValue(Preference preference) {
            preference.setOnPreferenceChangeListener(this);

            onPreferenceChange(preference,
                    PreferenceManager
                            .getDefaultSharedPreferences(preference.getContext())
                            .getString(preference.getKey(), ""));
        }

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            String stringValue = newValue.toString();

            if (preference instanceof ListPreference) {
                ListPreference listPreference = (ListPreference) preference;
                int prefIndex = listPreference.findIndexOfValue(stringValue);
                if (prefIndex >= 0) {
                    preference.setSummary(listPreference.getEntries()[prefIndex]);
                }
            }
            else {
                preference.setSummary(stringValue);
            }

            return true;
        }
    }
}

Upvotes: 3

Related Questions