How to remove item from gridview in android

How to delete any item values from gridview item dynamically using coding.

In this tutorial we are creating a simple GridView to to display textview items. Now using list item removing method we have remove the needed list array element. After that we just notify the adapter that we have delete the element now update the list again. So here is the complete step by step tutorial for How to remove item from gridview in android.

android-project-download-code-button

How to remove item from gridview in android.

Code for MainActivity.java file.

 package com.removeitemfromgridview_android_examples.com;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends Activity {

 GridView gridview;
 String[] item = new String[]{
 "ONE",
 "TWO",
 "THREE",
 "FOUR",
 "FIVE",
 "SIX"
 };
 Button button;
 List<String> ITEM_LIST;
 ArrayAdapter<String> arrayadapter;
 String DeletedItem;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 gridview = (GridView)findViewById(R.id.gridView1);
 
 button = (Button)findViewById(R.id.button1);
 
 ITEM_LIST = new ArrayList<String>(Arrays.asList(item));
 
 arrayadapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, ITEM_LIST);
 
 gridview.setAdapter(arrayadapter);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 DeletedItem = ITEM_LIST.get(2);
 
 ITEM_LIST.remove(2);

 arrayadapter.notifyDataSetChanged();

 Toast.makeText(MainActivity.this,"Item Deleted" , Toast.LENGTH_LONG).show();
 
 }
 });
 
 }
 }

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

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Click Here to Remove 3rd Item" />

 <GridView
 android:id="@+id/gridView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:numColumns="3" >

 </GridView>
</RelativeLayout>

Screenshot:

How to remove item from gridview in android

Click here to download How to remove item from gridview in android project with source code.

Leave a Reply

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