How to Create Custom GridView in Android Studio

1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:app="http://schemas.android.com/apk/res-auto"    
xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
tools:context=".MainActivity">

    <GridView        
android:id="@+id/grid"        
android:columnWidth="20dp"        
android:numColumns="3"        
android:stretchMode="columnWidth"        
android:layout_width="409dp"        
android:layout_height="729dp"        
android:layout_marginStart="1dp"        
android:layout_marginTop="1dp"        
android:layout_marginEnd="1dp"        
android:layout_marginBottom="1dp"        
app:layout_constraintBottom_toBottomOf="parent"        
app:layout_constraintEnd_toEndOf="parent"        
app:layout_constraintStart_toStartOf="parent"       
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

2. grid_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"    
android:orientation="vertical"    
android:layout_height="match_parent"    
android:gravity="center_horizontal">

    <ImageView        
android:id="@+id/iv"        
android:layout_width="50dp"        
android:layout_height="50dp"/>

    <TextView        
android:id="@+id/tv"       
android:layout_width="wrap_content"        
android:layout_height="wrap_content"/>

</LinearLayout>

3. MainActivity.class

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.GridView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<Item> gridArray = new ArrayList<>();    
CustomGridViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_main);
        Bitmap logoIcon = BitmapFactory.decodeResource(this.getResources(), 
R.drawable.ic_launcher);        
gridArray.add(new Item(logoIcon, "HOME"));        
gridArray.add(new Item(logoIcon, "PROFILE"));        
gridArray.add(new Item(logoIcon, "MAIN"));        
gridArray.add(new Item(logoIcon, "LANDING"));        
gridArray.add(new Item(logoIcon, "CATEGORY"));        
gridArray.add(new Item(logoIcon, "CONTACT"));

        GridView grid = (GridView)findViewById(R.id.grid);        
adapter = new CustomGridViewAdapter(this, R.layout.grid_list, gridArray);        
grid.setAdapter(adapter);
    }
}

4. Item.class

package com.example.myapplication;
import android.graphics.Bitmap;
public class Item {
    Bitmap image;    String title;
    public Item(Bitmap image, String title){
        super();        
this.image = image;        
this.title = title;    }

    public Bitmap getImage(){
        return image;    }

    public void setImage(Bitmap image){
        this.image = image;    }

    public String getTitle(){
        return title;    }

    public void setTitle(String title){
        this.title = title;    }
}

5. CustomGridViewAdapter.class

package com.example.myapplication;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
    Context context;    int layoutResourceId;    
ArrayList<Item> data = new ArrayList<>();
    public CustomGridViewAdapter(Context context, 
int LayoutResourceId, ArrayList<Item>data){
        super(context, LayoutResourceId, data);        
this.context = context;        
this.layoutResourceId = LayoutResourceId;        
this.data = data;    }

    @NonNull    
@Override    
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View row = convertView;        
Holder holder = null;
        if(row==null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();            
row = inflater.inflate(layoutResourceId, parent, false);
            holder = new Holder();            
holder.iv = (ImageView)row.findViewById(R.id.iv);            
holder.tv = (TextView)row.findViewById(R.id.tv);            
row.setTag(holder);        }
else{
            holder = (Holder)row.getTag();        }

        Item item = data.get(position);        
holder.tv.setText(item.getTitle());        
holder.iv.setImageBitmap(item.getImage());
        return row;    }

    static class Holder{
        TextView tv;        
ImageView iv;    }
}


THE END

No comments:

Post a Comment

Welcome To My Android Tutorial Lesson. Now this blogger cover : Android Studio Tutorial 1.  How To Start / Create a new project...