mducc
mducc

Reputation: 743

Elevation is not working in ContraintLayout

I don't know why android:elevation is not working

My code:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".UI.Activity.ChatActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:elevation="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.07"
        app:layout_constraintStart_toStartOf="parent"
        android:gravity="center_vertical">

        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:inputType="text"
            android:layout_marginLeft="15dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="5dp"
            android:background="@drawable/border_edit_text_messenger"
            android:textCursorDrawable="@color/colorWhite"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"/>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.8"
            android:src="@drawable/ic_send"
            android:scaleType="centerInside"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"/>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Upvotes: 9

Views: 13092

Answers (3)

android_dev71
android_dev71

Reputation: 617

Hope that this could be useful, previous solution unfortunally don't work for my case: I found a similar problem with material card do not showing elevation nor shadow if inside a constraintLayout (or not); I've solved both by valorizing other than cardElevation the layout_margin property, and with a value close to cardElevation, and making the latter not too high to avoid clipping.

android:layout_margin="5dp"
app:cardElevation="8dp"

I've a gist for material card inside a ConstraintLayout here and without it here

Let me know if it works for you too.

Upvotes: 0

Khemraj Sharma
Khemraj Sharma

Reputation: 58934

This is default behaviour that shadow is identified by some background, so there will be NO shadow for NO background.

  • Either set a background to your View.
  • If your view does not have background. then you can set android:outlineProvider="bounds".

Suggestion:

Both above solutions will work for Android versions >21.

Solution for all versions

You can put drawable shadow on Views.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.07"
    app:layout_constraintStart_toStartOf="parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        >

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@drawable/shadow"/>

</FrameLayout>

Put showdow.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <gradient
        android:angle="-90"
        android:endColor="#33000000"
        android:startColor="@android:color/transparent"/>

</shape>

output

You can find many ways to add shadow here.

Upvotes: 10

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

Somehow elevation does not work until you set any background color to the ViewGroup or View in which you want to set elevation.

Set

android:background="@color/colorWhite"
android:elevation="2dp"

to LinearLayout in your above xml.

Upvotes: 29

Related Questions