1

Here is my code for dynamically creating ImageButtons inside of gridviews. When I run the activity, the ImageButtons are not clickable. I tried adding android:descendantFocusability="blocksDescendants" to the xml file but this did not fix the problem.

EDIT: I updated the adapter to be for ImageViews, not ImageButtons, but the problem persists.

Here is my custom adapter:

    package com.example.myname.myproject;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;

/**
 * Created by myname on 3/31/17.
 */

public class ImageArrayAdapter extends BaseAdapter {
    Context context;
    int[] currentResources;
    String[] currentIcons;


    public ImageArrayAdapter(Context context, String[] currentIcons, int[] currentResources){
        ImageArrayAdapter.this.context = context;
        ImageArrayAdapter.this.currentIcons = currentIcons;
        ImageArrayAdapter.this.currentResources = currentResources;
    }

    @Override
    public int getCount() {
        return currentResources.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            imageView.setBackgroundColor(Color.TRANSPARENT);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setClickable(true);
        imageView.setImageResource(currentResources[position]);
        return imageView;
    }

}

Here is my xml file for the GridView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myname.myproject.AdminControlTabbed$PlaceholderFragment">

<GridView
    android:id="@+id/templateGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:clickable="true"
    android:descendantFocusability="blocksDescendants"
    />

Thanks for the help.

kmindspark
  • 487
  • 1
  • 7
  • 18

2 Answers2

2

Why not ImageViews instead of ImageButtons. Then you have to only set an OnItemClickListener for your GridView in the activity class where you have initialized it. After you attach the adapter to it, you can attach a clickListener too like this:

myGridView.setAdapter(this, currentIconsArray, currentResourcesArray);
myGridViewsetOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              //code for handling onClick event
        }
    });
  • Hi, thanks for the help. However, I tried using ImageViews and have posted the updated adapter above, the images remain unclickable even though I set clickable to true. – kmindspark Apr 02 '17 at 18:05
  • Did you attach setOnItemClickListener to the GridView object in your activity class? Can you post the code of the activity where you have initialized the GridView? – Nikolai Kanchev Apr 02 '17 at 18:09
  • Ok, thanks. Setting the on item click listener worked. I thought setting clickable to true would be sufficient to make the "clicking" effect visible. – kmindspark Apr 02 '17 at 18:13
1

Activity.dispatchTouchEvent(MotionEvent) dispach it to other listeners

bhandari
  • 11
  • 2