Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elevation is not working in ContraintLayout

Tags:

android

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>
like image 636
mducc Avatar asked Nov 01 '18 12:11

mducc


2 Answers

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.

like image 172
Pankaj Kumar Avatar answered Oct 29 '22 08:10

Pankaj Kumar


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.

like image 21
Khemraj Sharma Avatar answered Oct 29 '22 09:10

Khemraj Sharma