Difference between NumberFlagDecimal and NumberFlagSigned - android

I just want a numeric keypad in my android Xamarin application.
so which is better input type and why ?

from the docs
public static final int TYPE_NUMBER_FLAG_DECIMAL
Flag of TYPE_CLASS_NUMBER: the number is decimal, allowing a decimal
point to provide fractional values.
public static final int TYPE_NUMBER_FLAG_SIGNED
Flag of TYPE_CLASS_NUMBER: the number is signed, allowing a positive
or negative sign at the start.

If you want nothing but numbers allowed to be entered in your EditText` just set the digits property:
<EditText
.
.
.
android:inputType="numberDecimal|number"
android:digits="1234567890"
./>
You can also use the set the InputType as TYPE_CLASS_NUMBER
This will disable everything else but numbers good luck.
Revert in case of queries.

Solution:
If you want a keyboard only with number and without any other characters, you can do following steps:
Set your keyboard's inputType: numberPassword
<EditText
android:id="#+id/edit"
android:text=""
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:inputType="numberPassword"
/>
Then you will get a keyboard with only with numbers, however you will find numbers in EditText is invisible as security enter.
If you want to make the numbers visible in the EditText, you need to customize PasswordTransformationMethod:
private class NumericKeyBoardTransformationMethod : PasswordTransformationMethod
{
public override ICharSequence GetTransformationFormatted(ICharSequence source, View view)
{
return source;
}
}
And finally we need implementing it to the EditText in order to display the characters typed.
EditText myedit = FindViewById<EditText>(Resource.Id.edit);
myedit.TransformationMethod = new NumericKeyBoardTransformationMethod();
I find solution here and I translate it into C# code. Hope it will help you.

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 to avoid showing characters like Ä in android keyboard

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.

In Android EditText, how to force writing uppercase?

In my Android application I have different EditText where the user can enter information. But I need to force user to write in uppercase letters.
Do you know a function to do that?
Android actually has a built-in InputFilter just for this!
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
Be careful, setFilters will reset all other attributes which were set via XML (i.e. maxLines, inputType,imeOptinos...). To prevent this, add you Filter(s) to the already existing ones.
InputFilter[] editFilters = <EditText>.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = <YOUR_FILTER>;
<EditText>.setFilters(newFilters);
If you want to force user to write in uppercase letters by default in your EditText, you just need to add android:inputType="textCapCharacters". (User can still manually change to lowercase.)
It is not possible to force a capslock only via the XML. Also 3rd party libraries do not help. You could do a toUpper() on the text on the receiving side, but there's no way to prevent it on the keyboard side
You can use XML to set the keyboard to caps lock.
Java
You can set the input_type to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS. The keyboard should honor that.
Kotlin
android:inputType="textCapCharacters"
You can used two way.
First Way:
Set android:inputType="textCapSentences" on your EditText.
Second Way:
When user enter the number you have to used text watcher and change small to capital letter.
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable et) {
String s=et.toString();
if(!s.equals(s.toUpperCase()))
{
s=s.toUpperCase();
edittext.setText(s);
edittext.setSelection(edittext.length()); //fix reverse texting
}
}
});
Use input filter
editText = (EditText) findViewById(R.id.enteredText);
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
Even better... one liner in Kotlin...
// gets your previous attributes in XML, plus adds AllCaps filter
<your_edit_text>.setFilters(<your_edit_text>.getFilters() + InputFilter.AllCaps())
Done!
You can add the android:textAllCaps="true" property to your xml file in the EditText. This will enforce the softinput keyboard to appear in all caps mode. The value you enter will appear in Uppercase. However, this won't ensure that the user can only enter in UpperCase letters. If they want, they can still fall back to the lower case letters. If you want to ensure that the output of the Edittext is in All caps, then you have to manually convert the input String using toUpperCase() method of String class.
You should put android:inputType="textCapCharacters" with Edittext in xml file.
Rather than worry about dealing with the keyboard, why not just accept any input, lowercase or uppercase and convert the string to uppercase?
The following code should help:
EditText edit = (EditText)findViewById(R.id.myEditText);
String input;
....
input = edit.getText();
input = input.toUpperCase(); //converts the string to uppercase
This is user-friendly since it is unnecessary for the user to know that you need the string in uppercase.
Hope this helps.
For me it worked by adding android:textAllCaps="true" and android:inputType="textCapCharacters"
<android.support.design.widget.TextInputEditText
android:layout_width="fill_parent"
android:layout_height="#dimen/edit_text_height"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
In kotlin, in .kt file make changes:
edit_text.filters = edit_text.filters + InputFilter.AllCaps()
Use synthetic property for direct access of widget with id.
And in XML, for your edit text add a couple of more flag as:
<EditText
android:id="#+id/edit_text_qr_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...other attributes...
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
This will update the keyboard as upper case enabled.
Try using any one of the below code may solve your issue.
programatically:
editText.filters = editText.filters + InputFilter.AllCaps()
XML :
android:inputType="textCapCharacters" with Edittext
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable et) {
String s=et.toString();
if(!s.equals(s.toUpperCase()))
{
s=s.toUpperCase();
edittext.setText(s);
}
editText.setSelection(editText.getText().length());
}
});
Just do this:
// ****** Every first letter capital in word *********
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
/>
//***** if all letters are capital ************
android:inputType="textCapCharacters"
try this code it will make your input into upper case
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
Simple kotlin realization
fun EditText.onlyUppercase() {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
filters = arrayOf(InputFilter.AllCaps())
}
PS it seems that filters is always empty initially
Based on the accepted answer, this answer does the same, but in Kotlin. Just to ease copypasting :·)
private fun EditText.autocapitalize() {
val allCapsFilter = InputFilter.AllCaps()
setFilters(getFilters() + allCapsFilter)
}
To get all capital, use the following in your XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
To get capitalized keyboard when click edittext use this code in your xml,
<EditText
android:id="#+id/et"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Input your country"
android:padding="10dp"
android:inputType="textCapCharacters"
/>
I'm using Visual Studio 2015/Xamarin to build my app for both Android 5.1 and Android 6.0 (same apk installed on both).
When I specified android:inputType="textCapCharacters" in my axml, the AllCaps keyboard appeared as expected on Android 6.0, but not Android 5.1. I added android:textAllCaps="true" to my axml and still no AllCaps keyboard on Android 5.1. I set a filter using EditText.SetFilters(new IInputFilter[] { new InputFilterAllCaps() }); and while the soft keyboard shows lower case characters on Android 5.1, the input field is now AllCaps.
EDIT: The behavioral differences that I observed and assumed to be OS-related were actually because I had different versions of Google Keyboard on the test devices. Once I updated the devices to the latest Google Keyboard (released July 2016 as of this writing), the 'All Caps' behavior was consistent across OSes. Now, all devices show lower-case characters on the keyboard, but the input is All Caps because of SetFilters(new IInputFilter[] { new InputFilterAllCaps() });
Xamarin equivalent of ErlVolton's answer:
editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());
A Java 1-liner of the proposed solution could be:
editText.setFilters(Lists.asList(new InputFilter.AllCaps(), editText.getFilters())
.toArray(new InputFilter[editText.getFilters().length + 1]));
Note it needs com.google.common.collect.Lists.
2021: Answer
This is the only latest answer if you want to get the EditText from EditTextPreference.
In order to change the EditText value or attributes, you need to set setOnBindEditTextListener callback as per the new AndroidX Kotlin.
findPreference<EditTextPreference>("key")?.setOnBindEditTextListener {
it.filters = arrayOf<InputFilter>(InputFilter.AllCaps())
}
You can use android:inputType="textCapCharacters|textAutoComplete" in XML file
On your XML side where you use an editText, this code below will force the input-user-text to make them all capital without Java code but XML:
android:inputType="textAutoCorrect|none|numberSigned|textMultiLine|textNoSuggestions|number|datetime|textWebEmailAddress|textPersonName|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|numberDecimal"
These can make the text input area all in capital by force with no doubt.
If you want to make not AllCaps, to turn off, don't write "numberSigned" in your XML code above.
Simply, Add below code to your EditText of your xml file.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
And if you want to allow both uppercase text and digits then use below code.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

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