Google has shown some nice ways that buttons are shown on Lollipop here.
I'm talking about both the raised and the flat buttons.
How can I mimic them on pre-Lollipop versions, except for the special effects (ripple etc...) ?
I'm talking about this and this.
Of course, on Lollipop and above, I'd like to use the special effects.
I know that there are multiple (and even many) posts about this already, but I don't see any that tries to mimic it completely.
Add the compat library to your app. (The most current version can be found here.) Do this by adding a dependency within your build.gradle file:
Declate your apptheme in a styles.xml file located in the values directory:
Set the declared style as the one to use for your application in the manifest.xml file:
In your layouts declare a raised button as follows
Declare a flat button style as follows:
Result on both a 4.4 (a phone) and a 5.0 (a tablet) device:
Details can be found in the official documentation: http://developer.android.com/training/material/theme.html
In addition to @spierce7 answer, I improved that layout a little bit. So now it has a relative layout in it, which sets our custom TextView exactly in a center of CardView. So here is it.
I'm working on a material compatibility library. I wrote my own Button class with material-like styles, animated shadows and the touch ripple effect. Although it's not perfect yet, You can try it here.
This one is hacky according to Yigit (google dev on cardview) but does a descent job.
You can use negative numbers in order to get rid of the padding and margin inside of CardView.
Tested on api 15 nexus one and nexus 4 emulators. This solution will work on pre lollipop.
Code:
Updated Answer: It is recommended that you rely on Google's appcompat library to backport material design styles to your app. It seems to no longer backport the raised button (instead using flat buttons), but still adequately backports a button that matches the material design style. It is my recommendation that you go with this approach.
If you still wish to backport the raised button effect, you will need to rely on a 3rd party material design library, or you can look at my old solution below using the
CardView
and implement your own.To setup the appcompat library with Google's official Material Design backport:
Then you need to set your application theme in your
styles.xml
file.And in your
AndroidManifest.xml
ensure the theme above is set properly:After the above setup, ensure all your
Activity
's extend fromAppCompatActivity
, and from there all yourButton
s will be inflated with a Material Design style.If you are using a custom
Button
implementation in your project, ensure that instead of extendingButton
, extendAppCompatButton
. That goes for most of your customView
s:AppCompatTextView
,AppCompatEditText
, etc...How does it work? All of your Android xml layouts are inflated using something called a
LayoutInflater
. TheLayoutInflater
is provided by theActivity
, and has the option of having aLayoutInflaterFactory
provided to it so that you can override default implementations of standard Android widgets, likeButton
andTextView
. When you use anAppCompatActivity
, it's providing you with a non-standardLayoutInflater
, which is inflating special instances of the widgets that support Material Design!------------------------------------------------- Old Answer: -------------------------------------------------
So Android has given us the CardView to replicate such behavior. It provides a View with a rounded corners, that applies a drop shadow. You will probably also want to look at the #setCardElevation() method.
You can add the CardView Library to your Gradle Project:
The CardView will use the Lollipop implementation to do this if it's available (as Lollipops shadows are better), otherwise it will replicate it as best as it can.
Here is a sample:
The above code running on 5.0:
The above code running on 4.4.4:
It looks like there is a discrepancy in the fonts, as in the sans-serif-medium has a different font weight or isn't supported on 4.4.4, but that can be easily fixed by including the latest version of Roboto and overriding the fonts on the TextViews.
The material documentation you post point to an an "Elevated Button". CardView's are how we make "Elevated" anything.
I achieve same effect using,
My tested output: