Rakesh L
Rakesh L

Reputation: 1168

Android - How to pass object of a view from XML with data binding

I am building an Android application in which I am using Data binding library. I am little new to Data binding concept and looking for the solution for the following

There is a layout file called otp_validation.xml

<data>
    <variable
        name="otpViewModel"
        type="dial.to.go.otp.OTPViewModel" />
</data>
<ConstraintLayout
 .....................
 .....................
 .....................
<LinearLayout
            android:id="@+id/otp_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_20sdp"
            android:orientation="horizontal"
            android:weightSum="4">

            <androidx.appcompat.widget.AppCompatEditText
                android:layout_width="@dimen/_40sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_weight="1"
                android:background="@drawable/all_edit_selector"
                android:gravity="center"
                android:inputType="number"
                android:maxLength="1"
                android:textSize="@dimen/_22sdp" />

            <androidx.appcompat.widget.AppCompatEditText
                android:layout_width="@dimen/_40sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_marginLeft="@dimen/_8sdp"
                android:layout_weight="1"
                android:background="@drawable/all_edit_selector"
                android:gravity="center"
                android:inputType="number"
                android:maxLength="1"
                android:textSize="@dimen/_22sdp" />

</LinearLayout>


        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="@dimen/_30sdp"
            android:background="@color/colorPrimary"
            android:backgroundTint="@color/colorPrimary"
          
          <!-- I want to pass the view object of otp container as a parameter in the method validateOTP()
            android:onClick="@{() -> otpViewModel.validateOTP()}"
           
            android:src="@drawable/drawable_fab_proceed"
            app:borderWidth="0dp"
            app:fabSize="normal"
            app:rippleColor="@color/colorAccent" />
           
</ConstraintLayout>

Please notice the method validateOTP() being invoked in onClick event. I would like to send the view object of linear layout (otp_container) as a parameter. Please help me out for providing me the solution for this.

Upvotes: 2

Views: 3268

Answers (2)

rahat
rahat

Reputation: 2056

To pass the view which is clicked then you can do it

android:onClick="@{(view)->viewModel.onOthers(view)}"

To pass a view other than the clicked view, pass the id,

android:onClick="@{(view)->viewModel.onOthers(otpContainer)}"

Note that the signature of the method should be like

fun onOthers(view:View){
  //your code goes here
}

So that it can allow any type of view.

UPDATE otp_container will be otpContainer for Binding.

Upvotes: 2

Dhanuesh
Dhanuesh

Reputation: 1596

The logic of binding objects to an XML would pretty much be the same, create those objects in the activity/fragment class, initialize the corresponding variables in the XML file and use binding to bind the objects and variables.

Try the following

Activity class

val yourLinearLayout = findViewById<View>(R.id.otp_container)
binding.view = yourLinearLayout 

XML File

<data>
    <variable
        name="otpViewModel"
        type="dial.to.go.otp.OTPViewModel" />

    <variable
        name = "view"
        type = "android.widget.LinearLayout"
</data>

//rest of the logic

     <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        ..........          
        ..........
        android:onClick="@{() -> otpViewModel.validateOTP(view)}"
        ..........
        ........./>

Though it works, its indeed a bad practice. Viewgroup must know nothing about the views because if the activity/fragment gets destroyed/recreated, the view references in the viewgroup would lead to memory leaks

Upvotes: 1

Related Questions