Android TextWatcher doesn't work into two edit text consecutively - java

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);
}
}
}
});
}

Related

How to delete masked edittext content

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())

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);

'equals()' between objects of inconvertible types 'int' and 'TextWatcher' error

i have a strange error that i can't solve. The error is : 'equals()' between objects of inconvertible types 'int' and 'TextWatcher'. And this is my code:
if( textWatcher_ans.equals(R.string.editText_7)){
Toast.makeText(getApplication().getBaseContext(),
(R.string.Good),Toast.LENGTH_SHORT).show();
}
private final TextWatcher textWatcher_ans = new TextWatcher(){
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView_name.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView_name.setVisibility(View.GONE);
} else {
textView_name.setText("You have entered : " + editText_surname.getText());
}
}
I'm pretty stuck and i dont know what to do.
can someone pleas help me?
If your aim is to display the R.string.Good message if the text in editText_surname matches R.string.editText_7 then try something like this:
First attach the TextWatcher to editText_surname:
editText_surname.addTextChangedListener(textWatcher_ans);
In your afterTextChangedMethod:
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView_name.setVisibility(View.GONE);
} else {
textView_name.setText("You have entered : " + s.toString());
}
if (s.toString().equals(context.getString(R.string.editText_7)) {
Toast.makeText(getApplication().getBaseContext(),
(R.string.Good),Toast.LENGTH_SHORT).show();
}
}

How to get int value from one method to another

I need to carry my int value from one method to another, but I didnt find any solution. One method counts number of objects and the number needs to be "delivered" to another and check, if the count equals to something.
Do you have any idea, how to do it?
Check my codes below:
#Override
protected void onPostExecute(Void result) {
ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.countInBackground(callcount = new CountCallback(){
public void done(int count, ParseException e) {
if (e == null) {
Log.v("count is" + count, "count is");
//I need to export int count from "done" method to another
} else {
}
}
});
}
And the secont method, which has to recieve that value and use it:
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//i need to set int count from method above here
int threshold = 1;
int counter = mListView.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (mListView.getLastVisiblePosition() >= counter - threshold) {
updateData();
}
}
Whole code:
public class Fragment1 extends Fragment {
static ListView mListView;
static AnimalAdapter mAdapter;
static ProgressBar mProgressBar;
static EditText mEditText;
static LayoutInflater inflater;
CountCallback callcount;
int g_count = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.animalsfrag, container, false);
mListView = (ListView)rootView.findViewById(R.id.animal_list);
View header = getActivity().getLayoutInflater().inflate(R.layout.header, null);
header.setPadding(2, 8, 4, 2);
mListView.setVisibility(View.INVISIBLE);
mListView.requestFocus();
mListView.addHeaderView(header);
View footie = getActivity().getLayoutInflater().inflate(R.layout.footer, null);
mListView.addFooterView(footie);
footie.setVisibility(View.INVISIBLE);
mProgressBar = (ProgressBar) rootView.findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.VISIBLE);
RemoteDataTask task = new RemoteDataTask();
task.execute();
return rootView;
}
public void updateData() {
mListView = (ListView)getView().findViewById(R.id.animal_list);
final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.setCachePolicy(CachePolicy.NETWORK_ONLY);
query.orderByAscending("animal");
query.setLimit(mListView.getCount() + 5);
Log.v("updateData", "uploading data from Parse.com");
query.findInBackground(new FindCallback<Animal>() {
#Override
public void done(List<Animal> animals, ParseException error) {
if(animals != null){
mAdapter.clear();
mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.INVISIBLE);
RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);
footie.setVisibility(View.VISIBLE);
mAdapter.clear();
for (int i = 0; i < animals.size(); i++) {
mAdapter.add(animals.get(i));
}
}
}
}); }
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute(); }
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.countInBackground(callcount = new CountCallback(){
public void done(int count, ParseException e) {
if (e == null) {
Log.v("count is" + count, "count is");
if (mListView.getCount() == count) {
Log.v("all loaded", "executed");
g_count=count;
}
} else {
// The request failed
}
}
});
Log.v("onpostexecute", "executing");
mListView = (ListView) getView().findViewById(R.id.animal_list);
if(mListView.getCount() == 0){
updateData();
}
mEditText = (EditText) getView().findViewById(R.id.search_animal);
mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());
mListView.setVisibility(View.VISIBLE);
mListView.setTextFilterEnabled(true);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
}
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
int threshold = 1;
int counter = mListView.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (mListView.getLastVisiblePosition() >= counter
- threshold){
updateData();
Log.v("count", "count is" + g_count);
}
}
if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
View currentFocus = getActivity().getCurrentFocus();
if(currentFocus != null) {
currentFocus.clearFocus();
}
}
}
});
mEditText.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
System.out.println("Text ["+s+"]");
mAdapter.getFilter().filter(s.toString());
}
});
}
}
}
I'd be glad for any advice.
Thanks in advance.
If you are not calling function from another then you need to create a global variable.
Declare a variable just after your class declaration i.e.
int g_count = 0;
then in
public void done(int count, ParseException e) {
if (e == null) {
Log.v("count is" + count, "count is");
g_count=count;
}
Then in second fucntion
public void onScrollStateChanged(AbsListView view, int scrollState) {
int count=g_count; \\you dont really need to declare this, just use g_count
int threshold = 1;
int counter = mListView.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (mListView.getLastVisiblePosition() >= counter - threshold) {
updateData();
}
}

Categories