Search code examples
androidandroid-recyclerviewlayoutandroid-relativelayoutandroid-cardview

layout_below doesn't place text underneath


Hello, I tried to copy the Code of a tutorial, but it doesn't work out for me... The Layout_below won't move the text to underneath the picture, can somebody help?

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="20dp"
    android:id="@+id/parent"
    app:cardCornerRadius="7dp"
    app:cardElevation="7dp"
    android:layout_margin="5dp">

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

    <ImageView
        android:layout_width="130dp"
        android:layout_height="150dp"
        android:id="@+id/imgFood"
        android:src="@mipmap/ic_launcher"
        android:contentDescription="@string/todo" />

    <TextView
        android:id="@+id/txtMealName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imgFood"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:text="@string/meal_name"
        android:textSize="16sp"
        android:textStyle="bold" />


</com.google.android.material.card.MaterialCardView>

Solution

  • your text view and image view are not inside RelativeLayout
    replace below code with your code

    <?xml version="1.0" encoding="utf-8"?>
    <com.google.android.material.card.MaterialCardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:padding="20dp"
        android:id="@+id/parent"
        app:cardCornerRadius="7dp"
        app:cardElevation="7dp"
        android:layout_margin="5dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="130dp"
                android:layout_height="150dp"
                android:id="@+id/imgFood"
                android:src="@mipmap/ic_launcher"
                android:contentDescription="some text" />
    
            <TextView
                android:id="@+id/txtMealName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imgFood"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="7dp"
                android:text="cheese pizza "
                android:textSize="16sp"
                android:textStyle="bold" />
    
        </RelativeLayout>
    
    </com.google.android.material.card.MaterialCardView>
    

    enter image description here