In this post, I will show you how to add and remove grids in GridView. And also, I am explained about SharedPreferences. Project Structure...

How to Add and Remove Grids in GridView Dynamically How to Add and Remove Grids in GridView Dynamically

How to Add and Remove Grids in GridView Dynamically

How to Add and Remove Grids in GridView Dynamically


In this post, I will show you how to add and remove grids in GridView. And also, I am explained about SharedPreferences.
Project Structure:
Create a new Project in Eclipse/Android Studio with the required Specifications.
Codes:
activity_main.xml
Create activity_main.xml and replace it with the following code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:weightSum="5"
 android:orientation="vertical">

 <GridView
  android:id="@+id/gridView1"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:columnWidth="100dp"
  android:gravity="center"
  android:numColumns="auto_fit"
  android:stretchMode="columnWidth"
  android:layout_weight="4.5"/>

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="0.5"
  android:orientation="horizontal"
  android:weightSum="2" >

  <Button
   android:id="@+id/btn_add"
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:background="@null"
   android:text="@string/hint_add"/>

  <Button
   android:id="@+id/btn_remove"
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:background="@null"
   android:text="@string/hint_remove"/>

 </LinearLayout>

</LinearLayout>

activity_main_single.xml
Create activity_main_single.xml and replace it with the following code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:padding="5dp"
 android:orientation="vertical"
 android:gravity="center">

 <ImageView
  android:id="@+id/grid_item_image"
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:src="@mipmap/ic_launcher"
  android:contentDescription="@string/app_name"
  android:gravity="center" />

 <TextView
  android:id="@+id/grid_item_label"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="@string/label"
  android:textSize="15sp"
  android:gravity="center" />

</LinearLayout>

MainActivity.java
Open MainActivity.java and replace it with the following code
package com.example.grid;
 
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
 
import com.example.grid.adapter.ImageAdapter;
 
import java.util.ArrayList;
 
public class MainActivity extends Activity {

 GridView gridView;
 Button btn_add, btn_remove;
 ImageAdapter adapter;
 ArrayList<String> list;
 SharedPreferences sharedPreferences;
 SharedPreferences.Editor editor;
 String Pref_Name = "Grid";
 int count = 3;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  gridView = (GridView) findViewById(R.id.gridView1);
  btn_add = (Button) findViewById(R.id.btn_add);
  btn_remove = (Button) findViewById(R.id.btn_remove);

  sharedPreferences = getSharedPreferences(Pref_Name, MODE_PRIVATE);
  count = sharedPreferences.getInt("count", 3);

  list = new ArrayList<>();

  for (int i = 0; i < count; i++) {
   list.add("");
  }

  adapter = new ImageAdapter(this, list);
  gridView.setAdapter(adapter);

  btn_add.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    list.add("");
    adapter.notifyDataSetChanged();
    gridView.setAdapter(adapter);

    editor = sharedPreferences.edit();
    editor.putInt("count", count + 1);
    editor.apply();

    Toast.makeText(getApplicationContext(), "One Image Added", Toast.LENGTH_SHORT).show();

   }
  });

  btn_remove.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    list.remove("");
    adapter.notifyDataSetChanged();
    gridView.setAdapter(adapter);

    editor = sharedPreferences.edit();
    editor.putInt("count", count - 1);
    editor.apply();

    Toast.makeText(getApplicationContext(), "One Image Removed", Toast.LENGTH_SHORT).show();
   }
  });
 }
}
ImageAdapter.java
Open ImageAdapter.java and replace it with the following code
package com.example.grid.adapter;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.example.grid.R;
 
import java.util.ArrayList;
 
public class ImageAdapter extends BaseAdapter {
  
 private Context context;
 private ArrayList list = new ArrayList<>();

 public ImageAdapter(Context context, ArrayList list) {
  this.context = context;
  this.list = list;
 }

 @Override
 public int getCount() {
  return list.size();
 }

 @Override
 public Object getItem(int position) {
  return null;
 }

 @Override
 public long getItemId(int position) {
  return 0;
 }

 public View getView(int position, View convertView, ViewGroup parent) {

  LayoutInflater inflater = (LayoutInflater) Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  convertView = inflater.inflate(R.layout.activity_main_single, null);
  TextView textView = (TextView) convertView.findViewById(R.id.grid_item_label);
  ImageView imageView = (ImageView) convertView.findViewById(R.id.grid_item_image);

  int option = ((position+1)%3);

  switch (option) {
   case 0:
    textView.setText("Windows");
    imageView.setImageResource(R.drawable.windows_logo);
    break;
   case 1:
    textView.setText("Android");
    imageView.setImageResource(R.drawable.android_logo);
    break;
   case 2:
    textView.setText("IOS");
    imageView.setImageResource(R.drawable.ios_logo);
    break;
   default:
    textView.setText("Android");
    imageView.setImageResource(R.drawable.android_logo);
    break;
  }

  return convertView;

 }

}

Download Full Source Code


Download Code

1 comment:

Please Comment about the Posts and Blog