How to make a circular ripple on a button when it's being clicked?

后端 未结 12 2092
一个人的身影
一个人的身影 2021-01-29 18:59

Background

On the dialer app of Android, when you start searching for something, and you click the arrow button on the left of the EditText, you get a circular ripple

相关标签:
12条回答
  • 2021-01-29 19:07

    Try:

    android:background="@android:color/transparent"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?actionBarItemBackground"
    
    0 讨论(0)
  • 2021-01-29 19:12

    If you are using AppCompat theme, then you could set the background of the view as:

    android:background="?selectableItemBackgroundBorderless"
    

    This will add circular ripple on 21 and above and square background on below 21.

    0 讨论(0)
  • 2021-01-29 19:12

    Just Add this background to your view

    android:background=?android:attr/actionBarItemBackground

    0 讨论(0)
  • 2021-01-29 19:15

    If you already have a background image, here is an example of a ripple that looks close to selectableItemBackgroundBorderless:

                <ImageButton
                    android:id="@+id/btn_show_filter_dialog"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:background="@drawable/ic_filter_state"/>
    

    ic_filter_state.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:state_pressed="true"
            android:state_enabled="true"
            android:drawable="@drawable/state_pressed_ripple"/>
        <item
            android:state_enabled="true"
            android:drawable="@drawable/ic_filter" />
    </selector>
    

    state_pressed_ripple.xml: (opacity set to 10% on white background) 1AFFFFFF

     <?xml version="1.0" encoding="UTF-8"?>    
        <ripple xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape android:shape="oval">
                    <solid android:color="#1AFFFFFF"/>
                </shape>
                <color android:color="#FFF"/>
            </item>
        </ripple>
    
    0 讨论(0)
  • 2021-01-29 19:20

    If you want more generic XML files, I have two files:

    1) btn_ripple_background with code:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/ripple_circular_shape"/>
    </selector>
    

    2) ripple_circuler_shape with code:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/btn_state_pressed_text_color"
        android:shape="oval">
        <solid android:color="@color/btn_state_pressed_text_color" />
    </shape>
    

    finally the usage:android:foreground="@drawable/ripple_btn_background"

    0 讨论(0)
  • 2021-01-29 19:22

    You can create circle ripple drawable using android:radius attribute in xml.

    Example:

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="your_color"
        android:radius="your_radius" />
    

    Pay attention, that your your_radius should be less then your view width and height. For example: if you have view with size 60dp x 60dp your_radius should be near 30dp (width / 2 or height / 2).

    Work on Android 5.0 and above.

    0 讨论(0)
提交回复
热议问题