The following code produces an EditText (target version 23). I've been working on this for about 8 hours, and have received some suggestions, but I don't think anyone has ever seen this before, so I remain stuck.
Click on the field.
The A/N soft keyboard opens up.
Click the 123? button at bottom left. The numeric soft keyboard opens up.
Press any digit. Nothing happens.
Long press 5, "5/8" gets added into the text field.
Press any special character, such as #. It might add to the field.
Clear the field. Type "for", press 123?, now it will take digits.
Clear the field. Type "for?", press 123?, it will not take digits.
I added a TextWatcher. If the digits didn't post, the TextWatcher didn't see them either.
EditText bottomT = new EditText(model);
bottomT.setTextSize(14);
bottomT.setHint("ZIP");
bottomT.setHintTextColor(Color.BLACK);
bottomT.setBackgroundColor(Color.WHITE);
bottomT.setTextColor(Color.BLACK);
// bottomT.setInputType(InputType.TYPE_CLASS_NUMBER) Didn't make any difference.
// bottomT.setRawInputType(InputType.TYPE_CLASS_NUMBER) Didn't make any difference.
// bottomT.setText("", TextView.BufferType.EDITABLE); DIdn't make a difference
bottomT.setText("");
EditText is misbehaving because in my custom ViewGroup I had
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
....
child.layout(child.getLeft(), child.getTop(),
child.getLeft() + child.getMeasuredWidth(),
child.getTop() + child.getMeasuredHeight());
child.setRight(somevalue); // CAUSES EDITTEXT PROBLEMS
child.setBottom(somevalue); // CAUSES EDITTEXT PROBLEMS
It’s clear now that I can't setRight() and setBottom(), but it’s also clear that EditText should not get weird.
Ignore the backspace key.
Randomly ignore numeric keys, but accept the decimal point.
Ignore the newLine(Enter) key
Which keys are ignored, or not, depends on the device. Samsung Tab 4 or the Nexus 5 API 23 X86 emulator are good places to see this.
You have to add this line in your java code.
bottomT.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
Try this line of code .
bottomT.setInputType(InputType.TYPE_CLASS_NUMBER |InputType.TYPE_NUMBER_FLAG_DECIMAL);
Related
I had 6 EditText in a row to enter mac address, so after validating the user input in the macAddress EditText, if the no. of entries in one EditText becomes 2, i would call the focus on next edit box, as shown below.
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//myMacArray is an array for storing macaddress editTexts ID
//mIndex represents the the index for for each EditText
if((myMacArray [mIndex]).getText().length() == 2)
if(mIndex < 5)
myMacArray [mIndex+1].requestFocus();//requesting focus to
//next editText
}
Now, if i the user is using the soft Qwerty keypad then its working fine....but in case of 3X4 keypad if the user wants to enter "1b"(inputing "b" in 3x4 keypad will require to press the 2nd button twice) in any macAddress' editText , it wont work as the focus is now shifted to next editBox just after the first user click.
Please let me know if there is a way to detect the type of keypad opened by user or some other way to deal with this
You can "force" the type of keyboard you want to come up by specifying the
android:inputType="<...>"
attribute in your edittext. If your users are going to be typing letters and numbers there is no reason to let them use a phone pad.
https://developer.android.com/training/keyboard-input/style.html
https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
So I'm currently trying to achieve the following: I have one big piece of text not stored in my app yet. I will have one big EditText and what I want to do is when a user touches any key on the keyboard, a part of my text (like 2 or 3 characters at the time) should appear in the EditText. Then when a user touches a key again, another 2 or 3 characters of the text should appear inside the EditText. I've thought a lot about it but I can't seem to find the right approach to do it. Where to store my Text and how to code so that the app overrides whatever the user inputs and adds 2 or 3 characters in the EditText according to my text that I need to store somewhere.
Any ideas?
Take a look at
http://developer.android.com/reference/android/text/TextWatcher.html
Implement a TextWatcher and check the user input in onTextChanged() to determine the user input. Mark the input the user has made with a Span (which can be any custom object, it is just used to tag the text area), so you can later look it up with Editable#getSpans() in afterTextChanged(), where you can replace the whole span with your override text.
Code idea (untested):
onTextChanged(CharSequence s, int start, int before, int count){
((Spannable)s).setSpan(new MyMarkObject(), start, start+count, Spannable.SPAN_MARK_MARK)
}
afterTextChanged(Editable s)
{
MyMarkObject[] markers = s.getSpans(0, s.length(), MyMarkObject.class);
for (MyMarkObject marker: markers){
int start = s.getSpanStart(marker);
int end = s.getSpanEnd(marker);
s.replace(start, end, getYourDesiredReplacementTextFor(s.subSequence(start,end));
}
}
I'm implementing a form where the user is asked for a measurement. Depending on the locale, the user will be entering either meters or feet/inches.
Naively, my current implementation asks for a decimal point to be used with only feet. e.g., 2.5 feet would equal 2′ 6″ which is fairly misleading from my understanding. I'm not in a country that hasn't adopted to the metric system, so it wasn't obvious to me at the time.
There's a few possibilities and problems I see:
Show two EditText fields, one for feet one for inches. This makes layout design difficult since I'm also allowing the user to enter meters, which will only need one EditText. A possibility is to make one hidden at run time.
Using one EditText, I can't seem to find an android:inputType that allows for number input plus double primes and primes, or double quotes and apostrophes. e.g. 3′ 5″ or 3' 5". It means the EditText would have to use the standard alphabet keyboard which makes it slightly hindering to the user.
Also, with one EditText, I am happy to (and already doing so for just a single entry with meters and feet without inches), to suffix the EditText after the user loses focus, and remove the suffix when being focused again. This does make the code more complex for a simple input field.
Are there any other options for this?
I just realised soon after posting:
Why not create a NumberPicker popup or Spinner when the user focuses on the EditText. This would only show up for imperial measurements.
I'm thinking off the top of my head you can use a TextWatcher to check for when the user types a space. When that occurs, suffix in the ' at the end and let the user type the inches.
Or, always append the suffix to the end and push it down beyond the cursor every time the user types. When the user presses space then jump the cursor over and put " beyond it.
It may or may not be messy from a user flow perspective (I haven't tried or seen it), but it could solve some problems. You can continue to force the user to type only numerical values which keeps the number keyboard up instead of letters. They don't have to swap keyboards or do long-key presses to type a single character. It also shows the intentions to the user as he/she types.
edittext.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() != KeyEvent.KEYCODE_DEL) {
String input = edittext.getText().toString();
if (input.length() == 1) {
input = input + "'";
edittext.setText(input);
int pos = edittext.getText().length();
edittext.setSelection(pos);
}
}
return false;
}
});
I've added a TextWatcher to an EditText and am listening for changes in the text via the onTextChanged(CharSequence s, int start, int before, int count) method. When I paste text that has say 10 characters, into this EditText, onTextChanged() gets called 10 times, once for each character in the text I pasted, from left to right. I want onTextChanged() to be called only once after all 10 characters have been pasted into the EditText. I'm sure this should be possible, because otherwise what's the point in having the "count" param if it's always going to be 1?
count won't always be 1: for instance, if you select and delete a block of text or if you choose an autocomplete option.
In any case, the details of whether pasting happens in one chunk or one character at a time is an implementation detail, and if you rely on either behavior it's likely your app will break in the future.
Try using afterTextChanged it will only get one call
I have an editText that displays $0.00. When a user clicks on that, I want a numpad to come up. If they press 5, it should display $0.05, they press 3, it goes $0.53, they press 7 it goes $5.37, etc.
So far I have the editText displaying $0.00 and it brings up a numpad but you need to delete the numbers up to the $ sign and input the decimal yourself. I've had a few really complex ideas but I'm not sure I'm going about it the right way. Any suggestions?
You could use a variable protected int curValue = 0;
And set the editText each time a new number is pressed,
curValue *= 10; curValue += pressedNumber; editText.setText("$"+curValue/100.0f);
(as long as you don't need real big numbers)
Have you by chance taken a look at the android ref for currency?
http://developer.android.com/reference/java/util/Currency.html