Friday, August 21, 2015

How to add an item to GridView dynamically 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="#71c6ab"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Item : Itchweed"
        />
    <GridView
        android:id="@+id/gv"
        android:layout_width="675dp"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:numColumns="3"
        android:background="#e8ea63"
        android:verticalSpacing="5dp"
        android:horizontalSpacing="5dp"
        android:gravity="left"
        android:layout_below="@id/btn"
        >
    </GridView>
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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

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

import android.widget.Toast;


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);
        Button btn = (Button) findViewById(R.id.btn);

        // Initializing a new String Array
        String[] plants = new String[]{
                "Catalina ironwood",
                "Cabinet cherry",
                "Pale corydalis",
                "Pink corydalis",
                "Land cress",
                "Coast polypody",
                "Water fern"
        };

        // Populate a List from Array elements
        final List<String> plantsList = new ArrayList<String>(Arrays.asList(plants));

        // Create a new ArrayAdapter
        final ArrayAdapter<String> gridViewArrayAdapter = new ArrayAdapter<String>
                (this,android.R.layout.simple_list_item_1, plantsList);

        // Data bind GridView with ArrayAdapter (String Array elements)
        gv.setAdapter(gridViewArrayAdapter);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Add/insert item to ArrayAdapter
                // Insert at the end of ArrayAdapter
                // ArrayAdapter is zero based index
                plantsList.add(plantsList.size(),"Itchweed");

                // Update the GridView
                gridViewArrayAdapter.notifyDataSetChanged();

                // Get the newly added item from ArrayAdapter
                String addedItemText = plantsList.get(plantsList.size()-1);

                // Confirm the addition
                Toast.makeText(getApplicationContext(),
                        "Item added : " + addedItemText, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
More android examples