How can I show elevations in a view with shadows using ConstraintLayout
?
With Relative
and Linear
could perform elevations with shadows to implement list but I can not do it with ConstraintLayout
.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:id="@+id/list_ssid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:elevation="8dp"
android:background="#fff"
android:text="SSID"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/list_ch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:elevation="8dp"
android:background="#fff"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="CH"
app:layout_constraintLeft_toLeftOf="@+id/guideline"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/list_dB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:elevation="8dp"
android:background="#fff"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="dB"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.65"
tools:layout_editor_absoluteX="239dp"
tools:layout_editor_absoluteY="0dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.83"
tools:layout_editor_absoluteX="306dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>
To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View. setElevation() method. To set the translation of a view, use the View.
In order to make elevation work, you need to just set a background color to a view. You also may use hex color or color attribute. Just note that it mustn't be fully transparent.
Shadows and Outlines The bounds of a view's background drawable determine the default shape of its shadow. Outlines represent the outer shape of a graphics object and define the ripple area for touch feedback. Then this view and drawable cast the appropriate shadow.
In order to make elevation work, you need to just set a background color to a view.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/your_color"
android:text="SSID"/>
Doesn't matter what kind of View it is,
a child of ConstraintLayout or ConstraintLayout itself
You also may use hex color or color attribute.
Just note that it mustn't be fully transparent.
For some reason, elevation works in ConstraintLayout
if you give a dummy drawable as the background::
Create a drawable:
dummyBg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
Use this as the background for the view and use elevation as you normally would.
android:elevation="8dp"
android:background="@drawable/dummyBg"
android:padding="4dp"
So you'd get:
<TextView
android:id="@+id/list_ssid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="2dp"
android:elevation="8dp"
android:background="@drawable/dummyBg"
android:padding="4dp"
android:text="SSID"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With