• Register
  • Log in
  • Home
  • Contact

Android Create a GridView with BaseAdapter



Open the  resource file from res/layout/activity_main.xml MainActivity and creates a GridView to display the data

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.blogtest.MainActivity" >
     
     <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"      
        android:verticalSpacing="5dp"
        android:horizontalSpacing="5dp"
        android:stretchMode="columnWidth"
        android:isScrollContainer="false"
        android:layout_margin="10dp"
        android:gravity="center"
    />
 
</RelativeLayout>

 

MainActivity

public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        List<Country> countries = new ArrayList<Country>();
        Country newItem = new Country("USA",R.drawable.unitedstates);
        countries.add(newItem);
        newItem = new Country("CANADA",R.drawable.canada);
        countries.add(newItem);
        newItem = new Country("SWEDEN",R.drawable.sweden);
        countries.add(newItem);
        newItem = new Country("DENMARK",R.drawable.denmark);
        countries.add(newItem);
         
        GridView gridView = (GridView)this.findViewById(R.id.gridview);
        countryAdapter adapter = new countryAdapter(this,countries);
        gridView.setAdapter(adapter);
     
    }

Country Class

public class Country{
        public Country(String name,int flag){
            Name = name;
            Flag = flag;
        }
        public String Name;
        public int Flag;       
 }

Custom Adapter for the GridView source (BaseAdapter subclass)

public class countryAdapter extends BaseAdapter {
        private final LayoutInflater mInflater;
        Context context;       
        Activity currentActivity;
        List<Country> items;
        public countryAdapter(MainActivity _context,List<Country> _items) {
                 
            currentActivity = _context;  
            this.context = _context;
            this.items = _items;
            this.mInflater = (LayoutInflater) currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
        }
 
        @Override
        public int getCount() {
            return items.size();
        }
 
        @Override
        public Country getItem(int position) {
            return items.get(position);
        }
 
        @Override
        public View getView(final int position, View convertView, final ViewGroup parent)
        {
            if (convertView == null) {             
                convertView = mInflater.inflate(R.layout.gridchild, parent, false);
                Button btn =  (Button)convertView.findViewById(R.id.btnCountry);
                btn.setOnClickListener(new Button.OnClickListener(){
                 public void onClick(View v) {
                     String name = ((Button)v).getTag().toString();
                     if(name != ""){
                        Toast.makeText(context, name, Toast.LENGTH_LONG).show();
                     }
                 }
                });
            }
            Country country = items.get(position);    
                 
            Button btn = (Button)convertView.findViewById(R.id.btnCountry);
            btn.setText(country.Name);
            btn.setTag(country.Name);
             
            ImageView image = (ImageView)convertView.findViewById(R.id.imgCountry);
            image.setImageResource(country.Flag);
             
            return convertView;
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }
 
        @Override
        public boolean isEnabled(int position) {
            return true;
        }
 
    }

 

Create a new layout file for GridView customise view 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@android:color/white">
     
    <ImageView android:id="@+id/imgCountry"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"
        />
    <Button android:id="@+id/btnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="24sp"
        android:gravity="center"
        android:text="Country Name"
        />
 
</LinearLayout>

© 2021 - KodeCenter beta