Android - TextView don't show below another TextView

1985 views android
0

I've got some TextView in a CardView, but when i try to set my TextView "text_lieu" below the TextView "text_nom", nothing happened they are confused. Can you help me please ? <3

Here my xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="10dp"
android:orientation="vertical"
card_view:cardElevation="10dp"
android:layout_margin="10dp">

    <TextView
        android:id="@+id/text_nom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text
        android:layout_gravity="start"
        android:padding="20dp" />
    <TextView
        android:id="@+id/text_heure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:padding="20dp" />
    <TextView
        android:id="@+id/text_lieu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_below="@+id/text_nom"/>

</android.support.v7.widget.CardView>

answered question

Wrap them in a LinearView with a orientation of vertical.

I've did that but my TextView "text_heure" go to the midline but i want it at the end of the first line. Like "text_nom" at the start of the first line, "text_heure" at the end of the first line and "text_lieu" at the start of the second and end line.

3 Answers

9

A CardView extends a FrameLayout basically. That means that views are placed on top of each other, not in a linear fashion like in a LinearLayout. To do this you want to put your TextViews in a LinearLayout inside the CardView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="10dp"
android:orientation="vertical"
card_view:cardElevation="10dp"
android:layout_margin="10dp">

<LinearLayout
   android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/text_nom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:layout_gravity="start"
    android:padding="20dp" />
<TextView
    android:id="@+id/text_heure"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:padding="20dp" />
<TextView
    android:id="@+id/text_lieu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:layout_below="@+id/text_nom"/>
   </LinearLayout>
</android.support.v7.widget.CardView>

Note the orientation vertical in the linear layout.

If you want to keep the FrameLayout style, an item in it has the property android:layout_gravity which you can set to bottom to place an item in the bottom.

posted this
1

android:layout_below is only effective in a RelativeLayout.
You can put your views inside a RelativeLayout like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="10dp">

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

        <TextView
            android:id="@+id/text_nom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:padding="20dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/text_heure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:padding="20dp" />

        <TextView
            android:id="@+id/text_lieu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_nom"
            android:padding="20dp" />
    </RelativeLayout>

</android.support.v7.widget.CardView>

posted this
5

Wrap the textviews in a LinearLayout. You can adjust them they are are, you dont need to do the layout_below. LinearLayout will take care of that for you.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="10dp"
android:orientation="vertical"
card_view:cardElevation="10dp"
android:layout_margin="10dp">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/text_nom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:padding="20dp" />
    <TextView
        android:id="@+id/text_heure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp" />
    <TextView
        android:id="@+id/text_lieu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>

posted this

Have an answer?

JD

Please login first before posting an answer.