Bahu
Bahu

Reputation: 1596

Android: setOnClickListener not working for GridView

Hi I'm implementing setOnClickListener for GridView parent

Code:

Main layout

<LinearLayout
            android:id="@+id/profile_ll_friends"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:descendantFocusability="blocksDescendants"
            android:background="@drawable/linear_layout_border"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/profile_img_friends"
                android:layout_width="@dimen/profile_img_apf_width"
                android:layout_height="@dimen/profile_img_apf_height"
                android:layout_gravity="center_horizontal"
                android:contentDescription="@string/cont_desc"
                android:scaleType="fitXY"
                android:src="@drawable/bg_480_800"
                android:visibility="gone" />

            <GridView
                android:id="@+id/friends_photos_gv"
                android:layout_width="@dimen/profile_img_apf_width"
                android:layout_height="@dimen/profile_img_apf_height" >
            </GridView>

            <TextView
                android:id="@+id/profile_tv_friends"
                android:layout_width="@dimen/profile_img_apf_width"
                android:layout_height="@dimen/txt_size_height_profile"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="0dp"
                android:background="@color/txt_common_white"
                android:gravity="center"
                android:text="@string/txt_Friends"
                android:textColor="@color/txt_common_black"
                android:textSize="@dimen/txt_size" />
        </LinearLayout>

Activity

LinearLayout friendsLayout = (LinearLayout) findViewById(R.id.profile_ll_friends);

friendsLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ProfileActivity.this, FriendsListActivity.class);
                startActivity(intent);
            }
        });

Adapter Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/friends_photos_img"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/cont_desc"
        android:scaleType="fitXY"
        android:src="@drawable/bg_480_800"
        android:visibility="visible" />

</LinearLayout>

When clicking on TextView (profile_tv_friends) area, setOnClickListener working fine but when clicking on GridView (friends_photos_gv) area, setOnClickListener is not working

For this problem i tried

android:focusable="false"
android:focusableInTouchMode="false"

for ImageView (friends_photos_img) in the adapter layout and also tried

android:descendantFocusability="blocksDescendants"

for LinearLayout (profile_ll_friends), but setOnClickListener is not working when clicking on GridView area

Upvotes: 0

Views: 585

Answers (3)

Aditay Kaushal
Aditay Kaushal

Reputation: 1091

You should use:

friendlayout.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        // DO something

    }
});

Upvotes: 1

Kristo
Kristo

Reputation: 1367

A Gridview works like a listview you can try something like this.

    gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        // DO something

    }
    });

For more information checkout this link. GridView

Another mistake might be that you are referencing the linearlayout here instead of your actual GridView.

LinearLayout friendsLayout = (LinearLayout)   findViewById(R.id.profile_ll_friends);

Upvotes: 1

Iulian
Iulian

Reputation: 1156

Replace:

friendsLayout.setOnClickListener(new OnClickListener() {

with:

friendsLayout.setOnClickListener(new View.OnClickListener() {

Upvotes: 0

Related Questions