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) {
}
});
Related
in my application i have many edit text and i implemented textWatcher for them i want them to increase and decrease a textView number but i can only increase it
i tried to check if the old text is bigger that new text and decrease the textview number, but it returns false everytime
my code :
editText.addTextChangedListener(new TextWatcher() {
String oldText = "";
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.oldText = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (!editText.getText().toString().equals("")) {
int price = Integer.parseInt(editText.getText().toString()) * price_db;
productPrice.setText(price + "");
int totalPrice_n = Integer.parseInt(totalPrice.getText().toString());
int min = Integer.parseInt(editText.getText().toString()) - Integer.parseInt(oldText);
if(Integer.parseInt(editText.getText().toString()) > Integer.parseInt(oldText)){
totalPrice.setText((totalPrice_n + min * price_db) + "");
}else{
totalPrice.setText((totalPrice_n - min * price_db) + "");
}
}
}
});
my problem is that if condition only returns false and goes to else part, also my EditText default text is set to 0 so i think beforeTextChanged only take on 0 and check if new text is bigger than 0
i change editText's text with 2 button ( + , - ) and i want when i click on + button to increase the TextView number and also when i click on - button to decrease TextView number but it only increase it i dont know why
Replace my code with your code and check dude !!! :)
editText.addTextChangedListener(new TextWatcher() {
String oldText = "";
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.oldText = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (!editText.getText().toString().equalsIgnoreCase(""))
{
int amount=Integer.parseI`enter code here`nt(editText.getText().toString());
int price = Integer.parseInt(editText.getText().toString()) * price_db;
int totalPrice_n = Integer.parseInt(totalPrice.getText().toString());
productPrice.setText(price);
int min = amount - Integer.parseInt(oldText);
if(Integer.parseInt(oldText) > 0)
{
if(amount > Integer.parseInt(oldText)){
totalPrice.setText((totalPrice_n + min * price_db));
}else{
totalPrice.setText((totalPrice_n - min * price_db));
}
}
else
Toast.makeText(context,"old text is 0",Toast.LENGTH_LONG).show();
}
}
});
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;
}
});
});
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) {
}
});
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
}
});
}
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;
}