Is it possible to create an alert in the edittext?
I've seen it in 2 apps with exactly the same style, so I understand it's a default view or property.
YOu have to use https://github.com/vekexasia/android-edittext-validator
There are several values you can set to the test attribute:
regexp: for custom regexp
numeric: for an only numeric field
alpha: for an alpha only field
alphaNumeric:
personName: checks if the entered text is a person first or last name.
personFullName: checks if the entered value is a complete full name.
email: checks that the field is a valid email
creditCard: checks that the field contains a valid credit card using Luhn Algorithm
phone: checks that the field contains a valid phone number
domainName: checks that field contains a valid domain name ( always passes the test in API Level < 8 )
ipAddress: checks that the field contains a valid ip address
webUrl: checks that the field contains a valid url ( always passes the test in API Level < 8 )
date: checks that the field is a valid date/datetime format ( if customFormat is set, checks with customFormat )
nocheck: It does not check anything except the emptyness of the field.
It is possible using setError method. it is available from API Level one only.
editText.setError("Enter yor error message").
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="#string/cont_num"
android:inputType="phone"/>
</android.support.design.widget.TextInputLayout>
From the android design library
Rather than using an EditText widget one solution would be a RelativeLayout with a Auto complete TextView widget and an alert Icon of your choice set rightOf your AC TextView within the RL.
Then add a text change listener to your AC textview which is looking for your specific text input i.e. binary and if a breach is detected display warning and error icon?
Related
I am wondering why Android doesn't support this.
To explain my problem. I am working on an accessibility feature for an Android app.
I have a TextView that displays a number value like 123456.
If I use TalkBack, it will always read just this number value.
What I want it to give this value a context so that TalkBack would read something like: "Number value 123456" I am not able to do that as contentDescription just overrides the text value and hint attribute is read after the value like: "123456 Number value".
Is there an alternative for this? Let me also say that the app is not using a separate TextView which would say "Number value" that TalkBack could read.
The solution I did was to use
var number = 123456
textView.contentDescription = "Number value ${number}"
I don't know if I should parse the content of the EditText field called "startTime" if the textInput attribute is set to "Time."
The input keyboard offers the "Phone Number" set of keys which doesn't quite work for entering time (i.e. I wasn't able to add a period [is present in the keyboard] or a semicolon [is missing] as in 10.55 or 10:55)
Plus, I want to know what format (12-hour vs. 24-hour) is used by default.
It's important because then the question of handeling am/pm distinction comes into the picture.
Please include how to "set/fix it" snippets of code with your answer.
I am doing some math on the values of two EditText fields, I want to have the following validations on them:
They are not empty.
They are valid to each other(if the first field was integer the second should be the same, if the first was decimal the second should be the same).
I cannot figure out how to validate the decimal values or specifically the decimal point.
I have tried this out, but it didn't work. My app just crashes.
if (editText1.getText().toString().equals(".") || editText2.getText().toString().equals("."))
return;
For decimal value you can use this:
EditText et = (EditText) findViewById(R.id.myTextView) ;
et.setInputType(0x00002002);
It will accept only decimal values.
Another solution is here.
You can use contains() method to check if your string contains the argument you provided
if (textView1.getText().toString().contains(".") || textView2.getText().toString().contains(".")) {
Toast.makeText(getApplicationContext(), "Wrong Values", Toast.LENGTH_LONG).show();
First of all, you want to set your input type to accept decimals and/or numbers.
You can set it up in the xml layout:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal|number"
/>
Then you can just convert your EditText text to double:
double result = new Double(editText1.getText().toString());
or
double result = Double.valueOf(editText1.getText().toString());
This rocket science that you wroted is not needed anymore after that.
You can test whether something is a valid value or not by trying to parse it with Float.parseFloat and seeing if it throws a NumberFormatException (via try/catch). If it does, then you know the input is invalid.
Btw, it's nice if you provide a visual cue to the user to inform them that the input is invalid. As opposed to waiting until the user presses the "calculate" button and only tell them then.
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);