jramirez
jramirez

Reputation: 484

LinearLayout and OnItemClickListener not working

I've read that's possible to add an OnItemClickListener on a LinearLayout as if it was a ListView , but when I try to add it to the LinearLayout, it throws an error that's says that isn't defined the method for LinearLayouts. Do you know how can I set the onItemClickListener on the layout? Thanks a lot!

Upvotes: 1

Views: 2974

Answers (2)

Naveed Jamali
Naveed Jamali

Reputation: 668

Try this code, I hope it will work

linearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
ll.getChildAt(0).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
              //your code here
              functionCalledOnItemClicked();

           }
      });

Upvotes: 0

Sam
Sam

Reputation: 86958

You cannot add an OnItemClickListener to a LinearLayout, but you can add an OnClickListener. You will need to set the LinearLayout as clickable.

Example XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true" >

Example Java:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do something
    }
});

If you need more help post your relevant code and your LogCat errors.

Upvotes: 3

Related Questions