Reputation: 273
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.
Upvotes: 10
Views: 5486
Reputation: 389
#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);
}
}
Upvotes: 0
Reputation: 1451
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);
}
});
}
Upvotes: 1
Reputation: 871
You can find an answer to your question here
Basically, you will need to import androidX library and follow my code.
Upvotes: 0
Reputation: 760
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):
You can apparently use one or more of these, separated by a |
(I've never done this though).
Upvotes: 3
Reputation: 38585
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
Upvotes: 2