In Yigit Boyar and George Mount's talk on Android Databinding they illustrate how easy it is to bind to TextWatcher
's onTextChanged
(at 13:41). On a Button. Are their slides wrong? First of all the Button
View doesn't have an onTextChanged
property. It neither has a setOnTextChanged
method. Neither does EditText
. But they both have addTextChangedListener
which takes a TextWatcher
as input.
So what are they talking about? How do they do it? Their example code does not compile, but gives this error:
Error:(17) No resource identifier found for attribute 'onTextChanged' in package 'android'
How do I bind to a "Text Changed Event" on any View, or EditText in particular, with the Android Databinding framework?
The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time. // Avoids infinite loops.
Now, before we start we need to know some method of EditText which we use to get or fetch the text written in an EditText that is . getText() method and the other one is . setText() method that we use to set some pre-written text or string. EditText demo; demo=(EditText)findViewById(R.
How to include a Edittext in an Android App: First of all, create a new Android app, or take an existing app to edit it. In both the case, there must be an XML layout activity file and a Java class file linked to this activity. Open the Activity file and include a Edittext field in the layout (activity_main.
Actually it works out of the box. I think my mistake was using an old version of the data binding framework. Using the latest, this is the procedure:
View:
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/username" android:text="Enter username:" android:onTextChanged="@{data.onTextChanged}" />
Model:
public void onTextChanged(CharSequence s, int start, int before, int count) { Log.w("tag", "onTextChanged " + s); }
Make also sure that you have assigned model into DataBinding
For ex. in your activity
lateinit var activityMainDataBinding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) activityMainDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) val dataViewModel = ViewModelProvider(this).get(DataViewModel::class.java) activityMainDataBinding.dataModel = dataViewModel }
Make sure you are referncing gradle build tools v1.5.0 or higher and have enabled databinding with android.dataBinding.enabled true
in your build.gradle.
edit: Functioning demo project here. view. model.
To extend @Nilzors answer, it is also possible to pass text and/or other parameters in the layout:
View:
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/username" android:text="Enter username:" android:onTextChanged="@{(text, start, before, count) -> viewModel.onUsernameTextChanged(text)}" />
ViewModel:
public void onUsernameTextChanged(CharSequence text) { // TODO do something with text }
You always need to pass either zero or all parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With