Aaron Waller
Aaron Waller

Reputation: 315

How can I programm a layout like this?

I want to create a layout which looks like this: https://gyazo.com/2a08bcd731fb1cfeb07e72f75bc05e7e What should I use for this, that it fits to every device?

The screen is mine, but the problem is it does not fit to every screen size. On some devices it looks like this: https://gyazo.com/84d878061234c25ae872d4ed2491f2f4

Here is the code I used to pragramm this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/button1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Button"/>

            <Button
                android:id="@+id/Button2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_column="1"
                android:layout_row="0"
                android:text="Button"/>

            <Button
                android:id="@+id/Button3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_column="0"
                android:layout_row="1"
                android:text="Button"/>

            <Button
                android:id="@+id/Button4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_column="1"
                android:layout_row="1"
                android:text="Button"/>

              //and 200 buttons more

    </GridLayout>

</ScrollView>

How can I programm a layout like this which fits on every screen size? Important is the ScrollView

Upvotes: 0

Views: 71

Answers (1)

Aimy_N_Chu
Aimy_N_Chu

Reputation: 126

The first thought that came to mind is to use RecyclerView with a GridLayoutManager

GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2, LinearLayoutManager.VERTICAL, false);
yourRecyclerView.setLayoutManager(gridLayoutManager);

This will give you a list going down vertically with 2 columns. You probably want to check out CardView as well if you don't mind, but sometimes I like to use them together.

Once you've written the adapter for your RecyclerView you can set the GridLayoutManager and YourAdapter in your activity and that's pretty much the only thing you'll actually need in your activity: initialize the adapter, set the layout manager and adapter of your recyclerview, and done. You need to write the adapter yourself, but there's a lot of tutorials that you can Google.

Here is a link that I think I've followed before when I started learning about RecyclerView. It was sometimes ago so I'm not sure if it is still correct, but you can check it out as a start.

Upvotes: 1

Related Questions