Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

?android:attr/selectableItemBackground not visible enough on a dark background

On Android Lollipop, I'm using:

android:background="?android:attr/selectableItemBackground"

to have the material animated feedback when I click on a button.

It works well when I have a button contained in a white/light layout, like a CardView for example.

But when I want to use the same thing on a dark background, we barely see the effect, it is not visible enough.

Does someone have an idea?

Thank you

like image 454
Stéphane Avatar asked Feb 19 '15 11:02

Stéphane


People also ask

How do I enable dark theme in Android 10?

Dark theme applies to both the Android system UI and apps running on the device. There are three ways to enable Dark theme in Android 10 (API level 29) and higher: Use the system setting (Settings -> Display -> Theme) to enable Dark theme. Use the Quick Settings tile to switch themes from the notification tray (once enabled).

Does dark-themed Android window background drawables work on Android 10?

Note that dark-themed android:windowBackground drawables only work on Android 10. When the app’s theme changes (either through the system setting or AppCompat) it triggers a uiMode configuration change.

What is the use of selectableitembackgroundborderless?

?android:attr/selectableItemBackgroundBorderless for a ripple that extends beyond the view. It will be drawn upon, and bounded by, the nearest parent of the view with a non-null background. Note: selectableItemBackgroundBorderless is a new attribute introduced in API level 21.

How do I force dark mode on my Android app?

Apps must opt-in to Force Dark by setting android:forceDarkAllowed="true" in the activity's theme. This attribute is set on all of the system and AndroidX provided light themes, such as Theme.Material.Light. When you use Force Dark, you should make sure to test your app thoroughly and exclude views as needed.


2 Answers

On API 21+ you can set android:theme="@android:style/ThemeOverlay.Material.Dark" on a View or ViewGroup to change all of the theme attributes (text color, ripple color, button color, etc.) to the "dark" versions. If you set it on a ViewGroup, the theme is also applied to all of the child elements during inflation. It's an easy way to have regions of "dark" in an otherwise "light" interface (or vice versa).

<LinearLayout
    android:id="@id/my_dark_layout"
    ...
    android:theme="@android:style/ThemeOverlay.Material.Dark">

    <TextView
        android:id="@id/my_dark_bounded_ripple"
        ...
        android:background="?android:attr/selectableItemBackground"
        android:text="Bounded ripple" />

    <ImageButton
        android:id="@id/my_dark_unbounded_ripple"
        ...
        android:background="?android:attr/selectableItemBackgroundBorderless"
        android:src="@drawable/my_icon" />

</LinearLayout>
like image 166
alanv Avatar answered Sep 21 '22 13:09

alanv


Solution with AppCompat (works on old APIs too)

android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
android:background="?attr/selectableItemBackground"
like image 35
Ali Zarei Avatar answered Sep 23 '22 13:09

Ali Zarei