I set InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_CLASS_NUMBER to my EditText and i want to use comma for decimal separator. So i set digits "0123456789.," to EditText.
editText.keyListener = DigitsKeyListener.getInstance("0123456789.,")
I set a TextWatcher on EditText to handle user input. When i clicked comma(",") on Android emulator keyboard, it is working as expected but if i get build on my phone, which has Samsung Keyboard, comma key is disabled and doesnt work. I searched so much but i couldn't find a way.
Any idea with this problem?
It's the year 2022, and still this problem exists (at least on Samsung devices):)
So I solved this by adding a TextChangedListener to the EditText and check, whether the last entered character is equal to a (country specific) thousands separator. If so, I replace it by a country specific decimal separator:
public class EditTextTausenderErsetzer implements TextWatcher{
...
#Override
public void afterTextChanged(Editable editable)
{
// determine country specific separators
// char thousandsSep = .., char commaSep =..
int comma_index = editable.toString().indexOf(commaSep);
if(comma_index == -1) { // no comma so far, thus there might be a "." which shall be a comma separator
if (editable.toString().indexOf(thousandsSep) != -1) {
if (editable.toString().indexOf(thousandsSep, (editable.length()-1)) == (editable.length() - 1)) {
editable.delete(editable.length() - 1, editable.length() - 1);
editable.insert(editable.length(), commaSep + "");
}
}
}
// ...
// do other stuff like setting automatically thousands separators
This solution works, if your app manages the thousands separators, and you do not allow the user to set these.
I solved in this way
override fun afterTextChanged(editable: Editable) {
// If the user input from keyboard some special characters like dot, we replace it programmatically with comma
if (editable.toString().contains(".")) {
editable.delete(editable.toString().length - 1, editable.toString().length)
editable.insert(editable.toString().length, ",")
return
} }
Related
First I have to say I have read similar questions and answers here on SO and this question is basically a duplicate of this question and many others but the answers given to those questions doesn't work like the way i want it.
The problem:
Setting length filter on my EditText programmatically like this:
editText.setFilters(new InputFilter[]{new LengthFilter(10)} );
The only thing it does is hide the text that go over the limit in the EditText. It still shows the long (unlimited) text in suggestion box and i have to delete (backspace) for each letter that go over before being able to delete what is shown in the EditText.
Suggested Solutions:
Setting InputType to textFilter.
Programmatically I did this:
editText.setInputType( InputType.TYPE_TEXT_VARIATION_FILTER );
It hides suggestions but the unlimited text is still present and i still have to use backspace to delete letters that shouldn't be present.
Setting InputType to textNoSuggestions|textVisiblePassword.
Programmatically I did this (had to add TYPE_CLASS_TEXT too otherwise it wouldn't work):
editText.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
This one does work but the problem is it stops "gesture typing" and it changes the font to monospace.
Better Solutions?
As you can see these two methods don't actually work without additional problems. Is there any other way of doing this that I missed. Should I just use a TextWatcher if I want to keep gesture typing and suggestions?
I ended up using a TextWatcher instead. I'm not sure if it is the best way to do this but it does work with suggestions and it doesn't turn off gesture typing or change the font style. Here's how I did it (I'm quite new to android so if this needs improvement feel free to let me know).
I added an example in the comments to clarify what is going on.
Make these global variables:
private boolean mWatcherIsBlocked = false;
private String mBeforeChange;
private String mFilteredString;
private int mCursorPosition = 0;
Then create the TextWatcher and add it to your EditText
final int maxLength = 10; // desired length limit
/**
* lets say our EditText is showing "abcdefgh". We select "cdef" from it and
* paste a new text "ijklmnop" in the middle. What we should get according to
* our maxLength is this:
* (1) "ab" (0th up to the letter from before_change_text we were selecting) +
* (2) "ijklmn" (part of the text we pasted minus the number of letters the whole
* after_change_text goes over the 10 letter limit) +
* (3) "gh" (last part of before_change_text that wasn't selected)
*
* so the new text has to be "abijkmngh"
*/
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// get before_change_text if textWatcher isn't blocked
if (!mWatcherIsBlocked) mBeforeChange = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!mWatcherIsBlocked){
// get after_change_text if textWatcher isn't blocked
String after = s.toString();
// if after_change_text's length is bigger than the limit
if (after.length() > maxLength) {
// see how much it goes over the limit
int over = after.length() - maxLength;
// add parts (1) and (2) like our example above
String st = mBeforeChange.substring(0, start) + // (1)
after.substring(start, start + count - over); // (2)
// get where the cursor position should be after pasting (
// = after the last letter we could paste = length of (1) + (2) )
mCursorPosition = st.length();
// now add part (3) of our text to the first two
st += mBeforeChange.substring(
mBeforeChange.length() - (maxLength - st.length()),
mBeforeChange.length());
// now assign this new text to a global variable
mFilteredString = st;
} else {
// if after_change_text hasn't gone over the limit assign it
// directly to our global variable
mFilteredString = s.toString();
}
}
}
#Override
public void afterTextChanged(Editable s) {
// if filtered text is not the same as unfiltered text
// or textWatcher is not blocked
if (!mFilteredString.equals(s.toString()) && !mWatcherIsBlocked) {
// block textWatcher to avoid infinite loops created by setText
// (this might not work as I well as I think!)
mWatcherIsBlocked = true;
// set new text to our EditText
editText.setText(mFilteredString);
// set its cursor position
editText.setSelection(mCursorPosition);
// unblock the textWatcher
mWatcherIsBlocked = false;
}
}
};
// add the TextWatcher to our EditText
editText.addTextChangedListener(textWatcher);
I have an editText for searching phone number. It automatically switches between two modes:
entering phone number directly +7(900)000-00-00
searching contact book
So, to do this a have to change editText input type dynamically, when user inputs text, following these rules:
When edit is empty: INPUT_NO_SUGGEST
When user input phone number: INPUT_NO_SUGGEST
When user input something else: INPUT_TEXT
The problem appears when entering text with suggestions:
when I enter Can -> then click on suggest Cam it clears (left gif)
when I enter Can -> then click on suggest Can't it works as expected (right gif)
In the 1st case we change word 'Can'->'Cam' and it works wrong
In the 2nd case we supplement word 'Can'->'Can't' and it works ok
Code inside textWatcher:
private static final int INPUT_TEXT = InputType.TYPE_CLASS_TEXT;
private static final int INPUT_NO_SUGGEST = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
...
public void afterTextChanged(Editable s) {
boolean isPhone = isPhoneNumber(s);
int inputType;
if (s.length() == 0) {
inputType = INPUT_NO_SUGGEST;
} else {
if (isPhone) {
inputType = INPUT_NO_SUGGEST;
} else {
inputType = INPUT_TEXT;
}
}
editText.setInputType(inputType);
}
Have any ideas how it can be fixed?
In my app I have to format the input of two EditTexts like:
1234 4567 67: Ten digits that grouped by four. (The space is automatically, not inserted by user)
11/14: Four digits that separated by '/'. (The '/' is inserted automatically)
I don't know how to do it. Please help:
Put a listener on the edit text as afterTextChanged.
Get the number of digits by using the length() function.
Once you get the number of digits, you can extract each digit and then insert space of '/' at the appropriate place.
len=editText.getText.toString().length();
then you can do the appropriate change by checking the length.
num=Integer.parseInt(editText.getText.toString());
temp=num;
if(len>=10)
{
A:
if((len-4)>0)
{
for(i=0;i<(len-4);i++)
{
temp=temp/10; //we get the first 4 digits
}
editText.setText(temp+" "); //place letters and add space
temp=num%(10^(len-4)); //get the num without the first-4 letters
len=len-4; //modify length
goto A; //repeat again
}
editText.setText(temp); //add the last remaining letters
}
else if(len==4)
{
temp=num;
temp=temp%100; //store the last 2 digits
num=num/10; //get the first 2 digits
editText.setText(num+"/"+temp);
}
i havnt tried this but i think this will work.
Hope this will help. :)
I can think of two ways of achieving it:
Use addTextChangedListener:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Do your tricks here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Create custom Edittexts
This link wont do what you are looking for, but will give you an idea how to create custom EditText.
Use "onKeyListener" to get the input event.
EditText OnKeyDown
Then check for correct input and count the digits. Add the whitespace/slash in your code.
Sample code:
if (editText.getText.toString().length() % 4 == 0) editText.setText(editText.getText.toString() + " ");
Didn't try it by myself, but this would be the way i would try. In addition i would check for numeric input too.
I would need a way to detect if the EditText has been changed by the user typing something or by the app changing the text programmatically. Any standard way of doing this? I guess I could always do something hackish like unsetting the TextWatcher before setText() and setting it back again afterwards, but there's got to be a better way of doing this... right?
I tried checking if the EditText is focused in the TextWatcher, but that was of little help since the EditTexts gets focused "semi-randomly" anyway when scrolling...
Background
I have a ListView with EditTexts in every listitem. I've sorted out the basic problem of storing the values for the EditTexts for reuse when the user scrolls.
I also have a TextWatcher that sums up the values in all EditTexts and displays the sum when the user edits the content of any of the EditTexts.
The problem is that when I'm scrolling the list and my custom adapter is reentering the stored values in the EditTexts on bindView(), that also triggers the TextWatchers afterTextChanged() method, causing the scrolling to lag because the summing-up-function is triggered.
This sorted itself out a long time ago, but for anyone who finds their way here looking for an answer, here's what I did:
I ended up setting the Tag of the EditText to some arbitrary value right before I'm about to change it programmatically, and changing the value, and then resetting the Tag to null. Then in my TextWatcher.afterTextChanged() method I check if the Tag is null or not to determine if it was the user or the program that changed the value. Works like a charm!
Something like this:
edit.setTag( "arbitrary value" );
edit.setText( "My Text Value" );
edit.setTag(null);
and then
public void afterTextChanged(Editable s) {
if( view.getTag() == null )
// Value changed by user
else
// Value changed by program
}
The accepted answer is perfectly valid, but I have another approach;
#Override
public void onTextChanged(CharSequence charSequence,
int start, int before, int count) {
boolean userChange = Math.abs(count - before) == 1;
if (userChange) {
}
}
It works by checking if the change was a single character.
This is not a fool-proof solution as copy-paste operations might be missed, and non-user changes of a single character will also be missed.
Depending on your use case, this might be a viable solution.
One thing that helped to me is having boolean canListenInput field. Use it inside of watcher.
email.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (canListenInput) {
emailChanged = true;
}
}
});
Clear it before changing text programmatically. Set it inside of onAttachedToWindow, (after state) restoration:
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
canListenInput = true;
}
Depending on your use case (e.g. you are auto-populating this field when the user types into another field), you can also check if the view has focus, e.g.:
textView.doAfterTextChanged {
val isValueChangedByUser = textView.hasFocus()
// ...
}
I have created some extension methods to tackle this scenario
inline fun TextView.runTaggingCode(block: () -> Unit) {
this.setTag(R.string.tag_text_id, "set_from_code")
block()
this.setTag(R.string.tag_text_id, null)
}
fun TextView.isTaggedForCode() = this.getTag(R.string.tag_text_id) != null
where I have defined the R.string.tag_text_id as below
<string name="tag_text_id" translatable="false">dummy</string>
Now where I to use these methods, I will simply change my code as below,
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int,
) {
if (textView.isTaggedForCode()) {
return
}
textView.runTaggingCode {
// your logic here
}
}
But in case you don't want to change the same text view text, in it own TextWatcher you can also see the answer
You can do this by adding:
private String current = "";
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
[your_edittext].removeTextChangedListener(this);
//Format your string here...
current = formatted;
[your_edittext].setText(formatted);
[your_edittext].setSelection(formatted.length());
[your_edittext].addTextChangedListener(this);
}
On the soft keyboard in Android you can set the soft keyboard to show the numbers instead of a-z keyboard using android:inputType="numberDecimal". However, what do I do if I only want to show the top number row 1 2 3 4 5 6 7 8 9 0 and not the following rows starting with # # $ % ...?
Thanx for listening!
android:inputType="phone"
android:digits="1234567890"
is an option
You must only add this line on your code:
input.setRawInputType(Configuration.KEYBOARD_12KEY);
this will show only the numeric keyboard.
The phone number pad is the closest thing I've found (set inputType="phone" on your EditText).
The accepted answer did not work for me (tested on OnePlus 3t and Samsung Galaxy S6/S7 phones).
This solution uses numberPassword but overrides the default transformation method for the EditText to show characters instead of showing dots.
<EditText
android:id="#+id/userid"
android:inputType="numberPassword"
android:maxLength="6"
/>
// Numeric 6 character user id
EditText input = findViewById(R.id.userid);
// Process input and show characters instead of dots
input.setTransformationMethod(SingleLineTransformationMethod.getInstance());
Just posted an answer here but re-posting for simplicity:
Seems Android has added the functionality we were seeking. This is the xml I use for simple EditText numeric entry:
android:inputType="numberPassword"
android:digits="0123456789"
android:singleLine="true"
android:ems="4"
android:textColor="#android:color/black"
android:gravity="center"
The accepted answer didn't work for me. It kept showing other unwanted characters
I found a way to accomplish the behavior I wanted by changing the inputType to numberPassword and then disabling the password dots
textInput.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD
textInput.transformationMethod = SingleLineTransformationMethod()
Last I looked into it, Android did not have any good options for that. I ended up having to write my own version of a soft keyboard-like user interface.
I had implemented this for android in Xamarin. So, my code is in C#. But the principal stays the same. You can set attribute of edittext to android:inputType="numberPassword".
Then within your code you attach custom transformation method to your edittext view.
holder.edtxtQty.TransformationMethod = new HiddenPasswordTransformationMethod();
private class HiddenPasswordTransformationMethod : global::Android.Text.Method.PasswordTransformationMethod
{
public override Java.Lang.ICharSequence GetTransformationFormatted(Java.Lang.ICharSequence source, View view)
{
return new PasswordCharSequence(source);
}
}
private class PasswordCharSequence : Java.Lang.Object, Java.Lang.ICharSequence
{
private char DOT = '\u2022';
private Java.Lang.ICharSequence _source;
public PasswordCharSequence(Java.Lang.ICharSequence source)
{
_source = source;
}
public char CharAt(int index)
{
return _source.CharAt(index);
}
public int Length()
{
return _source.Length();
}
public Java.Lang.ICharSequence SubSequenceFormatted(int start, int end)
{
return _source.SubSequenceFormatted(start, end); // Return default
}
public IEnumerator<char> GetEnumerator()
{
return _source.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _source.GetEnumerator();
}
}
From code:
et.setInputType(InputType.TYPE_CLASS_NUMBER)