0

I have a TextView inside my CardView layout but it's not being displayed on neither the Layout preview nor the live device. Similar questions had answers like "remove tools:android:text=".." " etc. but that's not the issue here. Any ideas?

This is what i'm going for with the cardView:

final image

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="16dp"
    android:orientation="horizontal"
    android:weightSum="100"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/explore_desc">

        <androidx.cardview.widget.CardView
            android:id="@+id/localEvents"
            android:layout_width="150dp"
            android:layout_height="75dp"
            android:layout_marginEnd="10dp"
            android:layout_weight="50"
            app:cardCornerRadius="12dp">

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


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:fontFamily="@font/montserrat_bold"
                    android:text="@string/local_events"
                    android:textColor="@android:color/white"
                    android:textSize="14sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


            </androidx.constraintlayout.widget.ConstraintLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/beach_bg_placeholder" />

        </androidx.cardview.widget.CardView>
</LinearLayout>
Stelios Papamichail
  • 955
  • 2
  • 19
  • 57

2 Answers2

2

I tried your code & the following works perfectly. Since the ImageView and TextView were stacked up inside LinearLayout, the text was not showing on top of ImageView. Defining the TextView after ImageView puts it 'over' the ImageView like this:

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

    <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:orientation="horizontal"
            android:weightSum="100"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

        <androidx.cardview.widget.CardView
                android:id="@+id/localEvents"
                android:layout_width="150dp"
                android:layout_height="75dp"
                android:layout_marginEnd="10dp"
                android:layout_weight="50"
                app:cardCornerRadius="12dp">

            <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@drawable/test" />



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


                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginBottom="8dp"
                        android:text="Local Events"
                        android:textColor="@android:color/white"
                        android:textSize="14sp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        android:elevation="@dimen/_10sdp"
                />


            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>
    </LinearLayout>
</LinearLayout>

Screenshot:

enter image description here

SaadAAkash
  • 3,065
  • 3
  • 19
  • 25
  • I've tried changing the colors too but that doesn't fix it – Stelios Papamichail Aug 06 '19 at 13:46
  • Copy the `TextView` I posted in the answer, add `android:fontFamily="@font/montserrat_bold"` and `android:text="@string/local_events"` from your code and it should work. – SaadAAkash Aug 06 '19 at 13:49
  • I've already done that, it's literally the same `TextView` i posted with a different color but the color isn't the issue here. I've tried multiple colors and different background images but that's not it – Stelios Papamichail Aug 06 '19 at 13:51
  • @SteliosPapamichail, please add `android:elevation="@dimen/1dp"` to the `TextView`. – SaadAAkash Aug 06 '19 at 13:57
  • @SteliosPapamichail I've updated the code with the screenshot & reason why it didn't work because of text & image views being stacked up one upon another. – SaadAAkash Aug 06 '19 at 14:04
  • You're right, i should have thought of that. Awesome answer, thank you very much. I'll keep it in my mind from now on – Stelios Papamichail Aug 07 '19 at 09:25
0

You should be written like below

<?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:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">


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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="TextV'ew"
                android:textColor="@color/colorPrimary"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:layout_marginBottom="16dp"
            android:background="@drawable/america"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.cardview.widget.CardView>
Firat
  • 43
  • 8