How can I get EditText change? - android

I have a EditText and I want to inform each time text changes (with entering each character). What implementation should I use and what function should I override?

Use the addTextChangedListener method on your EditText and make your class implement or define an inner class implementing the TextWatcher class:
https://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)

You can declare a boolean variable as false and at each time et.edited make it true

Related

How to Enable/Disable a button based on all editetext is not empty using viewmodel and databinding in kotlin android

I need to create a Login Activity in an Android app using Data Binding with ViewModel using Kotlin. I want to enable/disable a Button based on the content of an EditText field. The expected behavior that I'm trying to achieve is the button should get enabled only when none of theEditText fields is empty.
Step 1: In ViewModel declare
var isEnabled : ObservableBoolean? = null and initialize it in init block.
Step 2: Set value of isEnabled in your Text Change Listener like
isEnabled?.set(!isLoginFormValid())
Step 3: bind variable in xml file like
android:enabled="#{viewmodel.isEnabled}"
Add a boolean MediatorLiveData in your ViewModel and bind this to the enabled attribute of the button.
You should have MutableLiveData fields that's two-way bound to your EditTexts. Add these as MutableLiveData as sources to the boolean MediatorLiveData so that it can observe changes to the EditTexts as user enters values.
Add whatever logic in the MediatorLiveData observers to set it's value to true/false depending on whatever logic you want (e.g. values of the EditTexts should not be null or empty)
you can put validations in the on click method of the login button.like:-
if(edittext_one!=null){then do the code here for the onclick }
like this you can put as much validations as you want on the edittext using if else statements.i hope this helps

Globaly request focus on setError()

Is there any way that i can set an attribute globally and request focus whenever an error is set?
Suppose i am validating a lot of EditText fields and setting an Error for each of them, after setting each other i have to request a focus:
editText.setError("Error");
editText.requestFocus();
However there exist a lot of fields and i am required to do call .requestFocus() in every single one. Is there any way to set it globally or setting explicitly is the only way around?
you can create one function for that and call that function in your whole app by giving class reference.
public static void EdittextError(EditText editText, String Error) {
editText.setError(Error);
editText.requestFocus(); }
Pass edit text obj. and Error String in function parameter.
Is there any way to set it globally or setting explicitly is the only way around?
No, there's no special attribute in system widget, so you have to set it by hand or extend EditText and override setError() to automatically request focus when called and then use MyEditText in your layouts

some EditTexts with only one TextWatcher

I'm trying to user TextWatcher interface in order to detect which EditText was changed.
I have an activity with 10 EditTexts, and it looks weird to use 10 TextWatchers for each one of them.
There is any way to use only one TextWatcher and to use switch statement on the Editable in the functions afterTextChanged?
What I would do is to create a class that extends EditText and create a TextWatcher in that class. You can then implement those EditTexts in your XML or create them programmatically in Java with the TextWatcher listening for each EditText.
Don't know if this will work for you but you can give it a try.
I have never tried this before, but it should work if you check if the EditText is in focus. There are a few ways to go about this and the most straightforward one is to check the focus of the EditText inside your TextWatcher methods. You'll need to do something like this:
if(mEdit1.hasFocus()) {
...
} else if(mEdit2.hasFocus()) {
...
} else if(mEdit3.hasFocus()) {
...
}
A different approach would be to use an OnGlobalFocusChangeListener on your root view and set a variable indicating with EditText currently has focus. It would still require a lot of if statements to check for which EditText has the focus, but may be a more reusable solution.

How to check the edittext Listener is remove or not

I am using the the two editext and add the seperate textWatcher classes object
when some one give the focus then second one textListener is remove.
Then
My Question is how to check the textListener is add or remove
thnax in advance
AFAIK, There is no method to get the current assigne TextWatcher instance of the EditText object.
Rather you just can do like,
mEdiText.addTextChangedListener(null);
before you assign any TextWatcher and then assign the Watcher object mEdiText.addTextChangedListener(new MyTextWatcher()); to the EditText in any event or any condition.
Assigning null will remove the previous TextWatcher.
And to know is it really worked or not, just run your app and test it once.

Copy String from an android from EditText into String variable on Android

I have a class that creates a view to gather data via a function getView() that provides a view with an EditText.
This class has also has variable answer.
When the user chances the EditText I want to store the content of the EditText in answer.
If I would use an onKeyListener I fear that the answer will probably get stored before the last letter is entered.
Is there a good way to handle this in the getView() function via some other listener?
You should addTextChangedListener to your EditText and implement in your class TextWatcher
Then you will just take the text from the methods and store in your answer

Categories

Resources