0

I am trying to make a simple two-way data binding behavior. So I have an edit text in my layout and when the value is changed, I want to update the variable inside my activity.

Here is the code inside my activity:

class MainActivity : AppCompatActivity() {

    private var input: String = ""

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.input = input
    }
}

Here is my edit text:

    <data>
        <variable
            name="input"
            type="String" />
    </data>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Input number.."
        android:inputType="number"
        android:text="@={input}" />

But I received an error saying:

Cannot find a setter for <android.widget.EditText android:textAttrChanged> that accepts parameter type 'androidx.databinding.InverseBindingListener' I have tried to make store the input variable as an observable variable of an object class that extends base observable.

  • Check https://stackoverflow.com/questions/40188894/cannot-find-the-setter-for-attribute-with-parameter – SimpleCoder Feb 13 '23 at 07:28
  • @SimpleCoder not sure if you meant for me to use binding adapters instead. But I used to be able to do something like this, but after reading the docs here https://developer.android.com/topic/libraries/data-binding/two-way#two-way-attributes, I suspect that edit text on text change is no longer directly supported for two way binding – William Tan Feb 13 '23 at 07:43

0 Answers0