Remove item from GridView in android on specific position on button click

How to delete grid view text items on button click by user define item position.

In this tutorial we are removing gridview inside items on external button click means we are accessing and deleting list string element using remove() method. Now after that we will use string list notify method to update again list values . So here is the complete step by step tutorial for Remove item from GridView in android on button click.

android-project-download-code-button

How to Remove item from GridView in android on specific position on button click.

Code for MainActivity.java file.

 package com.gridviewitemremove_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;
 Button Remove;
 String[] GridViewItem = new String[]{
 "ANDROID",
 "PHP",
 "BLOGGER",
 "WORDPRESS",
 "SEO",
 "JAVASCRIPT",
 "ASP.NET",
 
 };
 List<String> StringLIST;
 ArrayAdapter<String> arrayadapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 
 gridview = (GridView) findViewById(R.id.gridView1);
 
 Remove = (Button) findViewById(R.id.button1);
 
 StringLIST = new ArrayList<String>(Arrays.asList(GridViewItem));
 
 arrayadapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, StringLIST);

 gridview.setAdapter(arrayadapter);

 Remove.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 
 
 StringLIST.remove(3);
 
 arrayadapter.notifyDataSetChanged();

 Toast.makeText(MainActivity.this,"Delete Successfully", Toast.LENGTH_SHORT).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.gridviewitemremove_android_examples.com.MainActivity" >
 
 <GridView
 android:id="@+id/gridView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#02bd8a"
 android:numColumns="3" >
 
 </GridView>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:text="Click here to delete 4th Item" />

</RelativeLayout>

Screenshot:

delete-item

Remove item from GridView in android on specific position on button click

Click here to download Remove item from GridView in android on specific position on button click project with source code.

Leave a Reply

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