What is the best way of configuring a widget in android for entering a 4 digit PIN?
Currently I have:
<EditTextPreference
android:title="PIN"
android:summary="Your PIN number"
android:key="password"
android:numeric="integer"
android:maxLength="4"
android:password="true"
/>
This works quite well, but there is no way of constraining the input to be at least 4 digits. Is this how you would have someone set a PIN in preferences? Or is there a better way?
By the way, before people comment on security implications, I didn't choose the 4 digit PIN, it is for connecting to a third party system and I can't change it. And no, this isn't for banking.
You can add an OnPreferenceChangeListener and check the password length there.
E.g. if using a PreferenceActivity:
findPreference("password").setOnPreferenceChangeListener( new OnPreferenceChangeListener(){
public boolean onPreferenceChange (Preference preference, Object newValue){
if ((String)newValue).length == 4)
super.onPreferenceChange(preference, newValue);
else
Toast.makeText(this, "The PIN must have 4 digits", Toast.LENGTH_LONG).show();
}
});
There is an interesting View in Github that makes the coding of the 4 entry quite simple and easy to use. Check source
Related
This is separate from whether the app has requested an editor action: usually the editor action is displayed as a replacement for the enter key, but it's possible for an input method to offer both as alternatives.
The simple answer is, of course, "when it's possible to enter a newline". When a TextView is in single-line mode, it's not possible to enter a newline even if the keyboard shows a key: as I found in this answer, Android itself treats the key as an editor action and substitutes a zero-width space for any newlines added to the TextView.
How can the input method tell whether newlines will be respected, or otherwise whether it's appropriate to show an enter key (as an alternative to any specified editor action)?
Your best bet would be to check the EditorInfo object returned using the getCurrentInputEditorInfo() and take a best guess if showing the Enter key is apt or not.
I have an Android application using default values from the Preferences Android framework.
It all works fine except for phone numbers (defined with android:inputType="phone" in preferences.xml).
The phone numbers get treated as numeric value so if I go to the preferences screen to see the default values I see
3.3631241E10
for the value defined in preferences.xml as
android:defaultValue="+33631241234"
To avoid this problem, I have used values from strings.xml defining the default values in preferences.xml like this :
android:defaultValue="+33631241234
It works ... but I don't like it: that's a source of problem as I need to redefine the same phone number for each language used ! !
I must be doing something wrong as I haven't found anyone else with the same problem on internet however I don't see what I am doing wrong ! !
Any help would be really appreciated.
that's a source of problem as I need to redefine the same phone number for each language used
you don't - when the text is absent for a language the default is used - docs :
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml.
I have a preference to get port of a service as shown below
<EditTextPreference
android:defaultValue="4444"
android:key="port"
android:title="Port"
android:dependency="service_on"
android:inputType="number"
android:maxLength="4"
/>
I have restricted it to maxLength 4 but there does not seem to be a minlength or minvalue ??
Basically I want to avoid empty values or lower value ports to avoid conflicts of port already in use/ android allocated ports etc
Any ideas how can I smartly changed the xml only without having to check on callbacks/listeners ?
Thanks,
Hi in my project i have a edit text box, where i want the user to enter his phone number, I need it in a format like +0000000000 no need to allow - or any other symbols. I need to restrict the user while entering in keyboard. I have implemented the regular expression for validation for this format and I am using android:inputType="phone" is there any other input Type is available or can i customize it in any way. Looking for help pls...
Try it android:digits="+0123456789"
try this::
android:inputType="number"
it allow user to enter only integer value
I am essentially trying to set the digits value of an EditText programmatically. So far I have:
weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
weightInput.setKeyListener(DigitsKeyListener.getInstance());
Which is fine, but I also want to be able to include a decimal place (.). Any ideas?
Try this:
<EditText
android:inputType="number"
android:digits="0123456789."
/>
From Code:
weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
But, it allows the user to include several "."
See JoeyRA's answer for real numbers.
Try this:
weightInput.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
weightInput.setKeyListener(DigitsKeyListener.getInstance(false,true));
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
Use InputType.TYPE_NUMBER_FLAG_DECIMAL.
Also see: Input Types.
if anyone still finding proper way, note that its setRawInputType() not setInputType()
val digits = "ABCDabcd" // or any characters you want to allow
editText.keyListener = DigitsKeyListener.getInstance(digits)
editText.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME)
For IP address input ( Multiple dots and numbers )
try
<EditText
android:id="#+id/ipBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/ipAddrHint"
android:inputType="numberDecimal|number"
android:digits="0123456789."
android:textSize="30sp" />
None of the above solutions worked as expected. Digits in xml file still allow me to put "," in the input.
Check for it, it's works for me:
edittext.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);