Search code examples
androidanimationbuttonstroke

Animate the outer stroke of a view (button)


I'd like to flash/blink the stroke of a button a few times based on a timer. For example, every 30 seconds flash the stroke 3 times from white to black or some such thing. Can this be done? Thanks


Solution

  • you can create custom button layouts like such and put them in the drawable folder

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item>        
        <shape>
            <gradient
                android:endColor="@color/white"
                android:startColor="@color/whitesmoke"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="@color/grey3" />
            <corners
                android:radius="10dp" />
        </shape>
    </item>
    

    where the color grey3 is the border and you can start a timer and add a TimerTask class to your class and then make it run the task every 30 seconds and just change the buttons background from one .xml to another with a different border color.