Edittext backspace long hold is not working - android

I have Edittext in which I have added TextWatcher.when I press backspace its works but if i hold backspace it's not deleting all text
public sTextWatcher implements TextWatcher{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence source, int start, int before, int count) {
}
}
#Override
public void afterTextChanged(Editable s) {
edittext.setText(s.toString());
}

Try this code
editText.addTextChangedListener( new TextWatcher() {
boolean isEdiging;
#Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override public void afterTextChanged(Editable s) {
if(isEdiging) return;
isEdiging = true;
String str = s.toString().replaceAll("[^\\d]", "");
double s1 = 0;
try {
s1 = Double.parseDouble(str);
}catch (NumberFormatException e){
e.printStackTrace();
}
NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
((DecimalFormat)nf2).applyPattern("###,###.###");
s.replace(0, s.length(), nf2.format(s1));
isEdiging = false;
} editText.addTextChangedListener( new TextWatcher() {
boolean isEdiging;
#Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override public void afterTextChanged(Editable s) {
if(isEdiging) return;
isEdiging = true;
String str = s.toString().replaceAll("[^\\d]", "");
double s1 = 0;
try {
s1 = Double.parseDouble(str);
}catch (NumberFormatException e){
e.printStackTrace();
}
NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
((DecimalFormat)nf2).applyPattern("###,###.###");
s.replace(0, s.length(), nf2.format(s1));
isEdiging = false;
}
});
});

Related

Cyclical replacement the content of EditText with the limited length

For example, I have EditText with length limitation of two characters. When the first and second letters entered it's ok. But when we will try to enter a third letter the first letter should be replaced with it. Next letter should replace the second and so on in a circle. How can I do this one.
Try using TextWatcher on your edit text to achieve the goal
editText.addTextChangedListener(new TextWatcher() {
private int lastModifiedIndex = 1;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 2) {
char toReplace = s.charAt(s.length() - 1);
if (lastModifiedIndex == 0) {
editText.setText("" + s.charAt(lastModifiedIndex) + toReplace);
lastModifiedIndex = 1;
editText.setSelection(s.length());
} else {
editText.setText("" + toReplace + s.charAt(lastModifiedIndex));
lastModifiedIndex = 0;
editText.setSelection(s.length());
}
} else {
lastModifiedIndex = 1;
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Try this one
editText.addTextChangedListener(new TextWatcher() {
private int charLimit = 5;
private int position = 5;
private String newSequence;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > charLimit ) {
if (position == charLimit) {
newSequence = s.subSequence(s.length()-1, s.length()).toString() +
s.subSequence(1, charLimit);
position = 1;
} else {
position++;
newSequence = s.subSequence(0, position).toString() +
s.subSequence(position+1, s.length());
}
editText.setText(null);
editText.setText(newSequence);
editText.setSelection(position);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});

Android: Using DecimalFormat in TextWatcher with Edittext

I have a problem with using decimal format in textwatcher.
If I don't use DecimalFormat in TextWatcher, there is no problem in the program.
I can delete and set all edittext (above codes/photo left)
But if I use, the program stops.(below codes/photo right)
For example, when I enter the value in the first edittext and go to the second edittext, the program stops.
I also tried the code in afterTextChanged and try-catch but there was no change.
How can i solve this problem?
the_photo
etType5.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (getCurrentFocus() == etType5) {
if (etType5.length() != 0) {
double minute = Double.parseDouble(etType5.getText().toString());
double second = minute * 60.0;
etType6.setText(String.valueOf(second));
} else {
etType6.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});
etType6.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (getCurrentFocus() == etType6) {
if (etType6.length() != 0) {
double second = Double.parseDouble(etType6.getText().toString());
double minute = second * 0.016666667;
etType5.setText(String.valueOf(minute));
} else {
etType5.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});
etType5.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
DecimalFormat df = new DecimalFormat("#.###");
if (getCurrentFocus() == etType5) {
if (etType5.length() != 0) {
double minute = Double.parseDouble(etType5.getText().toString());
double second = minute * 60.0;
etType6.setText(df.format(second));
} else {
etType6.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});
etType6.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
DecimalFormat df = new DecimalFormat("#.###");
if (getCurrentFocus() == etType6) {
if (etType6.length() != 0) {
double second = Double.parseDouble(etType6.getText().toString());
double minute = second * 0.016666667;
etType5.setText(df.format(minute));
} else {
etType5.setText("");
}
}
}
public void afterTextChanged(Editable s) {
}
});

Automatically add colon to Edittext

I want to add a mac address to my database via EditText.
Is it possible to add a colon (:) after every second character?
The colon should be displayed directly in the EditText.
EDIT: Tried it. And I think I am on the right way ( your anwers confirm this :P )
inputMac = (EditText) view.findViewById(R.id.editText_mac);
inputMac.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 2 || s.length() == 5 || s.length() == 7 || s.length() == 9 || s.length() == 12 ){
inputMac.setText(inputMac.getText() + ":");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
But now after 12 characters I get e.g. 123456789123:::::
I've already answered a similar question, so this is how you can achieve it:
String mTextValue;
Character mLastChar = '\0'; // init with empty character
int mKeyDel;
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean flag = true;
String eachBlock[] = myEditText.getText().toString().split(":");
for (int i = 0; i < eachBlock.length; i++) {
if (eachBlock[i].length() > 6) {
flag = false;
}
}
if (flag) {
myEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
mKeyDel = 1;
return false;
}
});
if (mKeyDel == 0) {
if (((myEditText.getText().length() + 1) % 3) == 0) {
myEditText.setText(myEditText.getText() + ":");
myEditText.setSelection(myEditText.getText().length());
}
mTextValue = myEditText.getText().toString();
} else {
mTextValue = myEditText.getText().toString();
if (mLastChar.equals(':')) {
mTextValue = mTextValue.substring(0, mTextValue.length() - 1);
myEditText.setText(mTextValue);
myEditText.setSelection(mTextValue.length());
}
mKeyDel = 0;
}
} else {
myEditText.setText(mTextValue);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (s.length()>0) {// save the last char value
mLastChar = s.charAt(s.length() - 1);
} else {
mLastChar = '\0';
}
}
#Override
public void afterTextChanged(Editable s) {}
});
PS: It also handle deleting characters.
I tried I think I found a way wich is not that complicated. (Its not perfect but I think I will make it)
inputMac.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
macLengthBefore = inputMac.length();
Log.d("Textlänge BEFORE", macLengthBefore.toString());
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
macLengthAfter = inputMac.length();
Log.d("Textlänge AFTER", macLengthAfter.toString());
if (macLengthAfter > macLengthBefore && ((inputMac.getText().length() + 1) % 3 == 0) && inputMac.length() <= 15) {
inputMac.setText(inputMac.getText() + ":");
inputMac.setSelection(inputMac.getText().length());
}
}
});
Thanks #Rami for modulo query
After few trial and errors I was able to write a simple and working code:
mEditText.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) {
if ((s.toString().length() < 17) && ((before == 1 && count == 2) || (before == 4
&& count == 5))) {
String string = mEditText.getText().toString();
string = string.concat(":");
mEditText.setText(string);
mEditText.setSelection(string.length());
}
}
});
Below code goes into your xml file:
<EditText
android:id="#+id/edit_text"
style="#style/textfield_wh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="17"
android:digits="abcdefABCDEF0123456789:"
android:inputType="text" />
Try this,
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
*****APPLY YOUR LOGIC HERE*****
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}

How can i detect ENTER pressed without using OnkeyListner

I am having a problem with the on key listener, which i have completely explained here Unable to get line number on press of ENTER
Now if i am using a textwatcher to implement certain functionalities on my edittext, i need the line number everytime when my cursor moves to the next line. i want to know when the ENTER key is pressed, how can i do that
here is the code of textwatcher
scene.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//here is your code
strcheck = s.toString().substring((s.length()-1),s.length());
if (nowUpper)
strcheck = strcheck.toUpperCase();
else if (nowLower)
strcheck = strcheck.toLowerCase();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if (nowUpper){
if(ix==1)
{
if(strcheck=="\n")
strcheck="a";
Log.v(strcheck, strcheck);
ix=0;
scene.setText(scene.getText().toString().substring(0,scene.length()-1) + strcheck);
scene.setSelection(scene.getText().length());
}
else
{
ix=1;
}
// TODO Auto-generated method stub
}
}
});
try this TextWatcher:
TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkForNewLine(s, start, count, "new line entered");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
checkForNewLine(s, start, count, "new line deleted");
}
#Override
public void afterTextChanged(Editable s) {
}
private void checkForNewLine(CharSequence s, int start, int count, String msg) {
int end = start + count;
for (int i = start; i < end; i++) {
if (s.charAt(i) == '\n') {
Log.d(TAG, msg);
}
}
}
};
You can add OnKeyListener for your EditText:
myEdit.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP
&& keyCode == KeyEvent.KEYCODE_ENTER) {
int currentLine = getCurrentLine(myEdit);
}
return false;
}
});
private int getCurrentLine(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;
}

update textview without any buttons

I have 2 edittext fields empty and i want to when i type numbers inside the two fields a 3rd textview will automatically get the division of the 2 above edittexts. So no buttons to calculate that etc.
The thing is when i enter one number on any field the app gets force closed. Here is my code and the logcat:
amount.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
AddFuelActivity.this.updateValue();
}
});
litres.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
AddFuelActivity.this.updateValue();
}
});
protected void updateValue() {
int amountInt = Integer.parseInt(amount.getText().toString());
int litresInt = Integer.parseInt(litres.getText().toString());
float priceFuelAuto = 0;
priceFuelAuto = amountInt / litresInt;
fuelPrice.setText("€ " + priceFuelAuto);
}
logcat errors: i think these are the important ones.
01-22 13:23:21.650: E/AndroidRuntime(671): java.lang.NumberFormatException: Invalid int: ""
01-22 13:23:21.650: E/AndroidRuntime(671): at java.lang.Integer.invalidInt(Integer.java:138)
01-22 13:23:21.650: E/AndroidRuntime(671): at java.lang.Integer.parseInt(Integer.java:359)
01-22 13:23:21.650: E/AndroidRuntime(671): at java.lang.Integer.parseInt(Integer.java:332)
01-22 13:23:21.650: E/AndroidRuntime(671): atcom.example.mycarfuel.AddFuelActivity.updateValue(AddFuelActivity.java:155)
01-22 13:23:21.650: E/AndroidRuntime(671): atcom.example.mycarfuel.AddFuelActivity$2.onTextChanged(AddFuelActivity.java:67)
I fixed my code. Here is what i did:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (litres.length() > 0) {
AddFuelActivity.this.updateValue();
}
}
and
int amountInt = 0;
int litresInt = 0;
try {
amountInt = Integer.parseInt(amount.getText().toString());
litresInt = Integer.parseInt(litres.getText().toString());
} catch (NumberFormatException e) {
litresInt = 0;
amountInt = 0;
}
if(amountInt !=0 && litresInt != 0){
float priceFuelAuto = 0;
priceFuelAuto = amountInt / litresInt;
fuelPrice.setText("€ " + priceFuelAuto);
}
thank you for your replies people
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if(edittext1.length()!=0){
updateValue()
}
else{
edittext2.setText("");
}
just write below code..
amount.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.length()>0) <<<<===Just put this condition
AddFuelActivity.this.updateValue();
}
});
litres.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
if(s.length()>0) <<<<===Just put this condition
AddFuelActivity.this.updateValue();
}
});
protected void updateValue() {
int amountInt = Integer.parseInt(amount.getText().toString());
int litresInt = Integer.parseInt(litres.getText().toString());
float priceFuelAuto = 0;
priceFuelAuto = amountInt / litresInt;
fuelPrice.setText("€ " + priceFuelAuto);
}

Categories

Resources