17

I have a GridView with 81 buttons on it. I want to add clicklistener to this gridview but it is not available. I have added the OnItemClickListener but it is not working and I cannot understand why. There is no error with the code. The only thing not working is the OnItemClickListener.

My gridview children which has a button on it(gridview_members.xml);

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

<Button
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:textStyle="bold"
    android:text="Code\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

</RelativeLayout>

My ImageAdapter class;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return 81;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    gridView =new View(mContext);

    gridView = inflater.inflate(R.layout.gridview_members, null);

    Button city = (Button) gridView.findViewById(R.id.city);

    return gridView;
}

}

Gridview implementation on activity_main.xml ;

<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:numColumns="6" >
</GridView>

And finally my MainActivity.java file;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}
}
Eric Garcia
  • 77
  • 1
  • 12
Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
  • android:focusable="false" android:focusableInTouchMode="false" add this to your Button and try once again.. – kalyan pvs Nov 25 '13 at 11:30
  • 1
    You may set `OnClickListener` inside your custom adapter to button. – Chintan Soni Nov 25 '13 at 11:32
  • possible duplicate of [Android - Gridview with Custom BaseAdapter, create onclicklistener](http://stackoverflow.com/questions/20052631/android-gridview-with-custom-baseadapter-create-onclicklistener) – saiful103a Nov 25 '13 at 11:49

7 Answers7

13

GridView is clickable no need to put button inside GridView.

To add listener to the grid add the following code:

// Implement On Item click listener
gridView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position) {
            case 0: 
                break;
            case 1:
                break;

        }
    }
}); 

And so on you can add all numbers in the cases.

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
Mohammed Saleem
  • 568
  • 5
  • 20
7

You are requesting click on the item(not on the button inside the item) so need to change your child XML layout

from

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

<Button
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:textStyle="bold"
    android:text="Code\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

</RelativeLayout>

to

<?xml version="1.0" encoding="utf-8"?>
<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:clickable="true"
    android:textStyle="bold"
    android:text="Code\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

or you can add ClickListener inside adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    gridView =new View(mContext);

    gridView = inflater.inflate(R.layout.gridview_members, null);

    Button city = (Button) gridView.findViewById(R.id.city);

                city.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    //Toast here

        }});

    return gridView;
}
Himanshu Lakhara
  • 80
  • 1
  • 1
  • 8
Noob
  • 2,857
  • 6
  • 33
  • 47
1

If you want to get text that clicked item you can use this

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String item = ((TextView)view.findViewById(R.id.ID_OF_TEXTVIEW)).getText().toString();
                Toast.makeText(showMissions.this, "" + item, Toast.LENGTH_SHORT).show();

            }
        });
E J Chathuranga
  • 927
  • 12
  • 27
0

I am suggesting you to add click listener in getview method rather than adding in activity class. try this code

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

gridView =new View(mContext);

gridView = inflater.inflate(R.layout.gridview_members, null);

Button city = (Button) gridView.findViewById(R.id.city);

city.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
            //Do your task here
        }
    });
    return gridView;
}
chappjc
  • 30,359
  • 6
  • 75
  • 132
0

In your CustomAdapter into

 onBindViewHolder(...
{
  yourViewHolder.gridview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        //Do Something
       }
    });

}

Apple Appala
  • 329
  • 4
  • 16
0

You can add click listener this way to your grid view items in Kotlin

grdlyt_merchant_color_code.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            loyaltyColor = parent.getItemAtPosition(position) as LoyaltyColor?
            colorCode= LoyaltyUtils.getLoyaltyColorById(loyaltyColor!!.colorId).colorCode
            val color = ContextCompat.getColor(requireContext(), colorCode!!)
            shape.setColor(color)
            loyalty_card_img.background = shape
            uncheckAllColors()
            loyalty_card_img.visibility = View.GONE
            loyaltyColor!!.isColorSelected = true

            mAdapter.notifyDataSetChanged()
        }

0

re-write your Adapter, this will work tested

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    
    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
    View gridView = inflater.inflate(R.layout.gridview_members,null);  
  
    YOUR_CUSTOM_OBJECT obj = yOUR_CUSTOM_OBJECT.get(position);

    Button city = (Button) gridView.findViewById(R.id.city);
    city.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Your Code
            Toast.makeText(c, "Hlw There " + obj.SOME_GET_FUNCTION(), Toast.LENGTH_LONG).show();
        }
    });
    return gridView;
}

And your Grid View Code would be like this:

<GridView
    android:id="@+id/gridData"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bluegradient"
    android:stretchMode="columnWidth" />
Monzur
  • 1,341
  • 14
  • 11