0

I have a question. Why android:elevation doesn't show shadows? I am making shadows first time

Only used in ImageButton it's working (when I use with android:background it isn't working)

my_image

This is my code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


<Button
        android:text="TestBTT"
        android:layout_width="295dp"
        android:layout_height="41dp"
        android:padding="10dp"
        android:elevation="30dp"
        android:outlineProvider="bounds"
        android:background="@drawable/przycisk"
        android:id="@+id/button2" android:layout_marginTop="60dp"
        app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="72dp"/>
<ImageButton
        android:layout_width="247dp"
        android:layout_height="61dp"
        android:id="@+id/imageButton" android:layout_marginTop="188dp"
        android:elevation="20dp"
        app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="72dp" android:cropToPadding="true" android:adjustViewBounds="true"/>
<TextView
        android:text="TextView"
        android:elevation="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" android:textSize="24sp"
        android:layout_marginTop="360dp" app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="196dp"
        app:layout_constraintStart_toStartOf="parent"/>

What to do to make the shadows appear under the button? And what to do to show up under the button where is android: background?

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
DEEEP 1029
  • 13
  • 5

1 Answers1

0

The default Button style under Material has a StateListAnimator that controls the android:elevation and android:translationZ properties.

copied from here

just add this property to your Button. you can set your own using the android:stateListAnimator property.

android:stateListAnimator="@null"

full code :

        <Button
                android:id="@+id/my_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="20dp"
                android:elevation="2dp"
                android:translationZ="2dp"
                android:stateListAnimator="@null"
                android:background="@android:color/holo_green_light"
                android:text="BUTTON">

UpDate :

for Understanding I set it 10dp..

xml code :

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp">

        <Button
            android:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="20dp"
            android:elevation="10dp"
            android:translationZ="10dp"
            android:stateListAnimator="@null"
            android:background="@android:color/holo_green_light"
            android:text="BUTTON"/>

    </RelativeLayout>
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70