-1

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: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"/>

</android.support.v7.widget.CardView>
  • Wrap them in a LinearView with a orientation of vertical. – letsCode Nov 07 '18 at 18:53
  • 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. –  Nov 07 '18 at 18:57
  • look at my answer.... with a linear layout, you put the views where you want them. For example, if you want view 1 to be at the bottom... you would view V2, V3, V1 in that order within the linear layout. again, look at my answer... – letsCode Nov 07 '18 at 19:01
  • Oh i see, it works nice, thanks for your fast answer. Did you know how i can do to make "Text_lieu" invisible if there is no text inside ? Because when there's no text, the cardview is too big –  Nov 07 '18 at 19:06

3 Answers3

2

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.

P Fuster
  • 2,224
  • 1
  • 20
  • 30
0

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>

Edit:

if (myTextView3.getText().toString().trim().isEmpty())
    myTextView3.setVisibility(View.GONE);
else
    myTextView3.setVisibility(View.VISIBLE);
forpas
  • 160,666
  • 10
  • 38
  • 76
  • It works thank ! I've got a last question, how can i make "Text_lieu" invisible if there is no text because now my cardview is too big when there is no "text_lieu" text –  Nov 07 '18 at 19:00
  • By code you can do: `text_lieu.setVisibility(View.GONE);` – forpas Nov 07 '18 at 19:04
  • What need i place in my if() ? It's myTextView3 http://image.noelshack.com/fichiers/2018/45/3/1541617842-capture.png –  Nov 07 '18 at 19:11
  • I guess it's `myTextView3`. – forpas Nov 07 '18 at 19:13
  • Yup but i don't know what condition to place for the setinvisibility –  Nov 07 '18 at 19:52
  • The condition is `myTextView3.getText().toString().trim().isEmpty()`, this means that there is no text inside `myTextView3`. See the edit in my answer. – forpas Nov 07 '18 at 19:54
  • Sorry i do wrong, i reaccept. Thanks for all you do, the condition works good ! Have a nice day =) –  Nov 07 '18 at 20:24
0

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_lieu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"/>

    <TextView
        android:id="@+id/text_heure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
letsCode
  • 2,774
  • 1
  • 13
  • 37