1

The following is the custom ripple style of button:

Button

 <Button
        android:id="@+id/stop"
        style="@style/btn_start_style"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="@string/start"
        android:textSize="18dp"
        android:textStyle="bold" >
    </Button>

Style: btn_start_style

If I set "Background" in XML,the "style" will not working,so I set "Background" in "style".

 <style name="btn_start_style" parent="android:style/Widget.Material.Button">
    <item name="android:background">@drawable/btn_raised_start</item>
    <item name="android:textColor">@color/white</item>
</style>

Background: btn_raised_start.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@color/btn_start"/>
    </shape>
</item>

When click a "Raised Button",I want to change the BackgroundColor.

btnStop.setBackgroundColor(getResources().getColor(R.color.btn_start_day));

But when I change the BackgroundColor by the above code,the ripple gone!

zys
  • 1,306
  • 3
  • 18
  • 37
  • Have you seen this link? http://stackoverflow.com/questions/26686250/material-effect-on-button-with-background-color – Gennadii Saprykin Mar 17 '16 at 16:48
  • Yeah, but i want to change the color perennially when click it – zys Mar 17 '16 at 16:59
  • If you want to change the background color so that it never change, then you won't see ripple.. As far as I understand you want to change the ripple color, not the button color, right? You can create a separate selector for your ripple and change that when you do the click. Don't set the background color because it replaces the ripple drawable. Instead, get the ripple object and change the selector on it. – Gennadii Saprykin Mar 17 '16 at 17:10
  • could you give me a example? – zys Mar 17 '16 at 17:31
  • I think this should work: http://stackoverflow.com/questions/27787870/how-to-use-rippledrawable-programmatically-in-code-not-xml-with-android-5-0-lo – Gennadii Saprykin Mar 17 '16 at 17:34

0 Answers0