How can I run validation when my users exit textField in compose? - android

I have a function that will check if my textField has any issues
fun hasError(textFieldLabel: String):Boolean{}
I want this function to run only when the user has finished entering text.
When the user exists the text field, we assume they're finished and I run hasError() on that field
Here is what didn't work for me
val focusManager = LocalFocusManager.current
focusManager.moveFocus(FocusDirection.Next)
FocusDirection.Next Only moves the focus when you decide its appropriate, but doesn't listen when the user decides to move from one field to another
Modifier.onFocusChanged{}
This doesn't work because it triggers when the page loads, and calls hasError(), leaving all my textFields in an error state
I want to trigger hasError when the user exits a text field

You can simple listene for edit text change value. onValueChange is called when the text is changed and the text is finished,

YourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) { {
//User leaved your edit text
}
}
});

Related

android setError method interfering with manually setting icons

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

How to know when editText ends being editted?

I have two editTexts and when the user edits one and then hits done, it executes a method. I want to be able to know when the user stops editing it. Like in that case, with hitting the "done" button. Unfortunately, the user can 'stop' editing it, if he/she selects the other editText. For some strange reason, the OnEditorActionListener doesn't catch that case. What can I do about it? I've tried with onFocusChange, but that one is very unpredictable...
You can assume when the EditText looses focus
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//probably here!
}
});

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

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