Android spinner has no ripple on the button itself, just its parameters

I can't seem to get the pulsed state pulsation to work on Spinner. Individual drop-out items have ripples, but not the spinner itself. I thought that maybe this is the expected behavior, but this animation from the material design guides clearly shows the spinner with the state pulsation pressed before a drop-down list of options appears.

https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B3321sZLoP_HTS1LdnhrTl9TRzg/components-buttons-otherbuttons-060203_MobileDropdowns_xhdpi_006.b

+5
source share
6 answers

I created a new Spinner in Activity , and as you described, RippleDrawable does not apply to Spinner using the v7 compatibility library or on Android 5.0+. This is not surprising to me; Google often does not complete its own project.

To have an impact, all I did was create a RippleDrawable in res / values-v21 / and set this background to Spinner using spinner.setBackgroundResource(R.drawable.spinner_background); . I am sure you can also set this in your topic.

+2
source

set these Spinner properties in xml

 android:background="@drawable/ripple_effect" android:dropDownSelector="@drawable/ripple_effect" 

and ripple_effect.xml will look like this:

 <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight" > <item android:id="@android:id/mask"> <shape android:shape="rectangle" > <solid android:color="?android:colorAccent" /> </shape> </item> </ripple> 
0
source

There is a library on Github, i.e. Layout material Ripple Layout . This library supports devices up to Lollipop and Lollipop. In addition, you can create your own ripple color. Here's the use:

 Spinner spinner = (Spinner) findViewById(R.id.spinner); MaterialRippleLayout.on(spinner) .rippleColor(Color.BLACK) .create(); 

But I do not use this library. I use the default empty Spinner line, which is presented in the rev 23.0.1 support library, by compiling: compile 'com.android.support:appcompat-v7:23.0.1' in the build.gradle the app module.

So here is my complete code for the Spinner layout:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:dropDownVerticalOffset="@dimen/dropDown_spinner" style="@style/SpinnerStyle"/> <!-- apply the ripple style --> </RelativeLayout> 

The style is defined in res/values/styles.xml :

 <style name="SpinnerStyle" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar"> <item name="android:background">?android:selectableItemBackground</item> <item name="android:dropDownSelector">?android:selectableItemBackground</item> <item name="android:divider">@null</item> <item name="overlapAnchor">true</item> </style> 

After that, the ripple effect should work as expected.

0
source

I use this cool library to apply ripple effects to some views. I like it because you can wrap any view in an XML file and apply the ripple effect.

https://github.com/balysv/material-ripple

Hope this helps.

0
source

The hope below the code will give some meaning to the ripple effect on Spinner.

ripple_spinner.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <ripple android:color="?android:attr/colorControlHighlight"> <item> <shape> <solid android:color="@android:color/white" /> </shape> </item> </ripple> </item> 

Your XML: -

your_acitivity.xml

 <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:dropDownSelector="@drawable/ripple_spinner" android:popupBackground="@drawable/popup_spinner_item_background" /> 

Below, the background of the drop-down list will be changed.

popup_spinner_item_background.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="2dp" /> <solid android:color="@android:color/white" /> </shape> 

In your res / values ​​/styles.xml: -

styles.xml

 <resources> <style name="AppTheme" parent="android:Theme.Material.Light"> <!--Workaround for the Android bug--> <item name="android:dropDownListViewStyle">@style/Theme.MyListView</item> </style> <style name="Theme.MyListView" parent="@android:style/Widget.Material.ListView.DropDown"> <item name="android:listSelector">@drawable/spinner_ripple</item> </style> </resources> 

Hope this helps you.

0
source

There are no ripples for Spinner since the background is not ripplable. I do this as follows in the layout of selectableItemBackgroundBorderless ( https://developer.android.com/training/material/animations.html#Touch ):

  • a 0 -width Spinner , and remove the arrow background (use @null will be fine), even I delete the background of the usual Spinner, it does not have ripper, so I use TextView and ImageView, such as the Google calendar.

  • a TextView + ImageView (for down arrow)

When the user clicks on the TextView / ImageView, it has a ripple animation, and then calls Spinner. performClick . And I want it to be like a normal dropdown menu, so I create a 0 -width spinner since gone Spinner will not call onItemSelected .

for a hidden adapter, it might look like this:

 @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); ((TextView) view).setText(""); return view; } 

Click listener layout:

 layout.setOnClickListener(() -> { spinner.performClick(); }); 

For a spinner element listener:

 spinner.setOnItemSelectedListener(() -> { // this requires spinner visiblity is not gone }); 
0
source

Source: https://habr.com/ru/post/1234158/


All Articles