Skip to content

Commit

Permalink
Changed the theme, the attributes of a PreferenceFragment are obtaine…
Browse files Browse the repository at this point in the history
…d from.
  • Loading branch information
michael-rapp committed Oct 25, 2017
1 parent c2c6ee8 commit 752a96b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -129,39 +128,33 @@ public abstract class PreferenceFragment extends AbstractPreferenceFragment {
private int buttonBarElevation;

/**
* The color of dividers, which are contained by the fragment.
* Obtains all relevant attributes from the activity's current theme.
*/
private int dividerColor;
private void obtainStyledAttributes() {
obtainShowRestoreDefaultsButton();
obtainRestoreDefaultsButtonText();
obtainButtonBarBackground();
obtainButtonBarElevation();
}

/**
* Obtains, whether the button, which allows to restore the preferences' default values, should
* be shown, or not, from a specific theme.
*
* @param themeResourceId
* The resource id of the theme, the boolean value should be obtained from, as an {@link
* Integer} value
*/
private void obtainShowRestoreDefaultsButton(@StyleRes final int themeResourceId) {
boolean show = ThemeUtil
.getBoolean(getActivity(), themeResourceId, R.attr.showRestoreDefaultsButton,
false);
* be shown, or not, from the activity's current theme.
*/
private void obtainShowRestoreDefaultsButton() {
boolean show = ThemeUtil.getBoolean(getActivity(), R.attr.showRestoreDefaultsButton, false);
showRestoreDefaultsButton(show);
}

/**
* Obtains the text of the button, which allows to restore the preferences' default values, from
* a specific theme.
*
* @param themeResourceId
* The resource id of the theme, the text should be obtained from, as an {@link Integer}
* value
* the activity's current theme.
*/
private void obtainRestoreDefaultsButtonText(@StyleRes final int themeResourceId) {
private void obtainRestoreDefaultsButtonText() {
CharSequence text;

try {
text = ThemeUtil
.getText(getActivity(), themeResourceId, R.attr.restoreDefaultsButtonText);
text = ThemeUtil.getText(getActivity(), R.attr.restoreDefaultsButtonText);
} catch (NotFoundException e) {
text = getText(R.string.restore_defaults_button_text);
}
Expand All @@ -170,20 +163,16 @@ private void obtainRestoreDefaultsButtonText(@StyleRes final int themeResourceId
}

/**
* Obtains the background of the button bar from a specific theme.
*
* @param themeResourceId
* The resource id of the theme, the background should be obtained from, as an {@link
* Integer} value
* Obtains the background of the button bar from the activity's current theme.
*/
private void obtainButtonBarBackground(@StyleRes final int themeResourceId) {
private void obtainButtonBarBackground() {
try {
int color = ThemeUtil.getColor(getActivity(), themeResourceId,
R.attr.restoreDefaultsButtonBarBackground);
int color =
ThemeUtil.getColor(getActivity(), R.attr.restoreDefaultsButtonBarBackground);
setButtonBarBackgroundColor(color);
} catch (NotFoundException e) {
int resourceId = ThemeUtil.getResId(getActivity(), themeResourceId,
R.attr.restoreDefaultsButtonBarBackground, -1);
int resourceId = ThemeUtil
.getResId(getActivity(), R.attr.restoreDefaultsButtonBarBackground, -1);

if (resourceId != -1) {
setButtonBarBackground(resourceId);
Expand All @@ -195,45 +184,21 @@ private void obtainButtonBarBackground(@StyleRes final int themeResourceId) {
}

/**
* Obtains the elevation of the button bar from a specific theme.
*
* @param themeResourceId
* The resource id of the theme, the elevation should be obtained from, as an {@link
* Integer} value
* Obtains the elevation of the button bar from the activity's current theme.
*/
private void obtainButtonBarElevation(@StyleRes final int themeResourceId) {
private void obtainButtonBarElevation() {
int elevation;

try {
elevation = ThemeUtil.getDimensionPixelSize(getActivity(), themeResourceId,
R.attr.restoreDefaultsButtonBarElevation);
elevation = ThemeUtil
.getDimensionPixelSize(getActivity(), R.attr.restoreDefaultsButtonBarElevation);
} catch (NotFoundException e) {
elevation = getResources().getDimensionPixelSize(R.dimen.button_bar_elevation);
}

setButtonBarElevation(pixelsToDp(getActivity(), elevation));
}

/**
* Obtains the color of the dividers, which are contained by the fragment, from a specific
* theme.
*
* @param themeResourceId
* The resource id of the theme, the color should be obtained from, as an {@link
* Integer} value
*/
private void obtainDividerColor(@StyleRes final int themeResourceId) {
int color;

try {
color = ThemeUtil.getColor(getActivity(), themeResourceId, R.attr.dividerColor);
} catch (NotFoundException e) {
color = ContextCompat.getColor(getActivity(), R.color.preference_divider_color_light);
}

setDividerColor(color);
}

/**
* Handles the arguments, which have been passed to the fragment.
*/
Expand Down Expand Up @@ -654,6 +619,7 @@ public final void setButtonBarElevation(final int elevation) {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
obtainStyledAttributes();
handleArguments();
}

Expand Down Expand Up @@ -684,14 +650,4 @@ protected final View onInflateView(@NonNull final LayoutInflater inflater,
return frameLayout;
}

@Override
protected final void onObtainStyledAttributes(@StyleRes final int themeResourceId) {
super.onObtainStyledAttributes(themeResourceId);
obtainShowRestoreDefaultsButton(themeResourceId);
obtainRestoreDefaultsButtonText(themeResourceId);
obtainButtonBarBackground(themeResourceId);
obtainButtonBarElevation(themeResourceId);
obtainDividerColor(themeResourceId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@
*/
package de.mrapp.android.preference.activity.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.TypedArray;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import de.mrapp.android.preference.activity.R;
import de.mrapp.android.preference.activity.view.PreferenceListView;
import de.mrapp.android.util.ThemeUtil;

/**
* An abstract base class for all fragments, which show multiple preferences.
Expand All @@ -47,48 +45,29 @@ public abstract class AbstractPreferenceFragment extends android.preference.Pref
/**
* The color of the dividers which are shown above preference categories.
*/
private int dividerColor;
private int dividerColor = -1;

/**
* Obtains all relevant attributes from the activity's current theme.
*/
private void obtainStyledAttributes() {
int themeResourceId = obtainThemeResourceId();

if (themeResourceId != -1) {
onObtainStyledAttributes(themeResourceId);
}
obtainDividerColor();
}

/**
* Obtains the resource id of the activity's current theme.
*
* @return The resource id of the activity's current theme as an {@link Integer} value or 0, if
* an error occurred while obtaining the theme
* Obtains the color of the dividers, which are shown above preference categories, from the
* activity's theme.
*/
private int obtainThemeResourceId() {
private void obtainDividerColor() {
int color;

try {
String packageName = getActivity().getClass().getPackage().getName();
PackageInfo packageInfo = getActivity().getPackageManager()
.getPackageInfo(packageName, PackageManager.GET_META_DATA);
return packageInfo.applicationInfo.theme;
} catch (NameNotFoundException e) {
return -1;
color = ThemeUtil.getColor(getActivity(), R.attr.dividerColor);
} catch (NotFoundException e) {
color = ContextCompat.getColor(getActivity(), R.color.preference_divider_color_light);
}
}

/**
* Obtains the color of the dividers, which are shown above preference categories, from a
* specific theme.
*
* @param themeResourceId
* The resource id of the theme, the color should be obtained from, as an {@link
* Integer} value
*/
private void obtainDividerColor(@StyleRes final int themeResourceId) {
TypedArray typedArray = getActivity().getTheme()
.obtainStyledAttributes(themeResourceId, new int[]{R.attr.dividerColor});
setDividerColor(typedArray.getColor(0, -1));
setDividerColor(color);
}

/**
Expand Down Expand Up @@ -121,20 +100,6 @@ protected abstract View onInflateView(@NonNull final LayoutInflater inflater,
@Nullable final ViewGroup parent,
@Nullable final Bundle savedInstanceState);

/**
* The method, which is invoked in order to obtain relevant attributes from the activity's
* current theme. This method may be overriden by subclasses in order to obtain additional
* attributes.
*
* @param themeResourceId
* The resource id of the theme, the attributes should be obtained from, as an {@link
* Integer} value
*/
@CallSuper
protected void onObtainStyledAttributes(@StyleRes final int themeResourceId) {
obtainDividerColor(themeResourceId);
}

/**
* Returns the list view, which is used to show the fragment's preferences.
*
Expand Down

0 comments on commit 752a96b

Please sign in to comment.