-5

I'm working on a layout and I want to make the similar layout like the one in the picture below. I don't know what should I use to make this layout ? I've worked with Listview and Recycler View but I don't know if I can use them to make a layout like this?

Does anyone have a tutorial on how can I make a similar layout?

Layout

majid ghafouri
  • 787
  • 2
  • 7
  • 23
Umar Saleem
  • 109
  • 10
  • Use FlexBOX https://github.com/google/flexbox-layout – Ruben Meiring Oct 16 '19 at 07:29
  • 3
    Recycler view is the way to go. Create a ConstraintLayout to include your textviews and dividers for a single item and inflate that through the recyclerview adapter and you're good to go. – Nikos Hidalgo Oct 16 '19 at 07:30
  • Try this : https://www.raywenderlich.com/9475-constraintlayout-tutorial-for-android-complex-layouts, After this you learn basic of ConstraintLayout. – Rahul Khatri Oct 16 '19 at 07:39

2 Answers2

0

The best way is to use Recycler View and add divider between all items: look at this How to add dividers and spaces between items in RecyclerView?

And use this item layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="8dp"
        app:cardElevation="8dp">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/message_content_layout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/message_end_green_view"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:padding="8dp">

            <TextView
                    android:id="@+id/tv_received_message_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Received message:"
                    android:textColor="@color/green"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_marginStart="4dp"/>

            <TextView
                    android:id="@+id/tv_received_message_value"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/tv_received_message_key"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="Message" />

            <View
                    android:id="@+id/tv_message_divider"
                    android:layout_width="0dp"
                    android:layout_height="2dp"
                    android:background="@color/grey_light"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tv_received_message_key"
                    android:layout_marginTop="8dp"/>

            <TextView
                    android:id="@+id/tv_reply_message_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Repley message:"
                    android:layout_marginTop="8dp"
                    android:textColor="#4caf50"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/tv_message_divider"
                    android:layout_marginStart="4dp"/>

            <TextView
                    android:id="@+id/tv_reply_message_value"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/tv_reply_message_key"
                    app:layout_constraintTop_toTopOf="@id/tv_reply_message_key"
                    app:layout_constraintBottom_toBottomOf="@id/tv_reply_message_key"
                    tools:text="Message" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <View
                android:id="@+id/message_end_green_view"
                android:layout_width="8dp"
                android:layout_height="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:background="@drawable/shape_rectangle_green"/>

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

in which shape_rectangle_green is this drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

    <solid android:color="#4caf50" />

    <corners
            android:bottomLeftRadius="0dp"
            android:bottomRightRadius="8dp"
            android:topLeftRadius="0dp"
            android:topRightRadius="8dp" />
</shape>

I let you change the margins, padding and colors.

SebastienRieu
  • 1,443
  • 2
  • 10
  • 20
0

Use below layout as item of your RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    app:cardCornerRadius="15dp"
    app:cardMaxElevation="3dp"
    android:elevation="3dp"
    app:cardUseCompatPadding="true"
    app:cardBackgroundColor="@android:color/white">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_weight=".95"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="15dp">
                <TextView
                    android:id="@+id/tv_received_message_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Received Message:"/>
                <TextView
                    android:id="@+id/tv_received_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="First Received Message"
                    android:layout_marginStart="5dp"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/colorPrimary"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="15dp">
                <TextView
                    android:id="@+id/tv_reply_message_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Reply Message:"/>
                <TextView
                    android:id="@+id/tv_reply_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="First Reply Message"
                    android:layout_marginStart="5dp"/>
            </LinearLayout>
        </LinearLayout>
        <View
            android:layout_weight=".05"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"/>
    </LinearLayout>
</androidx.cardview.widget.CardView>

Add your color in background attribute of View

The Output of above code is:

enter image description here

I hope it works for you.

Android Geek
  • 8,956
  • 2
  • 21
  • 35