0

In viewModel i had a function like this:

fun getTextInputStyle(): Int {
val style = if (isTrue) {
    R.style.styleOne
} else {
    R.style.styleTwo
}
return style

}

In XML file there is a textInputLayout call the function from ViewModel:

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_first_name"
style="@{viewModel.getTextInputStyle}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_16sdp"
android:layout_marginTop="@dimen/_13sdp"
android:layout_marginEnd="@dimen/_16sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rg_title">


</com.google.android.material.textfield.TextInputLayout>

And here the error logged in the console:

      Cannot find a setter for 
        <com.google.android.material.textfield.TextInputLayout style> 
        that 
        accepts parameter type 'int'
   If a binding adapter provides the setter, check that the adapter is 
   annotated correctly and that the parameter type matches.

Sami Shorman
  • 149
  • 1
  • 11
  • Maybe this works: https://stackoverflow.com/questions/69692527/android-databinding-set-xml-style-from-a-function-call; you can simplify your function like this: `fun getTextInputStyle() = if(isTrue) R.style.styleOne else R.style.styleTwo` – Cactusroot Dec 28 '22 at 13:07
  • Thanks for your suggestion but that wont work for my case i want to change the whole style not the setTextAppearance as mentioned in the question you provide. – Sami Shorman Dec 29 '22 at 07:09

1 Answers1

1

You can use binding adapter to set style like this.

@BindingAdapter("bindTextViewStyle")
fun TextView.bindTextViewStyle(styleResourceId: Int) {
    this.style(styleResourceId)
}

and in XML

<TextView
    app:bindTextViewStyle="@{viewModel.textStyle}"
    .../>

Refer this link : https://stackoverflow.com/a/64009888/20839582

sajidjuneja
  • 184
  • 7