'how in GridLayout (Android) to fit the last two blocks to full width?

I am currently learning the Android front-end development, and our teacher gave us this task:

We need to create the next grid in Android Studio: Task Grid using GridLayout. For now I only created this.

Is there way to display the last two blocks of the GridLayout in full width?



Solution 1:[1]

Have a look at SpanSizeLookup.

Something like:

val layoutManager = GridLayoutManager(activity, 2)
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
    override fun getSpanSize(position: Int): Int {
        return when (position) {
            YOUR_CONDITION -> 2
            else -> 1
        }
   }
}
recyclerView.layoutManager = layoutManager

Replace YOUR_CONDITION with some logic fits your requirements to make a cell twice higher.

Solution 2:[2]

I am not sure if it works, but this post does not recommend it.

You could just put a LinearLayout with android:orientation="vertical" and android:width="match_parent" or another GridLayout below your GridLayout.

If you don't have any wrapping layout, wrap both of them into a LinearLayout with android:orientation="vertical" and put all the properties of your original GridLayout there.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vasily Kabunov
Solution 2 Cactusroot