How to delete masked edittext content - java

for example: "2020/55" I have an edittext shape, put one after 4 numbers, write a number after it and it works well. My problem starts at the time of deleting. When deleting the part after /, it shows"/////" the figures of the part before / as and instead.I add your picture, how can I delete it.
enter image description here
And mask class
public class CaseInputMask implements TextWatcher {
int uzunluk = 0;
EditText girilenMetin;
public CaseInputMask(EditText girilenMetin) {
this.girilenMetin = girilenMetin;
this.girilenMetin.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String metin = girilenMetin.getText().toString();
uzunluk = metin.length();
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
String metin = charSequence.toString();
String girilenDeger = girilenMetin.getText().toString();
if (girilenDeger.length() == 4) {
metin += '/';
girilenMetin.setText(metin);
girilenMetin.setSelection(metin.length());
}
} catch (Exception e) {
}
}
#Override
public void afterTextChanged(Editable editable) {
}
}

You should check deletion like this:
public class CaseInputMask implements TextWatcher {
private boolean running = false;
private boolean deleting = false;
private final String inputMask = "####/##";
public CaseInputMask() {
}
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
deleting = count > after;
}
#Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable editable) {
if (running || deleting) {
return;
}
running = true;
int length = editable.length();
if (length < inputMask.length()) {
if (inputMask.charAt(length) != '#') {
editable.append(inputMask.charAt(length));
} else if (inputMask.charAt(length-1) != '#') {
editable.insert(length-1, inputMask, length-1, length);
}
}
running = false;
}
}
And
girilenMetin.addTextChangedListener(new CaseInputMask())

Related

EditText cannot delete manually in Android java

I've been confused why my EditText can't delete my text manually.When I tried to scan the image that converts to text and display the text to editText or OCR sometimes I have this bugs, sometimes only 3 numbers will type instead of 23, and sometimes it runs smoothly, why did this happen? I have sample image below which can't delete the whole text displayed on EdiText manually, can anyone help me with this issue? I can't find the error yet since it just rarely appears, thanks in advance.
Java file
private int prevCount = 0;
// to have space after 4 number
private boolean isAtSpaceDelimiter(int currCount) {
return currCount == 4 ||currCount == 9 || currCount == 14 || currCount == 19;
}
//declared on onCreate()
scannedCardNumber(edt_card_number_inputted,til_card_number_inputted);
public void scannedCardNumber(EditText card_number , TextInputLayout tilCard){
card_number.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence s, int i, int i1, int i2) {
String cardNumber = card_number.getText().toString();
if(s.toString().length() != 23){
tilCard.setError(required_field);
}
else if(!cardNumber.matches("[0-9 ]+")) {
tilCard.setError("Invalid Format");
}
else{
tilCard.setError(null);
}
}
#Override
public void afterTextChanged(Editable editable) {
String field = editable.toString();
int currCount = field.length();
if (shouldIncrementOrDecrement(currCount, true)){
appendOrStrip(field, true);
} else if (shouldIncrementOrDecrement(currCount, false)) {
appendOrStrip(field, false);
}
prevCount = card_number.getText().toString().length();
}
});
}
private void appendOrStrip(String field, boolean shouldAppend) {
StringBuilder sb = new StringBuilder(field);
if (shouldAppend) {
sb.append(" ");
} else {
sb.setLength(sb.length() - 1);
}
edt_card_number_inputted.setText(sb.toString());
edt_card_number_inputted.setSelection(sb.length());
}
private boolean shouldIncrementOrDecrement(int currCount, boolean shouldIncrement) {
if (shouldIncrement) {
return prevCount <= currCount && isAtSpaceDelimiter(currCount);
} else {
return prevCount > currCount && isAtSpaceDelimiter(currCount);
}
}

requestFocus not changing the focus when when using getCurrentFocus

I am making an OTP Activity and need to implement delete feature, I have the below function that should be fired onKey up of the backspace button. My thought is that I could use getCurrentFocus to know which fields are currently being filled and then use requestFocus to select the one before, but it does not seem to work, I have another function for goToNext that works just fine, so I am confused why.
public void goToPrevious() {
EditText currentFocus = (EditText) getCurrentFocus();
if (currentFocus == otpOne) {
return;
}
if (currentFocus == otpTwo) {
otpOne.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpFour) {
otpThree.requestFocus();
return;
}
}
Below is the code for goToNext that works fine.
public void goToNext() {
EditText currentFocus = (EditText) getCurrentFocus();
if (currentFocus == otpOne) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpTwo) {
otpThree.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpFour.requestFocus();
return;
}
if (currentFocus == otpFour) {
return;
}
}
I call the goToPrevious by implementing an onKeyListener, below is the code
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
EditText currentFocus = (EditText) getCurrentFocus();
Log.i("ENOUGH", "onKey: " + keyCode + " " + event.getAction() + " " + KeyEvent.KEYCODE_DEL);
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DEL: {
currentFocus.setText(null);
goToPrevious();
return false;
}
}
return false;
}
I got to fix it by passing the value of getCurrentFocus directly into goToPrevious for some very weird reason the value of getCurrentFocus inside the onKey handler is different from goToPrevious, below is the code I ended up with.
public void goToNext(EditText currentFocus) {
if (currentFocus == otpOne) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpTwo) {
otpThree.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpFour.requestFocus();
return;
}
if (currentFocus == otpFour) {
return;
}
}
public void goToPrevious(EditText currentFocus) {
if (currentFocus == otpOne) {
return;
}
if (currentFocus == otpTwo) {
otpOne.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpFour) {
otpThree.requestFocus();
return;
}
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i("ENOUGH", "onKey: " + keyCode + " " + event.getAction() + " " + KeyEvent.KEYCODE_DEL);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
EditText currentFocus = (EditText) getCurrentFocus();
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DEL: {
if (currentFocus.getText().length() > 0) {
currentFocus.setText(null);
} else {
goToPrevious(currentFocus);
EditText focused = (EditText) getCurrentFocus();
focused.setText(null);
}
return false;
}
case KeyEvent.KEYCODE_FORWARD:
return true;
case KeyEvent.KEYCODE_BACK:
return true;
case KeyEvent.KEYCODE_ENTER:
return true;
default: {
currentFocus.setText("");
goToNext(currentFocus);
}
}
}
return false;
}
You can use like this following:
et1.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) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et2.requestFocus();
}
else if(s.length()==0)
{
et1.clearFocus();
}
}
});
et2.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) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et3.requestFocus();
}
else if(s.length()==0)
{
et1.requestFocus();
}
}
});
et3.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) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et4.requestFocus();
}
else if(s.length()==0)
{
et2.requestFocus();
}
}
});
et4.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) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et5.requestFocus();
}
else if(s.length()==0)
{
et3.requestFocus();
}
}
});
et5.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) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et6.requestFocus();
}
else if(s.length()==0)
{
et4.requestFocus();
}
}
});
et6.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) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et6.clearFocus();
}
else if(s.length()==0)
{
et5.requestFocus();
}
}
});

How to get locale of string on android?

I want to know what is the locale of input text in edittext!
However i tried to get the keyboard locale but that wasn't helpful.
I don't want to type english in the edittext so help me!
I want user to know that he/she can't type english!
Sorry for my bad english!
Here is my code :
edtUsernameLogin.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (editable.toString().isEmpty()) {
mtfUsernameLogin.setLabelColor(G.resources.getColor(R.color.cpb_white), "نام کاربری");
} else {
String locale = G.inputMethodManager.getCurrentInputMethodSubtype().getLocale();
if (locale.equals("fa") || locale.equals("ar")) {
mtfUsernameLogin.setLabelColor(G.resources.getColor(R.color.sPink), "نام کاربری باید انگلیسی باشد !");
} else {
mtfUsernameLogin.setLabelColor(G.resources.getColor(R.color.sGreen), "نام کاربری");
}
}
}
});
The Solution is :
InputFilter usernameFilter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String blockCharacterSet = " اآبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی";
if (blockCharacterSet.contains(source)) {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.grey));
edtNameEdit.setHint("نام و نام خانوادگی");
return source.toString();
} else {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.sPink));
edtNameEdit.setHint("نام و نام خانوادگی باید فارسی باشد!");
return "";
}
}
};
edtNameEdit.setFilters(new InputFilter[]{usernameFilter});
And important line :
edtNameEdit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

Overwrite text in EditText that has only 1 char is not working

I have multiple EditText views , each EditText view can contain only 1 char.
I need to make this rule - if I focus on one EditText , and it already has some text inside - then overwrite it . Also - if I press on delete key - I need the text to be cleared inside that view.
Then - I am checking if the EditText views has 1 empty cell - if not - checking if the EditText views has the correct letters.
I could have managed to make the clear button work, but I can not make the overwrite.
I did tried to use the TextWatcher , but it didn't work for me.
The EditText views are created dynamically .
Here is my code :
Answer.java
public class Answer {
String answer;
int answer_length;
int cell_margin=10;
int cell_size=180;
EditText[] EditTextArray;
public Answer(RelativeLayout rLayout1, Context context , String answer) {
this.answer = answer;
answer_length = answer.length();
if (answer_length>6){
cell_margin = 4;
cell_size = 110;
}
EditTextArray = new EditText[answer_length];
AnswerCell EditTextToSeeFirst = new AnswerCell(context,cell_size);
setListener(EditTextToSeeFirst);
EditTextArray[0] = EditTextToSeeFirst;
RelativeLayout.LayoutParams fparams = new RelativeLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
fparams.setMargins(cell_margin,0,cell_margin,0);
rLayout1.addView(EditTextToSeeFirst, fparams);
for (int i = 1; i<answer_length ; i++){
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.addRule(RelativeLayout.LEFT_OF, EditTextArray[i-1].getId());
lparams.setMargins(cell_margin,0,cell_margin,0);
AnswerCell newEditText = new AnswerCell(context,cell_size);
setListener(newEditText);
EditTextArray[i] = newEditText;
rLayout1.addView(EditTextArray[i], lparams);
}
rLayout1.setGravity(Gravity.CENTER );
}
public void setListener(AnswerCell ac){
ac.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("test","test");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
goToNextAvailableCell();
}
});
}
public void goToNextAvailableCell(){
for (int i = 0; i<answer_length ; i++) {
if(EditTextArray[i].getText().toString().matches("")){
EditTextArray[i].requestFocus();
return;
}
}
//Did not found empty cell
checkCorrectAnswer();
}
private void checkCorrectAnswer(){
String tryAnswer = "";
for (int i = 0; i<answer_length ; i++) {
tryAnswer += EditTextArray[i].getText().toString();
}
if (tryAnswer.matches(answer)){
Log.d("Correct !!","Correct Answer");
}
}
}
AnswerCell.java
public class AnswerCell extends EditText{
public AnswerCell(final Context context, int cell_size) {
super(context);
this.setId(View.generateViewId());
this.setBackgroundResource(R.color.answerCellBackground);
this.setHeight(cell_size);
this.setWidth(cell_size);
this.setFilters(new InputFilter[] {new InputFilter.LengthFilter(1)});
this.setCursorVisible(false);
this.setGravity(Gravity.CENTER);
this.setOnFocusChangeListener( new View.OnFocusChangeListener(){
public void onFocusChange( View view, boolean hasfocus){
if(hasfocus){
view.setBackgroundResource( R.drawable.answer_cell_has_focus);
}
else{
view.setBackgroundResource( R.drawable.answer_cell_lost_focus);
}
}
});
this.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
if(keyCode == KeyEvent.KEYCODE_DEL) {
((EditText)v).setText("");
return true;
}
return false;
}
});
}
}
thanks !
It will work with this code
TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//YOUR CODE
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//YOUR CODE
}
#Override
public void afterTextChanged(Editable s) {
String outputedText = s.toString();
// mOutputText.setText(outputedText);
}
};
Then add this in oncreate
mInputText.addTextChangedListener(watcher);
e2.addTextChangedListener(watcher);
e3.addTextChangedListener(watcher);
e4.addTextChangedListener(watcher);

Android TextWatcher doesn't work into two edit text consecutively

I just want to do that If I enter a grade then It will change to the points field according to entered grade and If I enter a point then It will change the grade filed according to entered point. I added textChangeListener for both editText but when I enter something my app just doesn't response.
http://i.stack.imgur.com/50LYo.png
grade.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 (grade.getText().toString().equals("A+"))
{
points.setText("4.00");
}
else if (grade.getText().toString().equals("A"))
{
points.setText("3.75");
}
else if (grade.getText().toString().equals("A-"))
{
points.setText("3.50");
}
else if (grade.getText().toString().equals("B+"))
{
points.setText("3.25");
}
else if (grade.getText().toString().equals("B"))
{
points.setText("3.00");
}
else if (grade.getText().toString().equals("B-"))
{
points.setText("2.75");
}
else if (grade.getText().toString().equals("C+"))
{
points.setText("2.50");
}
else if (grade.getText().toString().equals("C"))
{
points.setText("2.25");
}
else if (grade.getText().toString().equals("D"))
{
points.setText("2.00");
}
else if (grade.getText().toString().equals("F"))
{
points.setText("0.0");
}
else
points.setText(null);
}
#Override
public void afterTextChanged(Editable s) {
}
});
points.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 (points.getText().toString().matches("[4]|[4]\\.[0]{0,2}"))
{
grade.setText("A+");
}
else if (points.getText().toString().matches("[3]\\.[7][5]{1}"))
{
grade.setText("A");
}
else if (points.getText().toString().matches("[3]\\.[5][0]{0,1}"))
{
grade.setText("A-");
}
else if (points.getText().toString().matches("[3]\\.[2][5]{1}"))
{
grade.setText("B+");
}
else if (points.getText().toString().matches("[3]|[3]\\.[0]{0,2}"))
{
grade.setText("B");
}
else if (points.getText().toString().matches("[2]\\.[7][5]{1}"))
{
grade.setText("B-");
}
else if (points.getText().toString().matches("[2]\\.[5][0]{0,1}"))
{
grade.setText("C+");
}
else if (points.getText().toString().matches("[2]\\.[2][5]{1}"))
{
grade.setText("C");
}
else if (points.getText().toString().matches("[2]|[2]\\.[0]{0,2}"))
{
grade.setText("D");
}
else if (points.getText().toString().matches("[0]|[0]\\.[0]{0,2}"))
{
grade.setText("F");
}
else
grade.setText(null);
}
#Override
public void afterTextChanged(Editable s) {
}
});
Try following code:
Call setFocus in onViewCreated() method of fragment or onCreate() in activity
setFocus(grade);
setFocus(points);
TextWatcher gradeWatcher = 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) {
}
#Override
public void afterTextChanged(Editable s) {
if (grade.getText().toString().equals("A+"))
{
points.setText("4.00");
}
else if (grade.getText().toString().equals("A"))
{
points.setText("3.75");
}
else if (grade.getText().toString().equals("A-"))
{
points.setText("3.50");
}
else if (grade.getText().toString().equals("B+"))
{
points.setText("3.25");
}
else if (grade.getText().toString().equals("B"))
{
points.setText("3.00");
}
else if (grade.getText().toString().equals("B-"))
{
points.setText("2.75");
}
else if (grade.getText().toString().equals("C+"))
{
points.setText("2.50");
}
else if (grade.getText().toString().equals("C"))
{
points.setText("2.25");
}
else if (grade.getText().toString().equals("D"))
{
points.setText("2.00");
}
else if (grade.getText().toString().equals("F"))
{
points.setText("0.0");
}
else
points.setText(null);
}
};
TextWatcher pointWatcher = 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) {
}
#Override
public void afterTextChanged(Editable s) {
if (points.getText().toString().matches("[4]|[4]\\.[0]{0,2}"))
{
grade.setText("A+");
}
else if (points.getText().toString().matches("[3]\\.[7][5]{1}"))
{
grade.setText("A");
}
else if (points.getText().toString().matches("[3]\\.[5][0]{0,1}"))
{
grade.setText("A-");
}
else if (points.getText().toString().matches("[3]\\.[2][5]{1}"))
{
grade.setText("B+");
}
else if (points.getText().toString().matches("[3]|[3]\\.[0]{0,2}"))
{
grade.setText("B");
}
else if (points.getText().toString().matches("[2]\\.[7][5]{1}"))
{
grade.setText("B-");
}
else if (points.getText().toString().matches("[2]\\.[5][0]{0,1}"))
{
grade.setText("C+");
}
else if (points.getText().toString().matches("[2]\\.[2][5]{1}"))
{
grade.setText("C");
}
else if (points.getText().toString().matches("[2]|[2]\\.[0]{0,2}"))
{
grade.setText("D");
}
else if (points.getText().toString().matches("[0]|[0]\\.[0]{0,2}"))
{
grade.setText("F");
}
else
grade.setText(null);
}
};
/*---------- SET FOCUS LISTENERS ON EDIT TEXT--------------------------*/
public void setFocus(EditText editText){
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (v.getId() == grade.getId()) {
grade.addTextChangedListener(gradeWatcher);
points.removeTextChangedListener(pointWatcher);
} else if (v.getId() == points.getId()) {
points.addTextChangedListener(pointWatcher);
grade.removeTextChangedListener(gradeWatcher);
}
}
}
});
}

Categories