androidmaterial-designandroid-viewandroid-buttonandroid-attributes

Button with custom background and "?attr/selectableItemBackground"


How to define a Button with custom background and the attribute
"?attr/selectableItemBackground" ?

With this code the "?attr/selectableItemBackground" attribute is ignored, it doesn't show the touch feedback.

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:foreground="?attr/selectableItemBackground"/>

With this other code the selectable works but I lose the background color:

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"/>

Solution

  • Alright you have an option of parent

    Create parent and give background to white color and use selectableItemBackground as child's background.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"/>
    
    </LinearLayout>