I have an edit text which accepts both text and numeric values.
I have to format it with appending space after every 4 characters.
here are the steps to replicate the issue
1. tap on the edit text
2. select number mode on the keypad and keep entering numbers
3. the moment space is appended, the keyboard switches to text mode. how do I prevent it. Here is the code I tried
editText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
String text = editText.getText().toString();
textlength = editText.getText().length();
if(text.endsWith(" "))
return;
if(textlength == 5 || textlength == 10 || textlength == 20)
{
editText.setText(new StringBuilder(text).insert(text.length()-1, " ").toString());
editText.setSelection(editText.getText().length());
}
}});
}
What you can try to do is in your program
a) Check if space character is received. If yes
b) Switch to numeric keypad using the function setInputType(InputType.TYPE_CLASS_NUMBER)
Hopefully this will solve the issue. But you will need to be monitoring each character input by user.
Related
The code below produces an EditText, but after the numeric only soft keyboard comes up, typing is not recognized. If I change the setInputType to TYPE_CLASS_TEXT, it works just right. I've read and reread all the existing posts about this, but I don't see any where it refuses to accept the typing.
// And a zip code
zip = Util.buildOneLine("ZIP");
zip.setInputType(InputType.TYPE_CLASS_NUMBER);
zip.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
Log.w(TAG, "jkljl");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
Log.w(TAG, "jkljl");
}
#Override
public void afterTextChanged(Editable s)
{
Log.w(TAG, "jkljl");
}
});
public static EditText buildOneLine(String heading)
{
EditText bottomT = new EditText(this);
bottomT.setTextSize(TypedValue.COMPLEX_UNIT_PX, adjustedFont(14));
bottomT.setHint(heading);
temp.setSingleLine(true);
bottomT.setGravity(Gravity.CENTER_VERTICAL);
setLayout(bottomT);
return temp;
}
Well try this :
zip.setRawInputType(InputType.TYPE_CLASS_NUMBER |InputType.TYPE_NUMBER_FLAG_DECIMAL);
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.
I have to watch EditText in my App in such a way that if in EditText data is in 2 lines
then again I want to write in 3rd line then previous 2 line's data should get clear.
For this I am using following way to do
mTextWatcher = new TextWatcher() {
private int lines;
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Log.v("", "inside ontextchnaged");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
lines = getTotalLines(txtDataSource);
if (lines == 2) {
txtDataSource.removeTextChangedListener(mTextWatcher);
Log.v("", txtDataSource.getText().toString());
txtDataSource.setText("");
txtDataSource.addTextChangedListener(mTextWatcher);
}
}
#Override
public void afterTextChanged(Editable s) {
Log.v("", "inside aftertextchanged");
}
};
txtDataSource.addTextChangedListener(mTextWatcher);
and I am getting no of lines enter by the below code
private int getTotalLines(EditText editText) {
int lineNumber = 0;
String text = editText.getText().toString()
.substring(0, editText.getSelectionStart());
for (int i = 0; i < text.length(); i++) {
if (String.valueOf(text.charAt(i)).equalsIgnoreCase("\n")) {
lineNumber++;
}
}
return lineNumber;
}
So when i m getting 2 lines data in EditText then I am removing watcher from EditText
then clearing data of EditText and again I added watcher on EditText .
Every thing working fine but problem is after 2 lines when I start to wrote then it is not writing first character which I enter from Keypad it starts writing from second character input.
So after 2 lines when enter 2 characters then it didnt write first char which i enter it starts writing from second character.
Please help me to resolve this.
Thanks ....
Finally I solved my issue.
It may not be standard way to do, but I didn't find any other way to do this inside
beforeTextChanged()
I solved using the below code inside onTextChanged()
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
lines = getTotalLines(txtDataSource);
if (lines == 2 ) {
String frstChar = String.valueOf(s.toString().charAt(s.toString().length() - 1));
txtDataSource.removeTextChangedListener(mTextWatcher);
txtDataSource.setText("" + frstChar);
txtDataSource.setSelection(txtDataSource.getText().toString().length());
txtDataSource.addTextChangedListener(mTextWatcher);
}
}
and now in 3rd line first character is getting displayed in EditText.
I am sharing my way so that if someone else would face such issue they can solve by this way..
Thanks
Try to solve the problem when you edit from the middle of the word.
"setSelection" keeps forcing you to write at the end of the text.
I have a Number Picker that allow user to enter some number within a fixed range.
By default the number will not allow the user to enter a number larger than the max set for the number picker.
However i would like to display a message when that happens. Is there a way to access the edittext in the number picker ? Thanks.
You can implement TextChangedListener that will register every change in the field so that you can act accordingly on it.
for example.
yourEditText = (EditText) findViewById(R.id.yourEditTextId);
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
yourEditText. ...
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
I'm making a scrabble scorer app. And he user inputs the letter, in EditText, the score is computed and the word form and is displayed too...Now if another player is to add letters to the previous word the user should not reenter the letters in the word he just needs to add the letter from the previous word...say-- "inter" is previous word ... adding "n" should display "intern" adding again "ational" should display "international"....
Any help would be greatly appreciated...thankss a lot...
TextWatcher inputTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
textview.setText(s.toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
editText.addTextChangedListener(inputTextWatcher);
Try this one
I have three editboxes which has retrict to enter only one numeric digit on each box.when i enter the value of first box the focus should be moved from 1st box to 2nd.After entering value to 2nd box.,its focus should automatically moved to 2nd to 3rd like that i need to do.Could anybody help me regarding this?
You can use requestFocus() API to shift the focus from the code,
Keep listening for the text, using textWatcher, once specified limit is reached, call EditTextreference.requestFocus() to shift the focus.
This code moves focus from one EditText to another when EditText length greater than 1.
EditText et1,et2;
et1 = (EditText)findViewById(R.id.id1);
et2 = (EditText)findViewById(R.id.id2);
et1.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
Integer textlength = et1.getText().length();
if(textlength>=1){ //If text length greater than 1 move to next EditText
et2.requestFocus();
}
}
});