Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to databind to onTextChanged for an EditText on Android?

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?

like image 616
Nilzor Avatar asked Nov 19 '15 08:11

Nilzor


People also ask

What is two-way data binding android?

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.

How do I get text from EditText?

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 do I edit a EditText file?

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.


2 Answers

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.

like image 194
Nilzor Avatar answered Sep 30 '22 14:09

Nilzor


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.

like image 25
Micer Avatar answered Sep 30 '22 13:09

Micer