Button with custom background and "?attr/selectableItemBackground"
Asked Answered
P

3

7

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"/>
Poplar answered 13/3, 2017 at 11:10 Comment(2)
You can user ripple drawables instead.Isoniazid
But I don't want to create a ripple for API>21 and another drawable for API<21. It is not posible with an attribute?Poplar
I
4

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>
Isoniazid answered 13/3, 2017 at 11:21 Comment(2)
It is a good practice to add a parent layout just to get a background color for his child?Poplar
Not a good practice, but since you do not want to use ripple drawable, Its is an option.Isoniazid
J
3

Try set

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

Example main layout activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.petercarlim.myapplication.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:background="@android:color/darker_gray"
        android:foreground="?android:attr/selectableItemBackground"/>

</LinearLayout>

Or set in parent(other layout that call this layout) of you layout:

    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" 
Junoesque answered 13/3, 2017 at 11:20 Comment(4)
what API Level?Junoesque
API 25 and the minimum is 15Poplar
Here work... button with flags clickable, focusable, focusableInTouchMode, foreground and background, but my main layout don't have background or foreground. And my app theme is "default". What is your layout theme?Junoesque
Edit your answer with your layout and button inside, I will try it. My AppTheme is Theme.AppCompat.Light.DarkActionBar. ThanksPoplar
M
3

You should write a special drawable for it

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <selector>
            <item android:drawable="@drawable/your_drawable_rounded_enabled" android:state_enabled="true"/>
            <item android:drawable="@drawable/your_drawable_rounded_disabled" android:state_enabled="false"/>
        </selector>
    </item>
    <item android:drawable="?attr/selectableItemBackground"/>
</layer-list>
Meeks answered 12/6, 2019 at 12:56 Comment(1)
This must be the most recommendable answer. Working like a charm.Royden

© 2022 - 2024 — McMap. All rights reserved.