Charles
Charles

Reputation: 575

Android L Raised Normal Button

I've been hunting high and low for a way to get a "raised" rounded rectangular button without completely doing it myself using backgrounds and a custom view. I want buttons like this:

I thought this would do it:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter the Danger Zone"
        android:id="@+id/btn"
        android:elevation="8dp" <!-- Here -->
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"/>

</RelativeLayout>

But this does nothing. The button looks the same. WTF?

And yes, I'm using the latest SDK, etc. etc.

Edit: So I added a translationZ. Now, a shadow appears as the activity is opening, but disappears when the animation is done. ???

Upvotes: 1

Views: 688

Answers (2)

Stefan Bushev
Stefan Bushev

Reputation: 111

I had a similar problem i thought was due to wrongly inflated layouts, but it appeared that adding clipToPadding did the trick. This must be set to the parent ViewGroup containing the view you want to cast a shadow.

<YourParentLayout
...
android:clipToPadding="false"
...

also if your view has no background, use android:outlineProvider="bounds"

Upvotes: 1

IuriiO
IuriiO

Reputation: 641

Try adding state list animator:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_enabled="true"
    android:state_pressed="true">
    <set>
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueTo="8dp"
            android:valueType="floatType" />
    </set>
</item>
<item>
    <set>
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueTo="2dp"
            android:valueType="floatType" />
    </set>
</item>
</selector>

Upvotes: 1

Related Questions