skip to Main Content

I have a RelativeLayout container with a blue background color. Inside of it I have a TextView element, but for some reason the background is rendered on top of my text and not behind it.

Here’s my code:

<RelativeLayout
    android:id="@+id/my_widget"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:background="@drawable/layout_bg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="🥳"
            android:textSize="80dp"
        />

    </LinearLayout>
</RelativeLayout>

My Bg

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00aaff"/>
    <corners android:radius="15dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

Result:

enter image description here
What could be the issue here?
Thanks!

3

Answers


  1. Option 1: Use FrameLayout instead of Relative layout.

    <FrameLayout
    android:id="@+id/my_widget"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:background="@drawable/layout_bg">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="🥳"
            android:textSize="80dp"
        />
    
    </LinearLayout>
    </FrameLayout>
    

    Option 2: Try Elevation in your child layout.

    <RelativeLayout
    android:id="@+id/my_widget"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:background="@drawable/layout_bg">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
         android:elevation="10dp" //here (remove this command)
        android:gravity="center_horizontal">
    
    Login or Signup to reply.
  2. Set any color to TextView like:

    <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="🥳"
                android:textColor="#fff"
                android:textSize="80dp"
                />
    
    Login or Signup to reply.
  3. This is due to the default text foreground color of Android .

    Set any TextColor to the textview.

    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="🥳"
                android:textColor="@color/black"
                android:textSize="80sp"
                />
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search