How to get selected item from gridview in android

How to display GirdView clicked item inside textview.

GridView clicked item means there are so much type of items present inside a grid view and sometimes app developer needs to get selected item form app user so here is the complete step by step tutorial for How to get selected item from gridview in android.

android-project-download-code-button

How to get selected item from gridview in android.

Code for MainActivity.java file.

 package com.getgridviewselecteditem_android_examples.com;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class MainActivity extends Activity {

 GridView gridView;
 List<String> ItemsList;
 TextView DisplaySelectedItem;
 String selectedItem;
 String[] itemsName = new String[]{
 "ONE",
 "TWO",
 "THREE",
 "FOUR",
 "FIVE",
 "SIX",
 "SEVEN",
 "EIGHT",
 "NINE",
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 gridView = (GridView)findViewById(R.id.gridView1);
 
 DisplaySelectedItem = (TextView)findViewById(R.id.textView1);
 
 ItemsList = new ArrayList<String>(Arrays.asList(itemsName));
 
 gridView.setAdapter(new TextAdapter(this));
 
 gridView.setOnItemClickListener(new OnItemClickListener() {

 @Override
 public void onItemClick(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 
 selectedItem = parent.getItemAtPosition(position).toString();
 
 DisplaySelectedItem.setText(selectedItem +" is selected.");
 
 }
 });
 
 }
 
 private class TextAdapter extends BaseAdapter
 {

 Context context;
 
 public TextAdapter(Context context) 
 {
 this.context = context;
 }
 
 @Override
 public int getCount() {
 
 return itemsName.length;
 }

 @Override
 public Object getItem(int position) {
 // TODO Auto-generated method stub
 
 return itemsName[position];
 }

 @Override
 public long getItemId(int position) {
 
 // TODO Auto-generated method stub
 
 return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 // TODO Auto-generated method stub
 
 TextView text = new TextView(this.context);
 
 text.setText(itemsName[position]);
 
 text.setGravity(Gravity.CENTER);
 
 text.setBackgroundColor(Color.parseColor("#fbdcbb"));
 
 text.setLayoutParams(new GridView.LayoutParams(144, 144));
 
 
 return text;
 
 }
 
 
 }
}

Code for activity_main.xml layout file.

 <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.getgridviewselecteditem_android_examples.com.MainActivity" >

 
 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Selected Item Display Here"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 
 <GridView
 android:id="@+id/gridView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:numColumns="3"
 android:layout_below="@+id/textView1"
 android:layout_marginTop="35dp" >
 </GridView>
 
</RelativeLayout>

Screenshots:

gridview-item-clicked

How to get selected item from gridview in android

Click here to download How to get selected item from gridview in android project.

2 Comments

  1. Can we get the click of specific item on the gridview OnItemClickListener ??? how can we do that,… As my purpose of doing that is i want to increase the counter when we click the specific item on gridview .. Please reply

    • Yes Inderjeet you can do it by simply creating a Variable and all you have to do is increase the value of variable each time when some one clicked like ++a .

Leave a Reply

Your email address will not be published. Required fields are marked *