Blog purpose for android basic example for android app developer. any query please my contact

Friday 3 April 2020

how to remove item from gridview in android

In this tutorial we tend to area unit removing gridview within things on external button click means that we tend to area unit accessing and deleting list string part victimization remove() methodology. currently, afterward we are going to use a string list give notice methodology to update once more list values. therefore here is that the complete step by step tutorial for taking away items from GridView in golem on button click.

I started this automaton app development tutorials journal in 2019. At the mendicancy, I wrote tutorials for beginners concerning a way to develop Associate in Nursing automaton app. Most of my tutorials are terribly clear to scan and straightforward to know and straightforward to implement any existing project. Last five years, I coated several automaton app developments topics like layouts, widgets, bitmap, drawable, button, text view, alert dialog, card view, recycler read, API, date time picker, spinner, list view, grid view, theme, style, design, knowledge binding, area info, volley network library, automaton syntax, java syntax, notification, navigation drawer, wifi, Bluetooth, system permissions, storage, media, seek bar, progress bar, web view, animation, transition, toolbar, google map, screen, jetpack, work manager, automaton ktx, fragment, audio manager, activity, action bar and plenty of additional.

 

 

Follow the button click then a gridview item delted :

 

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();

            }

        });

 

 

 

    }

 

 

Follow the source code:

 

1.MainActivity.java:

package com.akash.removeitemgrid;


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",

            "HTML",

            "C#",

            "Python",

            "PHP",

            "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();

            }

        });




    }

}



2.activity_main.xml:

<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=".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 Item" />


</RelativeLayout>



3.strings.xml:

<resources>

    <string name="app_name">Remove item grid</string>

</resources>



4.dimens.xml:


<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    </resources>


5.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.removeitemgrid">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



6.Output:


how to remove item from gridview in android



how to remove item from gridview in android




No comments:

Post a Comment