How can we use android:inputType in EditTextPreference?
Asked Answered
S

5

10

I checked the documentation of EditTextPreference http://developer.android.com/reference/android/preference/EditTextPreference.html

But I failed to found the android:inputType attribute there. Then how it can be used in this code segment

<EditTextPreference
        android:key="edit"
        android:title="@string/location1"
        android:summary="@string/summary1"
        android:dialogTitle="@string/location1"
        android:dialogMessage="@string/message"
        android:inputType="text"
        android:singleLine="true"
        />

Same doubt for the android:singleLine attribute.

Scarlet answered 10/7, 2015 at 21:17 Comment(2)
Is android:summary ignored? I cannot see where its used.Residue
Does this answer your question? EditText, inputType values (XML)Seel
H
3

The docs don't list the attributes for that class, but the InputType attribute (and other EditText and TextView attributes) still work. It's only stated in the text. Also see this related question.

The EditTextPreference documentation doesn't explicitly list all the attributes it supports, but the text states:

See EditText Attributes.

The link there isn't very useful (they probably reorganised some of the attributes but never updated some links to it), but here's a direct link to inputType values. As a quick summary those values are (as of the time of posting):

  • none
  • text
  • textCapCharacters
  • textCapWords
  • textCapSentences
  • textAutoCorrect
  • textAutoComplete
  • textMultiLine
  • textImeMultiLine
  • textNoSuggestions
  • textUri
  • textEmailAddress
  • textEmailSubject
  • textShortMessage
  • textLongMessage
  • textPersonName
  • textPostalAddress
  • textPassword
  • textVisiblePassword
  • textWebEditText
  • textFilter
  • textPhonetic
  • textWebEmailAddress
  • textWebPassword
  • number
  • numberSigned
  • numberDecimal
  • numberPassword
  • phone
  • datetime
  • date
  • time

You can apparently use one or more of these, separated by a | (I've never done this though).

Halfslip answered 10/7, 2015 at 21:40 Comment(0)
M
2

You can't do it from XML, but EditTextpreference exposes the EditText so you can do it programmatically. After you load the preferences in your Activity/Fragment, you can do:

EditTextPreference pref = (EditTextPreference) PreferenceManager.findPreference("edit");
EditText prefEditText = pref.getEditText();
prefEditText.setInputType(InputType.TYPE_CLASS_TEXT);
prefEditText.setSingleLine(true);
// etc
Manciple answered 10/7, 2015 at 21:30 Comment(0)
L
1

Using AndroidX, to set input type to password for example:

root_preferences.xml:

<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:title="Password"
        android:key="my_pref_password"/>
</androidx.preference.PreferenceScreen>

And in your Setting Fragment:

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.root_preferences, rootKey);


    EditTextPreference pref = findPreference("my_pref_password");
    pref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
        @Override
        public void onBindEditText(@NonNull EditText editText) {
            editText.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    });
}
Lobbyist answered 13/8, 2019 at 10:38 Comment(0)
A
0

You can find an answer to your question here

Basically, you will need to import androidX library and follow my code.

Aimee answered 25/6, 2019 at 15:15 Comment(0)
G
0

#Simplest and Tested Answer#

For Only Decimal Value in EditTextPreference

Write this code in onCreatePreferences

Make sure use both CLASS and Attributes of that class

Example

InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL

public static class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);

        EditTextPreference new_bonus= findPreference("new_bonus");
        EditTextPreference old_bonus= findPreference("old_bonus");


        EditTextPreference.OnBindEditTextListener onBindEditTextListener = new EditTextPreference.OnBindEditTextListener() {
            @Override
            public void onBindEditText(@NonNull EditText editText) {
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            }
        };

        old_bonus.setOnBindEditTextListener(onBindEditTextListener);
        new_bonus.setOnBindEditTextListener(onBindEditTextListener);

    }
}
Griffon answered 15/5, 2021 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.