Question Detail

<style name="MyButton" parent="MyMaterialTheme.Base">
    <item name="colorControlHighlight">#fff</item>
    <item name="colorButtonNormal">#4ba6eb</item>
    <item name="android:textColor">#fff</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:paddingLeft">20dp</item>
    <item name="android:layout_marginBottom">5dp</item>
    <item name="android:padding">10dp</item>
    <item name="android:elevation">20dp</item>
</style>


Thread Reply

Anonymous

- 1 years ago

Eventually solved my problem using a state list animator. For the rounded corners i used Rishad Appat's solution. I provided some xml-codes below. Just apply the style buttonTest1 to your button, and it should work.

styles.xml

<style name="buttonTest1" parent="android:style/Widget.Material.Button">
    <item name="android:background">@drawable/button1_background</item>
    <item name="android:stateListAnimator">@anim/buttons_state_list_animator</item>
</style>

buttons_state_list_animator.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_pressed="true"
        android:state_enabled="true">
        <set>
            <objectAnimator
                android:propertyName="translationZ"
                android:duration="@integer/button_duration"
                android:valueTo="@dimen/button_rise"
                android:valueType="floatType" />
            <objectAnimator
                android:propertyName="elevation"
                android:duration="0"
                android:valueTo="@dimen/button_elevation"
                android:valueType="floatType" />
        </set>
    </item>

    <item
        android:state_enabled="true">
        <set>
            <objectAnimator
                android:propertyName="translationZ"
                android:duration="@integer/button_duration"
                android:valueTo="0"
                android:startDelay="@integer/button_duration"
                android:valueType="floatType" />
            <objectAnimator
                android:propertyName="elevation"
                android:duration="0"
                android:valueTo="@dimen/button_elevation"
                android:valueType="floatType" />
        </set>
    </item>

    <item>
        <set>
            <objectAnimator
                android:propertyName="translationZ"
                android:duration="0"
                android:valueTo="0"
                android:valueType="floatType" />
            <objectAnimator
                android:propertyName="elevation"
                android:duration="0"
                android:valueTo="0"
                android:valueType="floatType" />
        </set>
    </item>

</selector>

button1_background.xml

<ripple android:color="?attr/colorControlHighlight" xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_background"></item>
</ripple>

button_background.xml, as provided by Rishad Appat

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorBackgroundTest" />
<corners android:radius="2dp" />
</shape>Image result for raised button android