Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2266b920 authored by Mindy Pereira's avatar Mindy Pereira
Browse files

Create logic for applying padding only to custom preference layouts

n the case of any standard PreferenceFragment layout, we can
change the list styling to add padding and set clipToPadding to false.
In this case, we don't want extra padding in the parent ViewGroup
(R.id.prefs).
When an app specifies a custom preference fragment layout, we want
to add the previously existing padding styles R.id.prefs.

Change-Id: Idfd77dcbef264c6f5f4121b66fd54c684ad0a288
parent b43d7589
Loading
Loading
Loading
Loading
+84 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.preference;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

/**
 * @hide
 */
public class PreferenceFrameLayout extends FrameLayout {
    private static final int DEFAULT_TOP_PADDING = 0;
    private static final int DEFAULT_BOTTOM_PADDING = 0;
    private final int mTopPadding;
    private final int mBottomPadding;
    private boolean mPaddingApplied = false;

    public PreferenceFrameLayout(Context context) {
        this(context, null);
    }

    public PreferenceFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.preferenceFrameLayoutStyle);
    }

    public PreferenceFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.PreferenceFrameLayout, defStyle, 0);

        mTopPadding = (int) a.getDimension(
                com.android.internal.R.styleable.PreferenceFrameLayout_topPadding,
                DEFAULT_TOP_PADDING);
        mBottomPadding = (int) a.getDimension(
                com.android.internal.R.styleable.PreferenceFrameLayout_bottomPadding,
                DEFAULT_BOTTOM_PADDING);

        a.recycle();
    }

    @Override
    public void addView(View child) {
        int topPadding = getPaddingTop();
        int bottomPadding = getPaddingBottom();
        // Check on the id of the child before adding it.
        if (child != null && child.getId() != com.android.internal.R.id.default_preference_layout) {
            // Add the padding to the view group after determining if the padding already exists.
            if (!mPaddingApplied) {
                topPadding += mTopPadding;
                bottomPadding += mBottomPadding;
                mPaddingApplied = true;
            }
        } else {
            if (mPaddingApplied) {
                topPadding -= mTopPadding;
                bottomPadding -= mBottomPadding;
                mPaddingApplied = false;
            }
        }
        int previousTop = getPaddingTop();
        int previousBottom = getPaddingBottom();
        if (previousTop != topPadding || previousBottom != bottomPadding) {
            setPadding(getPaddingLeft(), topPadding, getPaddingRight(), bottomPadding);
        }
        super.addView(child);
    }
}
 No newline at end of file
+1 −3
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@

        </LinearLayout>

        <FrameLayout android:id="@+id/prefs"
        <android.preference.PreferenceFrameLayout android:id="@+id/prefs"
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="20"
@@ -66,8 +66,6 @@
                android:layout_marginBottom="16dp"
                android:paddingLeft="32dip"
                android:paddingRight="32dip"
                android:paddingTop="48dip"
                android:paddingBottom="48dip"
                android:background="?attr/preferencePanelBackground"
                android:visibility="gone" />
    </LinearLayout>
+4 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/default_preference_layout"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
@@ -27,6 +28,9 @@
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:paddingTop="48dip"
        android:paddingBottom="48dip"
        android:clipToPadding="false"
        android:drawSelectorOnTop="false"
        android:cacheColorHint="@android:color/transparent"
        android:scrollbarAlwaysDrawVerticalTrack="true" />
+9 −0
Original line number Diff line number Diff line
@@ -610,6 +610,9 @@

        <!-- Specifies a drawable to use for the 'home as up' indicator. -->
        <attr name="homeAsUpIndicator" format="reference" />

        <!-- Preference frame layout styles. -->
        <attr name="preferenceFrameLayoutStyle" format="reference" />
    </declare-styleable>

    <!-- **************************************************************** -->
@@ -2135,6 +2138,12 @@
        <!-- Drawable to draw below list content. -->
        <attr name="overScrollFooter" format="reference|color" />
    </declare-styleable>
    <declare-styleable name="PreferenceFrameLayout">
        <!-- Padding to use at the top of the prefs content. -->
        <attr name="topPadding" format="dimension" />
        <!-- Padding to use at the bottom of the prefs content. -->
        <attr name="bottomPadding" format="dimension" />
    </declare-styleable>
    <declare-styleable name="MenuView">
        <!-- Default appearance of menu item text. -->
        <attr name="itemTextAppearance" format="reference" />
+11 −2
Original line number Diff line number Diff line
@@ -49,6 +49,11 @@
        <item name="centerMedium">@android:drawable/popup_center_medium</item>
    </style>

    <style name="Widget.PreferenceFrameLayout">
        <item name="android:topPadding">0dip</item>
        <item name="android:bottomPadding">0dip</item>
    </style>

    <!-- Base style for animations.  This style specifies no animations. -->
    <style name="Animation" />

@@ -1874,4 +1879,8 @@
        <item name="android:textAppearance">@style/TextAppearance.Holo.Light.DialogWindowTitle</item>
    </style>

    <style name="Widget.Holo.PreferenceFrameLayout">
        <item name="android:topPadding">48dip</item>
        <item name="android:bottomPadding">48dip</item>
    </style>
</resources>
Loading