GridLayoutManager – how to auto fit columns?

You can calculate available number of columns, given a desired column width, and load the image as calculated. Define a static funtion to calculate as: public class Utility { public static int calculateNoOfColumns(Context context, float columnWidthDp) { // For example columnWidthdp=180 DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); float screenWidthDp = displayMetrics.widthPixels / displayMetrics.density; int noOfColumns = (int) … Read more

Set layout_column and layout_row in GridLayout programmatically

The equivalent of layout_column and layout_row, as with all layout_… parameters, is to be found as a parameter of a subclass of LayoutParams. In this case it’s GridLayout.LayoutParams, and we use it like this (for a 2×2 grid with a subview in the final row and column, centred within the cell): gridLayout.setColumnCount(2); gridLayout.setRowCount(2); gridLayout.addView(subview, new … Read more

gridView with different cells sizes, pinterest style

Is it a bit late to answer? check this library from etsy. it looks very promising. https://github.com/etsy/AndroidStaggeredGrid I think it is newer version of https://github.com/maurycyw/StaggeredGridView . Updated. Try using RecyclerView StaggeredGridLayoutManager from google instead RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(adapter);

RecyclerView GridLayoutManager: how to auto-detect span count?

Personaly I don’t like to subclass RecyclerView for this, because for me it seems that there is GridLayoutManager’s responsibility to detect span count. So after some android source code digging for RecyclerView and GridLayoutManager I wrote my own class extended GridLayoutManager that do the job: public class GridAutofitLayoutManager extends GridLayoutManager { private int columnWidth; private … Read more

Android GridLayout with dynamic number of columns per row

You can use FlexboxLayoutManager Add the following dependency to your build.gradle file: implementation ‘com.google.android:flexbox:1.0.0’ SAMPLE CODE LAYOUT.XML <LinearLayout 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:orientation=”vertical” tools:context=”.MainActivity”> <android.support.v7.widget.RecyclerView android:id=”@+id/recyclerView” android:layout_width=”match_parent” android:layout_height=”250dp” /> </LinearLayout> ACTIVTY CODE import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; import com.google.android.flexbox.FlexDirection; import com.google.android.flexbox.FlexboxLayoutManager; import com.google.android.flexbox.JustifyContent; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; ArrayList<String> … Read more

GridLayout (not GridView) how to stretch all children evenly

Starting in API 21 the notion of weight was added to GridLayout. To support older android devices, you can use the GridLayout from the v7 support library. The following XML gives an example of how you can use weights to fill the screen width. <?xml version=”1.0″ encoding=”utf-8″?> <android.support.v7.widget.GridLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:grid=”http://schemas.android.com/apk/res-auto” android:id=”@+id/choice_grid” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:layout_centerHorizontal=”true” android:padding=”4dp” … Read more