How to set click listener to LinearLayout using data binding

android data binding onclick example
android data binding onclick not working
android data binding default value
android data binding null check
android data binding update view
data binding java
data binding android
android data binding fragment

I am currently trying to set a click listener to a LinearLayout view in the .xml layout file using data binding.

I have managed to get it to work well on other views like a Button or TextView, but for some reason it is not working with a LinearLayout.

This is the bare bones of my attempts and I still cannot get it to work:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:clickable="true"
    android:focusable="true"
    android:onClick="@{action::linearLayoutClicked}"
    android:orientation="vertical">
</LinearLayout>

Where linearLayoutClicked is my method defined in the action class:

public void linearLayoutClicked(View view) {
    // specific logic
}

I have also tried with child views and those child views with clickable and focusable set to false as well as duplicateParentState set to true and false.

That action is exactly the same action that is being used on other views which is working correctly.

Is this a bug or am I doing this incorrectly? Why is this not working for LinearLayout but works without any problems for other views?

You can handle any view click event like below. Hope it helps!

1) Create interface for handle click event like below.

interface OnClickHandlerInterface {
    void onClick(View view)
}

2) Implement that click listener in action class as shown below

class MainActivity implements OnClickHanderInterface{
    @Override
    void OnClick(View view){

    }
}

3) Now bind this interface in XML file.

<data>

    <variable
        name="clickHandler"
        type=".OnClickHandlerInterface" />
</data>

4) Now register this interface in action class with use of binding object

mActivityMainBinding.clickHandler = this

5) Now set onClick on any which you want to set click listener. For you it's LinearLayout

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:clickable="true"
    android:focusable="true"
    android:onClick="@{(v)-> clickHandler.onClick(v)}"
    android:orientation="vertical">
</LinearLayout>

6) Now handle click when your linearLayout clicked you can get click on interface which is implemented in action class.

@Override
void OnClick(View view){
    switch(view.getId()){
    case R.id.linearLayout:
        // Handler click and do some actions
        break;
    }
}

6) As mentioned above you can get layout click by data binding. Hope it will work for you.

Thanks

Happy Coding!!

Layouts and binding expressions, Develop your application faster & smarter with Data Binding — Event Binding — Part 2 Data Binding does not create a listener and sets a null listener instead. Now you can reference it directly by using the referencing expression as below​. You can use the following mechanisms to handle an event: Method references: In your expressions, you can reference methods that conform to the signature of the listener method. When an expression evaluates to a method reference, Data binding wraps the method reference and owner object in a listener,

So everything was set up correctly and the generated bindings file shows the click listener being set up correctly, but for some very odd reason half the bindings were working and the newer ones weren't. By newer ones meaning the LinearLayout and all the attempts around it.

What solved the problem was a simple cache invalidation and respective restart and voila, the click listener is working perfectly on the LinearLayout. Just remember to set it clickable and focusable and any child views set as not clickable so they don't consume the event before the parent.

3 entire days around this one, took less than 10 minutes discussing with others here in the comments to remember trying to invalidate the cache of Android Studio, unbelievable of me...

Event Binding, OnClickListener which has a method onClick() , so the attribute for this event is android:onClick . - SearchViewsetOnSearchClickListener(View. For example, the binding adapter can take care of calling the setText() method to set the text property or call the setOnClickListener() method to add a listener to the click event. The most common binding adapters, such as the adapters for the android:text property used in the examples in this page, are available for you to use in the android.databinding.adapters package.

If Invalidate Cache does not work.

then Rebuild project from Build>Rebuild. Fix errors if any then Rebuild again.

Using data binding in Android - Tutorial, Using data binding, you can also handle events right from the layout xml Now the layout that has a button with the click function in the MyHandler Listener. data >. < LinearLayout. android:layout_width = "match_parent". The above LinearLayout will automatically add children using the item.xml layout with the “data” variable set to the items in entries. This can be used for any ViewGroup in which addView() is

Android Jetpack:Empower your UI with Android Data Binding-Part 2, Custom event using lambda expression. Define Interface public interface ClickHandler { public void  Instead of having to create an XML attribute to bind a listener, you can take advantage of Data Binding’s built-in support for two-way bindings. Behind the scenes, the generated Binding class for your layout is still going to create a listener, but it will do all the heavy-lifting to maintain and keep track of this listener.

Event Handling using DataBinding in Android., RecyclerView (a evolved version of ListView) uses an adapter that tells which Right now, you can use Data Binding to set click listeners right on the layout  Please Note: If the on touch listener’s onTouch method return true, then the on click listener will not be invoked. So the onTouch method return false by default. Of course if you want to avoid on click after on touch, you can make the onTouch method return true.

Data Binding Library, Using Data Binding on RecyclerView is very simple and clean. The layout setup is identical to the previous one but in this case, we should have an item layout that defines the necessary LinearLayout> EVENT HANDLING. Android offers support to write declarative layouts using data binding. This minimizes the necessary code in your application logic to connect to the user interface elements. The usage of data binding requires changes in your layout files. Such layout files starts with a layout root tag followed by a data element and a view root element. The

Comments
  • Ignore data binding for the moment. If you remove the android:onClick() attribute, and you call setOnClickListener() on your LinearLayout, do you get the results that you want?
  • @CommonsWare Yes, in-code click listener works correctly on the LinearLayout.
  • Hmmm... you might consider looking at the code that gets generated by data binding and see what's in there when you have your android:onClick attribute in place. What you have should be OK, though the combination that you're trying (event handler registered via data binding on a non-interactive View) may not have been thoroughly tested.
  • use android:onClick="linearLayoutClicked" instead of android:onClick="@{action::linearLayoutClicked}" i tried and its working
  • @Thomas Read my sample, it is already using that attribute with that value.