Android selectableItem select selection

I want to change the background of my view when the state is "activated" and I want to keep the effects (ripple) ?attr:selectableItemBackground . Is it possible to extend or combine a selector ?attr:selectableItemBackground ?

+10
android
source share
3 answers

You can use LayerDrawable to draw a ripple effect effect ( ?attr:selectableItemBackground ) over the color of the activated state.

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <selector> <item android:state_activated="true"> <color android:color="?attr/colorPrimary"/> </item> <item> <color android:color="@android:color/transparent"/> </item> </selector> </item> <item android:drawable="?attr/selectableItemBackground"/> </layer-list> 

Edit: Since it is not possible to use theme attributes in an XML drawing before API 21, it seems that it is better to use the ripple effect, which can be cut as a cutting foreground, and the activated color selector can be used as a background for drawing.

 <View xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/yourView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:foreground="?attr/selectableItemBackground" android:background="@drawable/activated_color_selector"> 

With res/drawable/activated_color_selector.xml containing:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="true"> <!-- Can't use the ?attr/colorPrimary before API 21 --> <color android:color="@color/primaryColor"/> </item> <item> <color android:color="@android:color/transparent"/> </item> </selector> 
+9
source share

To change the ripple color throughout the application, you can place this in your application theme

 <item name="colorControlHighlight">@color/ripple</item> 
0
source share

Unfortunately, the only way I found is to have an extra look in your layout that will emulate the selected state. Here it is (it works on pre-Lollipop):

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?attr/listPreferredItemHeight" android:background="?attr/selectableItemBackground"> <View android:id="@+id/item_selected" android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/colorControlHighlight"/> <android.support.v7.widget.AppCompatTextView android:id="@+id/item_title" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:textAppearance="?attr/textAppearanceListItem"/> </FrameLayout> 

Then, in your adapter, you set the item_selected visibility item_selected either View.VISIBLE or View.GONE , based on the need to make this specific item selected.

PS Obviously, the AppCompat support library is used in this solution, so to use it in the build.gradle file, build.gradle need to add the build.gradle line:

 implementation 'com.android.support:appcompat-v7:28.0.0' 
0
source share

All Articles