3

Is it possible to pass a String parameter on a button click of XML file to the methods of the view model file as like below

 android:onClick="@{v -> viewModel.postFeedRequest(@string/show_details)}"

I tried like above i am getting binding error.

Can anyone post any example on this.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Rakesh
  • 14,997
  • 13
  • 42
  • 62

2 Answers2

3

Try this:

 android:onClick="@{() -> viewModel.postFeedBackRequest(AppConstant.WithStringResource)}"

where AppConstant is your class and WithStringResource is your String constant with the value you want to send.

Update:

You can also directly send the string like this:

android:onClick='@{() -> viewModel.postFeedBackRequest("WithStringResource")}'

Or

 android:onClick="@{() -> viewModel.postFeedRequest(@string/show_details)}"
Deˣ
  • 4,191
  • 15
  • 24
1

Do like this In viiew model

  fun onClickFunction(num:String) {
            edExtnNumber.set(edTextField.get()+""+num)
        }

and in xml

android:onClick="@{() -> viewModel.onClickFunction(@string/value)}">
Sreejesh K Nair
  • 563
  • 1
  • 6
  • 16