-2

How to add this Layout to my CardView ?

I want to have a TextView, a divider and another TextView like in the "after" picture.

Before:

enter image description here

After:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/post_year"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="1" />

    <TextView
        android:id="@+id/post_title"
        style="@style/TextAppearance.AppCompat.Medium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textStyle="bold"
        tools:text="My First Post" />

</LinearLayout>

Thanks!!

rafsanahmad007
  • 23,062
  • 6
  • 43
  • 58
jglom
  • 23
  • 1
  • 14

3 Answers3

2

@Ajay Shrestha solution works perfectly, just adding another possible solution. If you do not want to fix the height of cardview as there can be some text in multiple lines, then follow below code. Otherwise, @Ajay Shrestha solution is much simpler. Just remove the padding of the Textviews and fix the height of CardView

 <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentPadding="10dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/post_year"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="1" 
        android:paddingTop="10dp"
        android:paddingBottom="10dp"/>

    <View
        android:id="@+id/line"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="@color/colorLightGray"
        android:layout_alignTop="@id/post_year"
        android:layout_alignBottom="@+id/post_title"
        android:layout_toRightOf="@id/post_year"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"/>

    <TextView
        android:id="@+id/post_title"
        style="@style/TextAppearance.AppCompat.Medium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textStyle="bold"
        tools:text="My First Post"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_toRightOf="@id/line"/>

</RelativeLayout>
</android.support.v7.widget.CardView>
Malwinder Singh
  • 5,696
  • 11
  • 49
  • 88
1

Do Like this

 <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        card_view:cardCornerRadius="4dp">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:text="4" />

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_margin="15dp"
                android:background="@color/black" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:text="Okay I am here" />

        </LinearLayout>

    </android.support.v7.widget.CardView>
Ajay Shrestha
  • 2,283
  • 1
  • 16
  • 25
1

Change android:orientation to horizontal. For the divider, the answer is in this: https://stackoverflow.com/a/2659038/433804

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#FF0000FF" />
Community
  • 1
  • 1
Karl Jamoralin
  • 1,041
  • 1
  • 15
  • 25