0

Could you guide me how to put 4 colors in linearlayout instead of create 4 views of linearlayout because I realized there is better way but I don't know how to apply it.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

The result should be like image below

Image

Taha Sami
  • 1,565
  • 1
  • 16
  • 43
  • With respect to memory and resource consumption constraint layout is better than linear layout. you can go with the constraint layout as in answers – AgentP Jan 01 '20 at 06:49

3 Answers3

1

Better to use ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
            android:id="@+id/imgOne"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ff00"
            app:layout_constraintBottom_toTopOf="@+id/imgThree"
            app:layout_constraintEnd_toStartOf="@id/imgTwo"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    <ImageView
            android:id="@+id/imgTwo"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#2196F3"
            app:layout_constraintBottom_toTopOf="@+id/imgFour"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/imgOne"
            app:layout_constraintTop_toTopOf="parent" />


    <ImageView
            android:id="@+id/imgThree"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FFC107"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/imgTwo"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imgOne" />


    <ImageView
            android:id="@+id/imgFour"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF5722"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/imgThree"
            app:layout_constraintTop_toBottomOf="@+id/imgTwo" />


</androidx.constraintlayout.widget.ConstraintLayout>

OUTPUT

enter image description here

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

Use Constraint Layout like this

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/dark_blue"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/clThird"
        app:layout_constraintEnd_toStartOf="@id/clSecond"
        app:layout_constraintStart_toStartOf="parent"
        android:id="@+id/clFirst">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/light_gray"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/clFourth"
        app:layout_constraintStart_toEndOf="@id/clFirst"
        android:id="@+id/clSecond">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/clFourth"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/clFirst"
        android:id="@+id/clThird">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/blue_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/clSecond"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/clThird"
        android:id="@+id/clFourth">

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Sejpal Pavan
  • 130
  • 10
1

You can use Linear Layout for vertical and Horizontal orientation with weight 1 like below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/grey1" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/grey2" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/grey3" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/grey4" />
    </LinearLayout>
</LinearLayout>

enter image description here

Arti
  • 672
  • 7
  • 19