Search code examples
androidkotlinandroid-linearlayout

why orientation of views change after setting elevation


after I set elevation for my LinearLayout my imageView which should on my LinearLayout goes behind it, what is the problem? here are my codes:

 <RelativeLayout
    android:layout_marginTop="100dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:elevation="10dp"
        android:layout_marginBottom="100dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:background="@drawable/login_cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

    <ImageView
        android:layout_centerHorizontal="true"
        android:src="@mipmap/login_iut_logo"
        android:layout_width="200dp"
        android:layout_height="200dp"/>


</RelativeLayout>

I want my ImageView to be on LinearLayout. it works well till not to set elevation for LinearLayout. I set elevation because I wanted its shadow.

I want to have something like this: enter image description here

but after I set elevation for LinearLayout it turns to something like this: enter image description here


Solution

  • If you want your image to be inside the linear layout you should nest it like this:

    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginTop="100dp"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent">
    
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="100dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="100dp"
        android:elevation="10dp" />
    
    
      <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:elevation="10dp" />
    
    </RelativeLayout>
    

    This should do the trick! (without changing much of your code)