android setError method interfering with manually setting icons - android

I'm using the setError method to show if an EditText field is incorrectly filled when the focus from that field shifts away. If it's correctly filled, I'm showing an icon: drawable.validated Here's my code:
#Override
public void afterTextChanged(Editable s) {
zip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (isValidZip(zip.getText().toString())) {
zip.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
} else {
zip.setError(getString(R.string.zip_error));
}
}
}
});
The problem is, the setError method is interfering with my manually setup of icon. And thus I can't see the drawable.validated icon even when the field is verified.
MORE DETAILS on when I get the error:
CASE 1 I fill the EditText field correctly in the very first time -> I change the focus from that field -> I can see the validated icon. See the following screenshot:
CASE 2 I fill the EditText zip field incorrectly -> I change the focus to Mobile Number field-> I see error in zip field-> Get back to the zip field and fill it correctly. Error in zip disappears -> change focus to Mobile Number field-> Now I can't see the VALIDATED icon in the zip field even if it is correctly filled:
drawable.validated is the blue-tick.
12345 is the zip field
What might be the issue and how to resolve that?

First of all, why do you setOnFocusChangeListener every time text change, you don't need to do this, you can just set it once.
I have just created test project and test it, works fine
mEditText = (EditText) findViewById(R.id.edit_text_input);
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (isValidZip(mEditText.getText().toString())) {
mEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_validated, 0);
} else {
mEditText.setError(getString(R.string.error_invalid_zip));
}
}
}
});
private boolean isValidZip(String s) {
return s.equals("correct");
}
Works as it should.I think your problem is that you are setting change listener every time text change.
You can try my application to see if it works correctly https://dropmefiles.com/a9ydI
Here the link to the complete source code
https://bitbucket.org/CROSP/testedittexterror/commits/all

Related

Android - Do something *after* text is changed in EditText

I build an android application. I have an EditText. I want to save changes (or anything else) automatically after the user change the text.
Now I use
editText.addTextChangedListener(textWatcher);
and
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
...
}
But it saves the changes after every little change (add \ remove a letter), and I want that it'll be saved only after the user finished (closed the keyboard, clicked on a different edittext, etc. ).
How can I do that?
Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control and you should save the data of editext. for example
EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
//SAVE THE DATA
}
}
});
Implement setOnFocusChangeListener for all EditTexts and you should handle Back Press event too. If the user change the data of edittext and didn't goto another EditText and pressed back then edited vallue will not save. So should use onKeyDown or onBackPressed too
As Kotlin is now official android language:
var editText = findViewById<EditText>(R.id.editText)
editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
//SAVE THE DATA
}
}

Edit box does not gain focus in android

i'm facing wired problem in android when i try to focus for a EditText control.
In my application, i am dynamically adding the edit controls, whenever new control is added i wanted to
give auto focus and popup the softkeypad automatically.
sometimes editbox get focus, but if i type text will not appear, sometimes text will appear in the other text control.
i'm observing this behaviour in my phone.
Here is my code snippet which i used to provide focus.
if(mOldEditBox!=null)
{
mOldEditBox.clearFocus();
}
textView.requestFocus();
//textView.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
//textView.setOnFocusChangeListener(focuschange);
mOldEditBox = textView;
i tried setting focuschangelistener event, still it didnt worked :(
OnFocusChangeListener focuschange = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
EditText txt = (EditText)v;
//txt.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.showSoftInput(txt, InputMethodManager.SHOW_FORCED);
}
}
};
Kindly help me where is the problem.. thanks in advance
You are probably not on the UI thread when requesting focus which might cause the strange behavior of the EditText. Try adding it to the message queue using the post(Runnable) method:
textView.post(new Runnable() {
public void run() {
textView.requestFocus();
}
});

Android: How disable EditText depending focus

I'm doing a app wich have two EditText. What I wanna do is, if you click on one and write on it, the other must erease anything It has and only shows the hint string. I'm triying doing it witha a check method that does:
if(celsius.isFocused()){
faren.setText(faren.getHint().toString());
}
if(faren.isFocused()){
celsius.setText(celsius.getHint().toString());
}
Then I call this method within the onCreate() method, but of course It only checks one time, and if use that checkMethod inside a loop, the app doesn't show anthing, It freezes. An suggestions?
Use the OnFocusChangeListener.
faren.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
celcius.setText(""); // This will automatically show the hint text
}
});

How to validate fields after focus is changed in edittext in android

I am trying to validate my edittext field after focus is changed but it gives when the time of typing only.i.e, after entering one charecter I am used below code
fname.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s){//some validations}
But my requirement, in my form so many fields are there if we go for validation submition time its a risky one. so please help me.
Add OnFocusChangeListener to your edit text
fname.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// If view having focus.
} else {
// If view not having focus. You can validate here
}
}
});

How to check whether a control gets a focus or not in android

I have a question that How to check whether the widget having a focus or not. Actually I have a form in which there is a DOB edit text, a user navigates all the edit text boxes with Next action on keyboard and I want when user navigates to DOB field from any of the control then a Calendar Dialog automatically appears, currently what happens is user have to click on edit text then a Calendar Dialog appears, I want whenever DOB field gets a focus then it automatically call the Calendar Dialog.
I have searched regarding the same enough on web but failed to achieve this. Please help me out about this problem.
Thanks in advance.
Is it not
if(view.isFocussed) {}
?
1) Create a Handler, as a field in your Activity. Like this:
private Handler myHandler = new Handler();
2) When you create the ListView, add a OnFocusChangeListener, like shown below.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
myHandler.postAtFrontOfQueue(new Runnable() {
public void run() {
myList.setSelection(0);
}
});
}
}
});

Categories

Resources