Ripple Effect for pre lollipop - android

I am developing an app, in which I want to add the ripple effect in my recyclerview. So can anyone tell me the code to do it, and is there a way I can achieve it in pre lollipop too. If not, please suggest me code with my app can show ripple effect in lollipop and upper versions and any simple effect for pre lollipop, to show item has been tapped or clicked.
Thank you.

you can use this library by this you can add ripple effect on pre-Lolipop version too.
for example
add button in your layout file
<com.rey.material.widget.Button
style="#style/ButtonRippleStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON"
app:rd_enable="true"/>
add custom style in your style file
<style name="ButtonRippleStyle" parent="Material.Drawable.Ripple.Wave.Light">
<item name="android:background">#null</item>
<item name="rd_rippleColor">#20F15A2B</item>
</style>

Why are there no ripples on pre-Lollipop?
A lot of what allows RippleDrawable to run smoothly is Android 5.0’s
new RenderThread. To optimize for performance on previous versions of
Android, we've left RippleDrawable out for now.
http://android-developers.blogspot.com.uy/2014/10/appcompat-v21-material-design-for-pre.html
If you want for pre-lollipop, you sould use a library like:
https://github.com/balysv/material-ripple
https://github.com/traex/RippleEffect
Others libraries: https://android-arsenal.com/tag/167
For >= 21 API you only need to set in your layout:
android:background="?android:attr/selectableItemBackground"

Related

Button design over Android OS versions

I'm trying to polish up some details about my app and I'm stuck on design of android Button widget. Up from Android 5.0 everything is fine with simple system Button, but problem starts when I start my app on lower versions - specifically 4.4.2 . System Button has no effect on click which is of course wrong UX. I'd like to avoid making multiple layouts for different android version and I think there is some way to solve this without making my own selector and using in in pre-Lollipop layouts, but I just can't find it. My Button layout looks as follows :
<Button
android:id="#+id/some_id"
android:layout_width="wrap_content"
android:layout_height="#dimen/size_48dp"
android:paddingLeft="#dimen/margin_32dp"
android:paddingRight="#dimen/margin_32dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
app:backgroundTint="#color/colorAccent"
android:textColor="#color/white"
android:text="#string/some_text"/>
As i said, on all post-lollipop version it has some effect (from android 6 it's ripple, before it just changes tint) but on Kitkat id does nothing. I tried using AppCompatButton, all sort of colorControlNormal, colorButtonNormal and I don't know what in styles but to no avail. Is there some "easy" way to solve this ?
You can create styles in both styles.xmlad v21 styles.xml
Below is code snippet of v21 styles.xml
<style name="ButtonRed" parent="Theme.AppCompat">
<item name="android:colorButtonNormal">#color/primary_red</item>
<item name="android:colorControlHighlight">#color/primary_red_dark</item>
<item name="android:textColor">#color/dash_title_color_white</item>
</style>
Below is code snippet of styles.xml
<style name="ButtonRed" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/primary_red</item>
<item name="colorControlHighlight">#color/primary_red_dark</item>
<item name="android:textColor">#color/dash_title_color_white</item>
</style>
Then you can use these styles as theme as given below
<Button
android:layout_width="wrap_content"
android:theme="#style/ButtonRed"
android:layout_height="wrap_content"
android:text="#string/recharge"
/>
Ripple type animation will only work from lollipop onwards. Below that the button will change colour state accordingly
Animations in material design give users feedback on their actions and provide visual continuity as users interact with your app. The material theme provides some default animations for buttons and activity transitions, and Android 5.0 (API level 21) and above lets you customize these animations and create new ones:
Touch feedback
Circular Reveal
Activity transitions
Curved motion
View state changes
Touch feedback in material design provides an instantaneous visual confirmation at the point of contact when users interact with UI elements. The default touch feedback animations for buttons use the new RippleDrawable class, which transitions between different states with a ripple effect.
Verify this Page
https://developer.android.com/training/material/animations.html#Touch
ANd Also these Answers will help you
how to create ripple effect for pre-lollipop

Android Ripple button with curved background for Pre-Lollipop devices

I need ripple effect for buttons. Currently I'm using ripple background for buttons in lollipop and upward devices using an xml in v21 directory and it is working fine, but I also need a ripple effect for pre-lollipop devices.
Here are my buttons.
I used some custom libraries for acquiring ripple effect for pre lollipop devices. For example I used com.balysv:material-ripple:1.0.2 library by following how to create ripple effect for pre-lollipop. In there they use this library which uses a custom view to wrap the button.
So when I use that for my curved, shaped buttons in pre lollipop, the result would be like this...
As you can see, the ripple effect goes out side of the button stroke boundaries. Is there is a way to resolve this?
Also, is there a way to acquire ripple effect using android's default support libraries instead of using custom 3rd party libraries for pre lollipop devices?
No. You don't have the option for ripple on pre-lollipop using support library. You may use this library - easy to use. Perform a check on OS and then implement the ripple if you have to like this.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// set layout background as ripple_drawable
} else {
// Implement the 3rd party library
}
use https://github.com/ozodrukh/RippleDrawable.
ripple.xml
<ripple xmlns:android="htp://schemas.android.com/apk/res/android"
android:color="yourRippleColor">
<!-- style for ripple -->
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="yourButtonCornerRadius"/>
<solid android:color="#FF00FF"/>
</shape>
</item>
<!-- background -->
<item android:drawable="yourBackgroundForButton" />
</ripple>
Activity.java
Button bu=...;
bu.setBackground(LollipopDrawablesCompat.getDrawable(getResources(),R.drawable.ripple,getTheme());
bu.setOnTouchListener(new DrawableHotspotTouch((LollipopDrawable)bu.getBackground()));
tested on Samsung Galaxy Advance Duos, android 4.4.2(api 19,KitKat)

Make ListView look the same despite Android version

My application uses a android.support.v7.widget.ListViewCompat and I'm trying to make the "Overscroll" effect look the same on Pre-lollipop and Lollipop.
On pre-lollipop the Overscroll has the Holo-blue color and on Lollipop and above, it uses the colorPrimary attribute.
Most related answers I find on SO are either very complicated (I think this should be a one-liner, preferable in the App theme) or "borks" the default effect on lollipop and looks wierd.
So can can I write to set the overscroll color to colorPrimary on pre-lollipop devices?
My style.xml (App Theme) file currently contains
<item name="colorPrimary">#color/my_green</item>
<item name="colorPrimaryDark">#color/my_forest</item>
<item name="colorAccent">#color/my_soil</item>
<item name="colorControlNormal">#color/my_green</item>
<item name="colorControlActivated">#color/my_forest</item>
<item name="colorControlHighlight">#color/white</item>
I also find it very strange that the colorControlHighlight color is not used when selecting items in the ListView on pre-lollipop but the default holo-orange color.
Glow is taken from R.drawable.overscroll_edge, so I guess you won't be able to replace it easily. It would be easier to replace the EdgeEffect class with custom drawable. See:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/widget/EdgeEffect.java#EdgeEffect
So maybe this is what you're looking for:
Unfortunately this is something really hidden and you won't be able to get that glow without reflection. The above screenshot was taken on Gingerbread, so if you're not afraid of hacks, it's possible. The code is rather long, so I won't paste it here. You can find the implementation in ScrollView and RecyclerView classes of Carbon. There's no ListView (it's now replaced by RecyclerView), but you should be able to get that working as well. It's a copy of Lollipop's glow set as edge effect, so it works and looks like it should.
https://github.com/ZieIony/Carbon/blob/master/carbon/src/main/java/carbon/widget/RecyclerView.java

Android custom button style: APi - < 21 and > 21 - attributes

I want to create several button styles instyles.xml. I want to support API 19 and higher so I would like to create button styles for both API. What I am doing:
I've created custom style in stylex.xml:
<style name="AppTheme.ButtonGreen" parent="android:Widget.Holo.Button">
<iten name="android:textColor">#color/accent</iten>
<item name="android:background">#color/primary</item>
</style>
In facy the color does not change. Can you help me with theming and give some basic attributes like: background color, text color etc. cause those does not work (only font color works).
I have also implemented style for v21 in styles (v21).xml:
<style name="AppTheme.ButtonGreen" parent="android:Widget.Material.Button">
<item name="android:textColor">#color/primary</item>
<item name="android:background">#color/text_icons</item>
</style>
But here I need font color, background color, elevation, font size etc. (only font color works).
Generally I guess I am doing something wrong. Can you guys help me style those buttons for different API?
Please read this article
https://www.androidcookbook.com/Recipe.seam?recipeId=3307
and this issue on StackOverflow:
How to create custom button in Android using XML Styles
I think the first one would be enough for your actual needs.
If you have question, please feel free to ask.
EDIT: if you still feel stuck - maybe it would be good to create an image and set it as ImageButton.
http://angrytools.com/android/button/
http://dabuttonfactory.com/
Or try to find issues abot using Material Design in lower APIs. This might be helpful: how to use material design features in api lower than 21 in eclipse?
you have to use background instead of backgroundtint

Lollipop's backgroundTint has no effect on a Button

I have a Button in my Activity, and I'd like it to have my theme's accent color.
Instead of making my own drawables like we had to do pre-Lollipop, naturally I'd like to use the new backgroundTint attribute.
<Button
android:id="#+id/btnAddCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/accent"
android:text="#string/addressInfo_edit_addCode" />
Unfortunately it has no effect, the button stays gray.
I tried different values for backgroundTintMode, which didn't change anything.
I also tried doing it programmatically in my Activity, which didn't change anything.
addCodeView.findViewById(R.id.btnAddCode).setBackgroundTintList(
getResources().getColorStateList(R.color.accent));
Why is my tint ignored?
EDIT:
Just to clarify, I am indeed testing on a Lollipop device.
Other widgets (e.g. EditText) are correctly and automatically tinted.
The bad news
Like BoD says, it's meaningless to tint a Button's background in Lollipop 5.0 (API level 21).
The good news
Lollipop 5.1 (API level 22) seems to have fixed this by changing btn_mtrl_default_shape.xml (among other files): https://android.googlesource.com/platform/frameworks/base/+/6dfa60f33ca6018959ebff1efde82db7d2aed1e3%5E!/#F0
The great news
The new support library (version 22.1+) adds backward-compatible tinting support to lots of components, including AppCompatButton!
Unfortunately, the android:backgroundTint property still doesn't work (maybe I'm doing something wrong) -- so you have to set the ColorStateList in code, using setSupportBackgroundTintList(). It'd be really nice to see android:backgroundTint supported in the future. Update: Marcio Granzotto commented that app:backgroundTint works on AppCompatButton! Note that it's app:, not android:, because it's in the app/library.
<LinearLayout
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="match_parent" >
<AppCompatButton
android:id="#+id/mybutton"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Testing, testing"
app:backgroundTint="#ff00ff"/>
</LinearLayout>
Your activity will automatically inflate an AppCompatButton instead of the normal Button if you let it inherit from AppCompatActivity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton v = (AppCompatButton) findViewById(R.id.mybutton);
ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00});
v.setSupportBackgroundTintList(csl);
}
}
You should of course get the ColorStateList from a color resource, but I was lazy, so...
Oh, and don't forget to base your app theme on one of the Theme.AppCompat themes, or the compat views will be very, very sad... ;)
This worked on both 2.3.7 (Gingerbread MR1) and 5.0 (Lollipop 'Classic').
It seems that tinting a ripple drawable is meaningless (and the default background of a button is a ripple drawable).
In fact, after looking at the platform's default button drawable, I found the "correct" way to do this:. You have to define this in your theme:
<item name="android:colorButtonNormal">#color/accent</item>
(Of course this is only for level 21+.)
Warning: since this is defined in a theme, this will use the given color for all the buttons (at least all of the buttons in activities using that theme.)
As a bonus, you can also change the ripple color by defining this:
<item name="android:colorControlHighlight">#color/accent_ripple</item>
To resolve issues related to tinting on Android 5.0.x I use something like this:
public static void setButtonTint(Button button, ColorStateList tint) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
((AppCompatButton) button).setSupportBackgroundTintList(tint);
} else {
ViewCompat.setBackgroundTintList(button, tint);
}
}
It uses the support method only for API 21 and the ViewCompat one for all other cases.
I usually do it dynamically by using PorterDuff:
mbutton = (Button) findViewById(R.id.mybutton);
mbutton.getBackground().setColorFilter(anycolor, PorterDuff.Mode.MULTIPLY);
You can check different blending modes here and nice examples here.
Just use app:backgroundTint instead of android:backgroundTint, the tint will take effect below Lollipop. The reason is AppCompatActivity use AppCompatViewInflater to auto change Button or TextView to AppCompatButton or AppCompatTextView, then app:backgroundTint take effect.
In my project I used it, it worked.
Tested on API 19 through API 27
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/retry"
android:textColor="#android:color/white"
app:backgroundTint="#android:color/holo_red_dark" />
produces output as -
I think you need to have android:background set to make android:backgroundTint work.
To be more accurate, my guess is that you can't backgroundTint the default button background from Material themes, which is defined as a RippleDrawable.
Just use app:backgroundTint instead of android:backgroundTint
Similar issue was reported on google https://code.google.com/p/android/issues/detail?id=201873
But after release of Android Support Library, revision 23.2.1 (March 2016) This bug is solved.
Issue : FloatingActionButton.setBackgroundTintList(#Nullable ColorStateList tint) no longer changes background color
update Support Library to Android Support Library to 23.2.1
Use design support library(23.2.1) and appcompatwidgets as below
Material Design for Pre-Lollipop Devices :
AppCompat (aka ActionBarCompat) started out as a backport of the
Android 4.0 ActionBar API for devices running on Gingerbread,
providing a common API layer on top of the backported implementation
and the framework implementation. AppCompat v21 delivers an API and
feature-set that is up-to-date with Android 5.0
Android Support Library 22.1 :
The ability to tint widgets automatically when using AppCompat is
incredibly helpful in keeping strong branding and consistency
throughout your app. This is done automatically when inflating layouts
- replacing Button with AppCompatButton, TextView with AppCompatTextView, etc. to ensure that each could support tinting. In
this release, those tint aware widgets are now publicly available,
allowing you to keep tinting support even if you need to subclass one
of the supported widgets.
If we look into the source code of Support Library, we see that it tints normally it's known buttons, but if we change the shape of our button (I have round button) tint doesn't work ok in api<=21.
We can also see that TintManager became public class (appcompat-v7:23.1.1), so we can take ColorStateList from default button shape (which is tinted ok in 5.0) for current theme (so we don't have to create the array of colors):
Context c = ...; // activity
AppCompatButton ab = ...; // your button
// works ok in 22+:
if (Build.VERSION.SDK_INT <= 21) {
// default appcompat button, that is tinted ok with current theme colors "abc_btn_default_mtrl_shape":
// ColorStateList tint = TintManager.get(c).getTintList(R.drawable.abc_btn_default_mtrl_shape);
// Appcompat 23.2 change:
ColorStateList tint = AppCompatDrawableManager.get().getTintList(c, R.drawable.abc_btn_default_mtrl_shape);
ab.setSupportBackgroundTintList(tint);
}
Because attribute backgroundTint is only used in API level 21 and higher
Be aware recyclerview most updated lib can cause this bug as well.
This command
sendBtnView.setBackgroundTintList(colorState)
worked perfectly in the past but stop working for me. after research it turns out the cause is the lib that was addeded to gradle dependencies:
compile 'com.android.support:recyclerview-v7:+'
So I tried to change it to 23.02.1 as it was recommended here in Amit Vaghela post.
I changed to
compile 'com.android.support:recyclerview-v7:23.02.1'
But gradle error said recyclerview lib does not have this version (23.02.1) (gradle could not find it in Jcenter raw.github or repo).
Then, becaouse I knew setBackgroundTintList command used to worke well in the past with version 22.02.0' in all the other libs I have in gradle dependencies.
so I change it to:
compile 'com.android.support:recyclerview-v7:22.02.0'
And now it works again.
I am not sure if this is recommended but you can try this:
Drawable newDrawable = mBtnAction.getBackground(); // obtain Bg drawable from the button as a new drawable
DrawableCompat.setTint(newDrawable, mActivity.getHomeTobBarBGColor()); //set it's tint
mBtnAction.setBackground(newDrawable); //apply back to button
In a general sense it works. Tried ViewCompat but it doesn't seem to work properly.
you can use backgroundTint <android.support.design.button.MaterialButton with "com.android.support:design:28.0.0-rc01" version
If you are using androidx then adding both prefixed and not prefixed version resolved issue on android 5.1:
<style name="Button_Primary">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:backgroundTint">#color/button_primary_selector</item>
<item name="backgroundTint">#color/button_primary_selector</item><!--needed for android 5-->
</style>
button_primary selector is in color folder with following content:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<item android:state_enabled="true" android:color="#android:color/holo_blue_dark" />
<item android:state_enabled="false" android:color="#android:color/darker_gray" />
</selector>
and apply it on regular button on AppCompatActivity
<Button style="#style/Button_Primary"/>
If you are developing an app that your target sdk is higher than api 21 and your minSdkVersion is 21(Lollipop) instead of
android:backgroundTint="#color/accent"
you can simply say..
android:background="#color/accent"

Categories

Resources