Sabir Hossain
Sabir Hossain

Reputation: 1205

Is there any way to setOnItemclickListener for a Linear Layout with more than one child item

I have a Linear Layout with more than 1 child item is there any way to set onItemClickListener for my Linear layout?

Here is xml file

  <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal">

           <ImageView
               android:layout_width="80dp"
               android:layout_height="80dp"
               android:padding="1dp"
               android:src="@drawable/fish"
               android:layout_marginLeft="7dp"
               />
           <ImageView
               android:layout_width="80dp"
               android:layout_height="80dp"
               android:padding="1dp"
               android:src="@drawable/fruits"
               android:layout_marginLeft="8dp"/>
           <ImageView
               android:layout_width="80dp"
               android:layout_height="80dp"
               android:padding="1dp"
               android:src="@drawable/vegetables"
               android:layout_marginLeft="8dp"/>
           <ImageView
               android:layout_width="80dp"
               android:layout_height="80dp"
               android:padding="1dp"
               android:src="@drawable/oils"
               android:layout_marginLeft="8dp"/>
       </LinearLayout>

Upvotes: 0

Views: 571

Answers (5)

koperko
koperko

Reputation: 2487

You seem to not understand the difference between an AdapterView widgets and basic ViewGroups .. There is no way to set one listener with a position argument out-of-the-box on LinearLayout..

You can however subclass it, expose an interface and iterate over child elements. On each one of them set an OnClickListener which just calls the implementation of your exposed interface along with index of this element in the iteration.

public class MyLinearLayout {

    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(i);
                }
            });
        }
    }
}

Upvotes: 0

Tabish Hussain
Tabish Hussain

Reputation: 852

One way you can do that is

for(int i= 0 ; i < linearLayout.getChildCount() ; i++){
   View view  = linearLayout.getChildAt(i);
   view.setTag(i);
   view.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v){
           int position = (int) v.getTag();
        }
   });
}

other way is to extends LinearLayout and create one with this functionality

Upvotes: 1

Vishal Chhodwani
Vishal Chhodwani

Reputation: 2577

first set id of LinearLayout in your XML. and then set OnClickeListener on this LinearLayout from your code.

Try This.

 <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:id="@+id/linearLayout">

       ...

   </LinearLayout>

 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

 linearLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Do what you want.
                }
            });

Upvotes: 0

bogdanN
bogdanN

Reputation: 193

Add an id to your LinearLayout, inside activity get a reference to that linear layout and set onClickListener to that object

Upvotes: 0

toteto
toteto

Reputation: 11

Just set

android:clickable="true"

to the LinearLayout. Also, don't forget to provide onClickListener to the view when you inflate it.

Upvotes: 0

Related Questions