I want an EditText to contain exactly 8 digits (like bank accounts). Of course in addition to :
android:inputType="number"
Is there an option in XML or do I have to manage this programmatically ?
Programmatically.
Also, you can save time by using existing library EditText with validation. You can use a regex type with following pattern [0-9]{8}.
I think there is no way just in xml.
You have to validate it programmatically.
#Override
public void afterTextChanged(Editable s) {
// validation code goes here
}
This is my phone edit text:
<EditText
style="#style/ObligatoryEditTextStyle"
android:id="#+id/etxtClientPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:gravity="left"
android:textColor="#color/hub_blue_normal"
android:visibility="visible"
android:selectAllOnFocus="true"
android:digits="0123456789"
android:maxLength="8"/>
Tell me if I helped you and good programming!!
Try the following line in your EditText:
android:maxLength="8"
Change input type in decimal, then set maxLength="8".
android:inputType="decimal"
android:maxLength="8"
Related
Modifying an APK to try to make something slightly more user friendly. There is a login screen with username and password and a server portal field. The portal field now never needs to change. So I want to set a default text so the user does not have to type in the full portal URL
Currently looks like this
<EditText android:textSize="15.0dip"
android:textColor="#color/black"
android:textColorHint="#color/white"
android:id="#id/et_sever_url"
android:background="#drawable/selector_login_fields"
android:paddingLeft="20.0dip"
android:paddingRight="20.0dip"
android:focusable="true"
android:nextFocusUp="#id/et_password"
android:nextFocusDown="#id/bt_submit"
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:layout_marginLeft="25.0dip"
android:layout_marginTop="20.0dip"
android:layout_marginRight="25.0dip"
android:text="#string/serverurl"
android:hint="#string/serverurl"
android:maxLines="1" android:lines="1"
android:layout_below="#id/et_password"
android:layout_centerHorizontal="true"
android:inputType="textUri"
android:textCursorDrawable="#null"
android:fontFamily="sans-serif" />
The text and the hint just put the server url in the field but it still has to be manually typed in. I tried to use android:setText="#string/serverurl" but then the apk fails to compile again.
There is no edittext properties like this : android:setText="#string/serverurl"
All you want to do set default string to your editext then in xml just use below property.
android:text="#string/serverurl"
Also remove the hint property you really don't need that because you are seting default text so hint won't be shown.
Also second why to do this in java code like below.
EditText edtServerUrl =findViewById(R.id.et_sever_url);
edtServerUrl.setText(R.string.serverurl)
Put above code inside onCreate() method of activity.
I have a login form in my android application, the problem is when I entered special characters it bypass all my checks. I meant to say its log in thru invalid password and redirect to next activity. I had used android:digits property of EditText. it works fine on simple EditText but not working when I set inputType property.
xml:
<EditText
android:layout_weight="2"
android:id="#+id/edt_signup_password"
android:layout_width="match_parent"
android:inputType="textPassword"
android:digits="0,1,2,3,4,5,6,7,8,9,qwertzuiopasdfghjklyxcvbnm"
android:background="#drawable/edittext"
android:padding="4dp"
android:maxLength="15"
android:layout_height="wrap_content"
android:ems="10" />
how can I restrict the special characters when android:inputType="textPassword" . Thanks in Advance
One way to do this via java would be to use a Regex.
You would define what characters you want to allow like such:
final static String INPUT = "[ a-zA-Z0-9_-]+";
final static Pattern PATTERN = Pattern.compile(INPUT);
This INPUT pattern would allow space, underscore, dash, 0-9 and lower and upper case letters.
Then to check the inputted text against the pattern you would use the following:
Matcher matcher = PATTERN.matcher(textView.getText().toString());
if (matcher.matches()) {
//login here
}
else {
//do something else
}
Is there any way to limit users allowed to type only some selected character set (Eg: A or B or C or D) in Android layout.xml
Use the digits attribute, only the characters passed to digits will be allowed in the EditText.
android:digits="abc123"
And yes, "digits" is a misleading name, it accepts letters and symbols too.
You can use the inputType attribute:
<EditText android:id="#+id/edit_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number" />
Possible Values for android:inputType attribute in edit text are: 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
This android:digit will allow only the digits and alphabets you want to be filled in EditText.
<EditText
android:inputType="text"
android:digits="0123456789*qwertzuiopasdfghjklyxcvbnm,_,-"
android:hint="Only letters, digits, _ and - allowed"
/>
You can set your EditText Field like this...
<EditText
android:inputType="text"
android:digits="THE SELECTED CHARACTERS THAT USERS ARE ALLOWED TO TYPE"
/>
By listing the selected characters (A,B,C,and D) that users are allowed to type in the android:digits, you are limiting the possible characters that users can type. This is the easiest way I've learned through searching.
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);
I have an EditText in which I want only integer values to be inserted. Can somebody tell me which property I have to use?
Add android:inputType="number" as an XML attribute.
For example:
<EditText
android:id="#+id/myNumber"
android:digits="0123456789."
android:inputType="numberDecimal"
/>
In code, you could do
ed_ins.setInputType(InputType.TYPE_CLASS_NUMBER);
For only digits input use android:inputType="numberPassword" along with editText.setTransformationMethod(null); to remove auto-hiding of the number.
OR
android:inputType="phone"
For only digits input, I feel these couple ways are better than android:inputType="number". The limitation of mentioning "number" as inputType is that the keyboard allows to switch over to characters and also lets other special characters be entered. "numberPassword" inputType doesn't have those issues as the keyboard only shows digits. Even "phone" inputType works as the keyboard doesnt allow you to switch over to characters. But you can still enter couple special characters like +, /, N, etc.
android:inputType="numberPassword" with editText.setTransformationMethod(null);
inputType="phone"
inputType="number"
android:inputType="numberDecimal"
<EditText
android:id="#+id/age"
android:numeric="integer"
/>
Try the following code:
Edittext_name.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
It will allow you to enter just numbers. You cannot enter chars.
if you want to enter chars, not numbers, you can edit the values between the quotes inside getInstance.
using the below can solve your problem better;
in xml:
<EditText
android:id="#+id/age"
android:inputType="numberDecimal|numberSigned" />
or //in activity inside etfield.addtextchangelistener
private String blockCharacterSet="+(/)N,*;#";//declare globally
try {
for (int i = 0; i < s.length(); i++) {
if (blockCharacterSet.contains(s.charAt(i) + "")) {
String corrected_settempvalue = arrivalsettemp.substring(0, arrivalsettemp.length() - 1);
et_ArrivalSetTemp.setText(corrected_settempvalue);
if (corrected_settempvalue.length() != 0)
et_ArrivalSetTemp.setSelection(corrected_settempvalue.length());
}
}
} catch (Exception d) {
d.printStackTrace();
}
If anyone want to use only number from 0 to 9 with imeOptions enable then use below line in your EditText
android:inputType="number|none"
This will only allow number and if you click on done/next button of keyboard your focus will move to next field.
You can use it in XML
<EditText
android:id="#+id/myNumber"
android:digits="123"
android:inputType="number"
/>
or,
android:inputType="numberPassword" along with editText.setTransformationMethod(null); to remove auto-hiding of the number.
or,
android:inputType="phone"
Programmatically you can use
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
I need to catch pressing Enter on a keyboard with TextWatcher. But I found out that all numeric keyboards android:inputType="number" or "numberDecimal" or "numberPassword" e.t.c. don't allow me to catch Enter when user press it.
I tried android:digits="0123456789\n" and all numeric keyboards started to work with Enter and TextWatcher.
So my way is:
android:digits="0123456789\n"
android:inputType="numberPassword"
plus editText.setTransformationMethod(null)
Thanks to barmaley and abhiank.
I don't know what the correct answer was in '13, but today it is:
myEditText.setKeyListener(DigitsKeyListener.getInstance(null, false, true)); // positive decimal numbers
You get everything, including the onscreen keyboard is a numeric keypad.
ALMOST everything. Espresso, in its infinite wisdom, lets typeText("...") inexplicably bypass the filter and enter garbage...
the simplest for me
android:numeric="integer"
although this also more customize
android:digits="0123456789"