Search code examples
androidandroid-layoutuser-interfaceandroid-framelayout

which layout should i use for making a table type layout in android?


I want to make layout like this:

enter image description here

Can anyone guide me which layout is more suitable?(I have fix number of rows and columns).

Relative layout or linear layout? or something else?


Solution

  • Use the below code for the design you want

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
     android:layout_width="match_parent"
    android:layout_height="match_parent">
     <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="100dp">
    
        <RelativeLayout
            android:background="#8FDFBF"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginEnd="1dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/card_title1"
                android:text="title1"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:layout_below="@+id/card_title1"
                android:layout_marginTop="20dp"
                android:text="12"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_weight="1"
            android:background="#8FDFBF"
            android:gravity="center"
            android:layout_marginStart="1dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/card_title2"
                android:text="title2"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:layout_below="@+id/card_title2"
                android:layout_marginTop="20dp"
                android:text="21"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </RelativeLayout>
    
    </LinearLayout>
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/margin_2"
        android:layout_width="match_parent"
        android:layout_height="100dp">
    
        <RelativeLayout
            android:background="#4ADCA1"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginEnd="1dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/card_title3"
                android:text="title3"
                android:gravity="center"
                android:textColor="@android:color/white"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:layout_below="@+id/card_title3"
                android:layout_marginTop="20dp"
                android:text="25 c"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_weight="1"
            android:background="#4ADCA1"
            android:gravity="center"
            android:layout_marginStart="1dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/card_title4"
                android:text="title4"
                android:gravity="center"
                android:textColor="@android:color/white"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:layout_below="@+id/card_title4"
                android:layout_marginTop="20dp"
                android:text="40 mb"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </RelativeLayout>
    
    </LinearLayout>
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/margin_2"
        android:layout_width="match_parent"
        android:layout_height="100dp">
    
        <RelativeLayout
            android:background="#9EFAD5"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginEnd="1dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/card_title5"
                android:text="title5"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:layout_below="@+id/card_title5"
                android:layout_marginTop="20dp"
                android:text="50 %"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_weight="1"
            android:background="#9EFAD5"
            android:gravity="center"
            android:layout_marginStart="1dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/card_title6"
                android:text="title6"
                android:gravity="center"
                android:textColor="@android:color/white"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:layout_below="@+id/card_title6"
                android:layout_marginTop="20dp"
                android:text="90 %"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </RelativeLayout>
    
    </LinearLayout>