Reputation: 2676
I would like to enable a AppCompatButton when all fields in a form are complete.
I am using a ViewModel and would like to use Databinding to enable this.
I have 2 methods that fire when text is changed on the views to update an objects data in the viewmodel.
The issue I am running into is when both fields are complete, I need to enable a button on the layout allowing them to proceed.
An example would be log in, when the username and password fields are fulled in, the log in button becomes enabled.
Upvotes: 5
Views: 5018
Reputation: 326
You can use the same solution like here
But if you want use only AndroidArch and DataBinding you can create your own approach like below:
class MyVM : ViewModel() {
...
val mLoginLiveData = MutableLiveData<String>()
val mPasswordLiveData = MutableLiveData<String>()
val mLoginPasswordMediator = MediatorLiveData<Boolean>()
...
init {
mLoginPasswordMediator.addSource(mLoginLiveData) { validateForm() }
mLoginPasswordMediator.addSource(mPasswordLiveData) { validateForm() }
...
}
private fun validateForm() {
// put your validation logic here, and update the following value
// as `true` or `false` based on validation result
// mLoginPasswordMediator.value = ...
}
override fun onCleared() {
// DO NOT forget to remove sources from mediator
mLoginPasswordMediator.removeSource(mLoginLiveData)
mLoginPasswordMediator.removeSource(mPasswordLiveData)
}
}
and in your activity class listen your MediatorLiveData
:
class MyActivity : AppCompatActivity() {
...
override fun onCreate(savedInstanceState: Bundle?) {
//Obtain your ViewModel class here
//Initialize binding here
...
mBinding.lifecycleOwner = this
mVM.mLoginPasswordMediator.observe(this, Observer { validationResult ->
mBinding.yourButton.isEnabled = validationResult
})
}
}
And didn't forget to use your LiveData
s' in 'your_activity_layout'.xml
:
...
//Add your ViewModel class to layout here
<EditText
...
android:text="@={vm.mLoginLiveData}"
... />
...
<EditText
...
android:text="@={vm.mPasswordLiveData}"
... />
...
Upvotes: 9
Reputation: 158
To do this you would have to add onEditorActionListener to your editText which you would enable the button lets say for example i have an edit text named password which i want to enable a login button when it is not empty and when the text entered i it is not less than 8 i would validate and enable the button like this:
password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (password.getText().length() > 0 && password.getText.lenght !< 8) {
//Automatically click button to login
loginBtn.setEnabled(true);
return true;
}
return false;
}
});
You can also use a TextWatcher or an addTextChangedListener on the EditText see here for fore details: https://freakycoder.com/android-notes-66-how-to-use-textwatcher-for-more-than-one-edittext-e190b7ae1070
https://www.dev2qa.com/android-enable-disable-button-by-edittext-text-length/
Upvotes: 0