How to avoid showing characters like Ä in android keyboard - android

I am stuck in an issue where one of my user entering characters like Ä in an edittext and I am preparing an XML from that data. Due to encoding issue I can't prepare a valid XML from these kind of characters. Is there anyway I can stop user to enter such kind of characters or can I hide these characters from android default keyboard itself ?
Regards

you can restrict the user to enter only specific characters in the edit text like below,
<EditText
android:id="#+id/YourEdittextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:privateImeOptions="nm"
android:digits="0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ" />
Above code restrict user to enter only English digits, letters and space

Hide suggestion string in your edittext.
android:inputType="textFilter"

Few days before I was facing same issue I solved this by creating below method.
public static String stripSpecialUTFChars(String string) {
return string.replaceAll("\u0080", "");
}
Please pass your String to above method and use this like below
textView.setText(Html.fromHtml(stripSpecialUTFChars(yourString)));
actually this issue is due to UTF characters.

Related

Use InputType Number but format edittext live text to phone number format

So the easiest way for the edittext to format your input is to use the phone inputType. The problem is that this gives you a few other symbols other then numbers. And I don't want the user to be able to type in those symbols.
I tried to only listen to digits by doing android:digits by this causes the input to no longer be formatted.
Is there a trick that i could do to get this to work? Other then writing a custom textWatcher that formats the text.
If you want to show only numbers in your keyboard just like this screenshot
You have to use numberPassword input type instead of phone input type
android:inputType="numberPassword"
But when you apply this your entries in editText appear as bullets because inputType is numberPassword, So to solve this problem make a new java class for example:
public class NumberKeyPadTransformationMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return source;
}}
And in your main activity where you fetch the ids of edittext or where you use that edittext you have to simply add one line of code
editText.setTransformationMethod(new NumberKeyPadTransformationMethod());
Your whole problem will solve.
Hope this will help you.
You should use as Athira pointed:
android:inputType="number"
and if you want decimal numbers then:
android:inputType="numberDecimal"

How do I restrict the Android keyboard presented to a user to Latin?

I have an application that requires that the user enter an ASCII password between dec 32 and 126. This means that only a latin character and number keyboard is valid for editing. However, when I launch my edittext, a cyrillic keyboard is presented to the user.
My question is, how do I ensure that only a latin keyboard is presented to the user?
Try to add this to your xml:
<EditText
android:inputType="text"
android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm" />
There is no way to force IME to a certain language/character set. However, if you set your EditText to be type password, most software keyboards will limit itself to ASCII + number. That's the closest you will get without writing your own input method.
<EditText
android:password="true"
...
/>

Android xml phone number in keyboard restriction

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

Set EditText Digits Programmatically

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);

How do you set EditText to only accept numeric values in Android?

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"

Categories

Resources