Articles → Android → Gridview Control In Android

Gridview Control In Android






What Is Gridview Control?





Example




  1. Create an adapter for gridview.
  2. 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;
      }
    }


  3. Bind grid using adapter
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new GridAdapter(this));




Picture showing the output of GridView control in android
Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250