168 | * NOTE that this will return the internal {@link OnBindEditTextListener} instead of the one set
169 | * via {@link #setOnBindEditTextListener(OnBindEditTextListener)}.
170 | *
171 | * @return The {@link OnBindEditTextListener} set for this preference, or {@code null} if
172 | * there is no OnBindEditTextListener set
173 | * @see OnBindEditTextListener
174 | */
175 | @Nullable
176 | //@Override
177 | public OnBindEditTextListener getOnBindEditTextListener() {
178 | return this.onBindEditTextListener;
179 | //return super.getOnBindEditTextListener();
180 | }
181 |
182 | @Override
183 | public void setOnBindEditTextListener(@Nullable OnBindEditTextListener onBindEditTextListener) {
184 | this.onBindEditTextListener = onBindEditTextListener;
185 | }
186 |
187 | @Deprecated
188 | public EditText getEditText() {
189 | throw new UnsupportedOperationException("Use OnBindEditTextListener to modify the EditText");
190 | }
191 |
192 | @Override
193 | public void setText(String text) {
194 | String oldText = getText();
195 | super.setText(text);
196 | if (!TextUtils.equals(text, oldText)) {
197 | notifyChanged();
198 | }
199 | }
200 | }
201 |
--------------------------------------------------------------------------------
/preferencex/src/main/java/com/takisoft/preferencex/PreferenceActivityResultListener.java:
--------------------------------------------------------------------------------
1 | package com.takisoft.preferencex;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import androidx.annotation.NonNull;
6 | import androidx.annotation.Nullable;
7 | import androidx.fragment.app.Fragment;
8 | import androidx.preference.Preference;
9 |
10 | /**
11 | * An interface for custom preferences that want to handle click events by either starting an
12 | * {@link Activity} for results calling {@link Fragment#startActivityForResult(Intent, int)} or
13 | * using the supplied {@link PreferenceFragmentCompat} fragment manually (e.g. adding a fragment to
14 | * it).
15 | */
16 | public interface PreferenceActivityResultListener {
17 | /**
18 | * Called when the user clicks on the preference.
19 | *
20 | * @param fragment The preference fragment that shows the preference screen.
21 | * @param preference The preference instance.
22 | */
23 | void onPreferenceClick(@NonNull PreferenceFragmentCompat fragment, @NonNull Preference preference);
24 |
25 | /**
26 | * Called when an activity you launched exits, giving you the requestCode
27 | * you started it with, the resultCode it returned, and any additional
28 | * data from it. The resultCode will be
29 | * {@link Activity#RESULT_CANCELED} if the activity explicitly returned that,
30 | * didn't return any result, or crashed during its operation.
31 | *
32 | * @param requestCode The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.
33 | * @param resultCode The integer result code returned by the child activity through its setResult().
34 | * @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
35 | */
36 | void onActivityResult(int requestCode, int resultCode, @Nullable Intent data);
37 | }
38 |
--------------------------------------------------------------------------------
/preferencex/src/main/java/com/takisoft/preferencex/PreferenceCategory.java:
--------------------------------------------------------------------------------
1 | package com.takisoft.preferencex;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.os.Build;
6 | import android.text.TextUtils;
7 | import android.util.AttributeSet;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.TextView;
11 |
12 | import androidx.annotation.ColorInt;
13 | import androidx.annotation.ColorRes;
14 | import androidx.core.content.res.TypedArrayUtils;
15 | import androidx.preference.PreferenceViewHolder;
16 | import androidx.recyclerview.widget.RecyclerView;
17 |
18 | /**
19 | * An extended {@link androidx.preference.PreferenceCategory} that allows the user to set the color
20 | * and the visibility of the title.
21 | */
22 | public class PreferenceCategory extends androidx.preference.PreferenceCategory {
23 | private static final int[] CATEGORY_ATTRS = new int[]{R.attr.colorAccent};
24 |
25 | protected int color;
26 | protected View itemView;
27 |
28 | public PreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
29 | super(context, attrs, defStyleAttr, defStyleRes);
30 |
31 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PreferenceCategory, defStyleAttr, 0);
32 | color = a.getColor(R.styleable.PreferenceCategory_pref_categoryColor, 0);
33 | a.recycle();
34 | }
35 |
36 | public PreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
37 | this(context, attrs, defStyleAttr, 0);
38 | }
39 |
40 | public PreferenceCategory(Context context, AttributeSet attrs) {
41 | this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceCategoryStyle, android.R.attr.preferenceCategoryStyle));
42 | }
43 |
44 | public PreferenceCategory(Context context) {
45 | this(context, null);
46 | }
47 |
48 | private void setTitleVisibility(View itemView, boolean isVisible) {
49 | if (itemView == null) {
50 | return;
51 | }
52 |
53 | final RecyclerView.LayoutParams currentParams = (RecyclerView.LayoutParams) itemView.getLayoutParams();
54 | final RecyclerView.LayoutParams param;
55 |
56 | final boolean wasHidden = itemView.getTag() != null && currentParams.width == 0;
57 |
58 | if (itemView.getTag() == null) {
59 | param = new RecyclerView.LayoutParams((ViewGroup.MarginLayoutParams) currentParams);
60 | itemView.setTag(param);
61 | } else {
62 | param = (RecyclerView.LayoutParams) itemView.getTag();
63 | }
64 |
65 | if (isVisible) {
66 | if (itemView.getVisibility() == View.GONE || wasHidden) {
67 | currentParams.width = param.width;
68 | currentParams.height = param.height;
69 | currentParams.leftMargin = param.leftMargin;
70 | currentParams.rightMargin = param.rightMargin;
71 | currentParams.topMargin = param.topMargin;
72 | currentParams.bottomMargin = param.bottomMargin;
73 | itemView.setVisibility(View.VISIBLE);
74 | }
75 | } else {
76 | if (itemView.getVisibility() == View.VISIBLE || !wasHidden) {
77 | currentParams.width = 0;
78 | currentParams.height = 0;
79 | currentParams.leftMargin = 0;
80 | currentParams.rightMargin = 0;
81 | currentParams.topMargin = 0;
82 | currentParams.bottomMargin = 0;
83 | itemView.setVisibility(View.GONE);
84 | }
85 | }
86 | }
87 |
88 | @Override
89 | public void setTitle(CharSequence title) {
90 | super.setTitle(title);
91 |
92 | setTitleVisibility(itemView, !TextUtils.isEmpty(getTitle()));
93 | }
94 |
95 | public void setColor(@ColorInt int color) {
96 | this.color = color;
97 | }
98 |
99 | public void setColorResource(@ColorRes int resId) {
100 | int color;
101 |
102 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
103 | color = getContext().getResources().getColor(resId);
104 | } else {
105 | color = getContext().getColor(resId);
106 | }
107 |
108 | setColor(color);
109 | }
110 |
111 | @Override
112 | public void onBindViewHolder(PreferenceViewHolder holder) {
113 | super.onBindViewHolder(holder);
114 |
115 | itemView = holder.itemView;
116 |
117 | TextView titleView = (TextView) holder.findViewById(android.R.id.title);
118 |
119 | if (titleView != null) {
120 | final TypedArray typedArray = getContext().obtainStyledAttributes(CATEGORY_ATTRS);
121 |
122 | if (typedArray.length() > 0 && typedArray.getIndexCount() > 0) {
123 | final int accentColor = typedArray.getColor(typedArray.getIndex(0), 0xff4081); // defaults to pink
124 | titleView.setTextColor(color == 0 ? accentColor : color);
125 | }
126 |
127 | typedArray.recycle();
128 | }
129 |
130 | boolean isVisible = !TextUtils.isEmpty(getTitle());
131 | setTitleVisibility(holder.itemView, isVisible);
132 | /*if (!isVisible) {
133 | return;
134 | }*/
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/preferencex/src/main/java/com/takisoft/preferencex/SwitchPreferenceCompat.java:
--------------------------------------------------------------------------------
1 | package com.takisoft.preferencex;
2 |
3 |
4 | import android.annotation.SuppressLint;
5 | import android.content.Context;
6 | import android.content.res.ColorStateList;
7 | import android.content.res.TypedArray;
8 | import android.graphics.drawable.Drawable;
9 | import android.util.AttributeSet;
10 | import android.view.View;
11 |
12 | import androidx.appcompat.content.res.AppCompatResources;
13 | import androidx.preference.PreferenceViewHolder;
14 |
15 | public class SwitchPreferenceCompat extends androidx.preference.SwitchPreferenceCompat {
16 | private static final int[] ATTRS = new int[]{androidx.appcompat.R.attr.controlBackground, R.attr.colorControlNormal};
17 |
18 | private final View.OnClickListener contentClickListener = new View.OnClickListener() {
19 | @SuppressLint("RestrictedApi")
20 | @Override
21 | public void onClick(View v) {
22 | performClick((View) v.getParent());
23 | }
24 | };
25 |
26 | private final View.OnClickListener widgetClickListener = new View.OnClickListener() {
27 | @Override
28 | public void onClick(View v) {
29 | final boolean newValue = !isChecked();
30 |
31 | if (callChangeListener(newValue)) {
32 | setChecked(newValue);
33 | }
34 | }
35 | };
36 |
37 | private boolean withSeparator = false;
38 |
39 | {
40 | refreshWithSeparator(false);
41 | }
42 |
43 | public SwitchPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
44 | super(context, attrs, defStyleAttr, defStyleRes);
45 | }
46 |
47 | public SwitchPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) {
48 | super(context, attrs, defStyleAttr);
49 | }
50 |
51 | public SwitchPreferenceCompat(Context context, AttributeSet attrs) {
52 | super(context, attrs);
53 | }
54 |
55 | public SwitchPreferenceCompat(Context context) {
56 | super(context);
57 | }
58 |
59 | void refresh() {
60 | if (!withSeparator) {
61 | return;
62 | }
63 |
64 | boolean value = getPersistedBoolean(false);
65 | boolean oldPersistent = isPersistent();
66 |
67 | setPersistent(false);
68 | setChecked(value);
69 | setPersistent(oldPersistent);
70 | /*boolean old = mChecked;
71 | mChecked = getPersistedBoolean(false);
72 |
73 | if (old != mChecked) {
74 | notifyDependencyChange(shouldDisableDependents());
75 | notifyChanged();
76 | }*/
77 | }
78 |
79 | @Override
80 | protected void onClick() {
81 | if (!withSeparator) {
82 | super.onClick();
83 | }
84 | }
85 |
86 | private void refreshWithSeparator(boolean changeLayout) {
87 | if (setWithSeparator(getFragment() != null) && changeLayout) {
88 | notifyHierarchyChanged();
89 | }
90 | }
91 |
92 | private boolean setWithSeparator(boolean withSeparator) {
93 | if (this.withSeparator == withSeparator) {
94 | return false;
95 | }
96 |
97 | this.withSeparator = withSeparator;
98 | if (withSeparator) {
99 | setLayoutResource(R.layout.preference_material_ext);
100 | } else {
101 | setLayoutResource(androidx.preference.R.layout.preference_material);
102 | }
103 |
104 | return true;
105 | }
106 |
107 | @Override
108 | public void setFragment(String fragment) {
109 | super.setFragment(fragment);
110 | refreshWithSeparator(true);
111 | }
112 |
113 | @Override
114 | public void onBindViewHolder(PreferenceViewHolder holder) {
115 | super.onBindViewHolder(holder);
116 |
117 | if (withSeparator) {
118 | holder.findViewById(android.R.id.widget_frame).setOnClickListener(widgetClickListener);
119 | holder.findViewById(R.id.pref_content_frame).setOnClickListener(contentClickListener);
120 | //?attr/controlBackground
121 | //androidx.appcompat.R.attr.controlBackground
122 |
123 | final TypedArray typedArray = getContext().obtainStyledAttributes(ATTRS);
124 |
125 | if (typedArray.length() > 0 && typedArray.getIndexCount() > 0) {
126 | int id = typedArray.getResourceId(0, 0);
127 | if (id != 0) {
128 | Drawable bgDrawable = AppCompatResources.getDrawable(getContext(), id);
129 | holder.findViewById(androidx.preference.R.id.switchWidget).setBackgroundDrawable(bgDrawable);
130 | }
131 |
132 | ColorStateList separatorColor = typedArray.getColorStateList(1);
133 | if (separatorColor != null) {
134 | int[] stateSet = isEnabled() ? new int[]{android.R.attr.state_enabled} : new int[]{-android.R.attr.state_enabled};
135 | int dividerColor = separatorColor.getColorForState(stateSet, separatorColor.getDefaultColor());
136 | holder.findViewById(R.id.pref_separator).setBackgroundColor(dividerColor);
137 | }
138 | }
139 |
140 | typedArray.recycle();
141 | }
142 |
143 | holder.itemView.setClickable(!withSeparator);
144 | holder.itemView.setFocusable(!withSeparator);
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/preferencex/src/main/res/layout/preference_list_master_switch.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |