diff --git a/app/src/main/res/layout/edit_password.xml b/app/src/main/res/layout/edit_password.xml new file mode 100644 index 0000000..8b6c133 --- /dev/null +++ b/app/src/main/res/layout/edit_password.xml @@ -0,0 +1,16 @@ + + + + diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index 7879618..71556c6 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -78,6 +78,7 @@ android:persistent="false" android:summary="Should be a number password input (it is now!)" android:title="AutoSummaryEditTextPreference" + app:pref_editLayout="@layout/edit_password" app:pref_summaryHasText="%s is a secret number" app:pref_summaryPasswordSubstitute="\u00A9" app:pref_summaryPasswordSubstituteLength="0" /> diff --git a/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreference.java b/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreference.java index d8c5123..494cd6f 100644 --- a/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreference.java +++ b/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreference.java @@ -1,12 +1,19 @@ package com.takisoft.preferencex; import android.content.Context; + +import androidx.annotation.LayoutRes; +import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatEditText; + +import android.content.res.TypedArray; import android.text.TextUtils; import android.util.AttributeSet; import android.widget.EditText; public class EditTextPreference extends androidx.preference.EditTextPreference { + @LayoutRes + private int editLayout; private EditText editText; public EditTextPreference(Context context) { @@ -23,10 +30,23 @@ public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - editText = new AppCompatEditText(context, attrs); - editText.setId(android.R.id.edit); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextPreference, defStyleAttr, 0); + editLayout = a.getResourceId(R.styleable.EditTextPreference_pref_editLayout, 0); + a.recycle(); + + if (editLayout == 0) { + editText = new AppCompatEditText(context, attrs); + editText.setId(android.R.id.edit); + } + } + + @LayoutRes + public int getEditLayout() { + return editLayout; } + @Nullable public EditText getEditText() { return editText; } diff --git a/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreferenceDialogFragmentCompat.java b/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreferenceDialogFragmentCompat.java index 3bc354b..21c7f9b 100644 --- a/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreferenceDialogFragmentCompat.java +++ b/preferencex/src/main/java/com/takisoft/preferencex/EditTextPreferenceDialogFragmentCompat.java @@ -20,9 +20,11 @@ package com.takisoft.preferencex; +import android.content.Context; import android.os.Bundle; import androidx.preference.PreferenceDialogFragmentCompat; import android.text.Editable; +import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; @@ -42,10 +44,19 @@ public static EditTextPreferenceDialogFragmentCompat newInstance(String key) { return fragment; } - protected void onBindDialogView(View view) { - super.onBindDialogView(view); + @Override + protected View onCreateDialogView(Context context) { + View view = super.onCreateDialogView(context), oldChild = view.findViewById(android.R.id.edit); + if (oldChild == null) { + throw new IllegalStateException("Dialog view must contain an EditText with id" + + " @android:id/edit"); + } - this.mEditText = getEditTextPreference().getEditText(); + ViewGroup container = (ViewGroup) oldChild.getParent(); + int layout = getEditTextPreference().getEditLayout(); + View newChild = layout == 0 ? getEditTextPreference().getEditText() + : LayoutInflater.from(context).inflate(layout, container, false); + this.mEditText = newChild.findViewById(android.R.id.edit); this.mEditText.setText(this.getEditTextPreference().getText()); Editable text = mEditText.getText(); @@ -53,14 +64,18 @@ protected void onBindDialogView(View view) { mEditText.setSelection(text.length(), text.length()); } - ViewParent oldParent = this.mEditText.getParent(); + ViewParent oldParent = newChild.getParent(); if (oldParent != view) { if (oldParent != null) { - ((ViewGroup) oldParent).removeView(this.mEditText); + ((ViewGroup) oldParent).removeView(newChild); } - this.onAddEditTextToDialogView(view, this.mEditText); + if (container != null) { + container.removeView(oldChild); + container.addView(newChild, oldChild.getLayoutParams()); + } } + return view; } private EditTextPreference getEditTextPreference() { @@ -71,18 +86,6 @@ protected boolean needInputMethod() { return true; } - protected void onAddEditTextToDialogView(View dialogView, EditText editText) { - //ViewGroup container = (ViewGroup) dialogView.findViewById(androidx.preference.R.id.edittext_container); - View oldEditText = dialogView.findViewById(android.R.id.edit); - if (oldEditText != null) { - ViewGroup container = (ViewGroup) (oldEditText.getParent()); - if (container != null) { - container.removeView(oldEditText); - container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - } - } - } - public void onDialogClosed(boolean positiveResult) { if (positiveResult) { String value = this.mEditText.getText().toString(); diff --git a/preferencex/src/main/res/values/attrs.xml b/preferencex/src/main/res/values/attrs.xml index 74e0f8d..94888f5 100644 --- a/preferencex/src/main/res/values/attrs.xml +++ b/preferencex/src/main/res/values/attrs.xml @@ -4,6 +4,10 @@ + + + +