Thursday, August 20, 2015

How to use GridView OnItemClickListener in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#b0c1aa"
    >
    <!--
        GridView
            GridView is a ViewGroup that displays
            items in a two-dimensional, scrollable grid.
    -->
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f7f7f3"
        android:layout_marginBottom="10dp"
        android:padding="10dp"
        android:fontFamily="sans-serif-condensed"
        />
    <GridView
        android:id="@+id/gv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="3"
        android:layout_below="@id/tv"
        android:background="#c7dac0"
        >
    </GridView>
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity {

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

        // Get the widgets reference from XML layout
        GridView gv = (GridView) findViewById(R.id.gv);
        final TextView tv = (TextView) findViewById(R.id.tv);

        String[] plants = new String[]{
                "Striped alder",
                "Amy root",
                "Arizona sycamore",
                "Green ash",
                "Cherry birch",
                "Gray birch",
                "Mahogany birch",
                "Spice birch",
                "Yellow birch"
        };

        List<String> plantsList = new ArrayList<String>(Arrays.asList(plants));

        /*
            setAdapter (ListAdapter adapter)
                Sets the data behind this GridView.

                Parameters
                adapter : the adapter providing the grid's data
         */
        // Data bind GridView with ArrayAdapter (String Array elements)
        gv.setAdapter(new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1,plantsList));

        /*
            setOnItemClickListener (AdapterView.OnItemClickListener listener)
                Register a callback to be invoked when an item
                in this AdapterView has been clicked.

                Parameters
                listener : The callback that will be invoked.
         */
        // Set an item click listener for GridView widget
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get the GridView selected/clicked item text
                String selectedItem = parent.getItemAtPosition(position).toString();

                // Display the selected/clicked item text and position on TextView
                tv.setText("GridView item clicked : " +selectedItem
                        + "\nAt index position : " + position);
            }
        });
  }
}
More android examples