what's the simplest way to implement an item grid of two columns in android studio

423 Views Asked by At

some guides say recyclerview. some say gridview. some say recyclerview with grid adapter...

i have no idea what these methods have in differences as i'm still new to android development. i've tried them all but somehow always ended up messing things up. can someone explain a simple way please? (very preferably in kotlin) i know how to make an item but mostly struggle with the adapter (specially considering most guides for it are written in java and i can barely even read Kotlin). i've been trying to do it for a week so i would be really grateful.

2

There are 2 best solutions below

0
avalerio On

You will want to use a regular RecyclerView and any RecyclerView.Adapter will work. The key is the LayoutManager you give to the RecyclerView. If you give it a GridLayoutManager it will ask how many items per row, its actually fairly simple.

Here is a modified excerpt from the article below.

RecyclerView recyclerView = findViewById(R.id.recyclerView);
// set a GridLayoutManager with 2 number of columns
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);
gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // set Horizontal Orientation
recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView

Here is a pretty in-depth article showing you how to do it. The article also has a simple implementation of an Adapter as-well. https://abhiandroid.com/materialdesign/recyclerview-gridview.html

A fair warning though the article uses some outdated libraries you should be using androidx for nearly everything. Luckily the implementation is not different.

0
Kevin Cloud On

I think, if your list is just simple (no big images, small size of items, also no need any complicated action with onClickListener), then you can use gridview. But in case the list is complicated, then you should use RecyclerView. In fact, for newest Android applications, we barely ListView or GridView - they are just for simple, very simple lists. If you are going to go far with Android, then I strongly recommend you to use RecyclerView.