Articles → Android → GridView control in android

GridView control in android






What is GridView control?





Example






package com.example.demo_application;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GridAdapter extends BaseAdapter 
{    
    private Context mContext;    
    String[] strArray = new String[] {"Name 1", "Name 2", "Name 3", "Name 4"};    
    
    public GridAdapter(Context c)    
    {        
        mContext = c;    
    }    
    
    @Override    
    public int getCount() 
    {        
        return strArray.length;    
    }    
    
    @Override    
    public Object getItem(int i) 
    {        
        return null;    
    }    
    
    @Override    
    public long getItemId(int i) 
    {        
        return 0;    
    }    
    
    @Override    
    public View getView(int i, View view, ViewGroup viewGroup) 
    {        
        TextView textView;        
        if(view == null)        
        {            
            textView = new TextView(mContext);        
        }        
        else        
        {            
            textView = (TextView) view;        
        }        textView.setText(strArray[i]);        return textView;    }}




GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new GridAdapter(this));





Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, November 07, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250