Round rectangle corners and shadows in Android 4.x? - android

I want to create something close to Material Theme in app that is supposed to work with Android 4.x and 5.x.
I have tried to use Material theme, but I need api 21 for that. My current minimum api is 15 and I want to keep that.
Do I have to create shadows as PNG images or there is easier way?
I want to keep application compatybile with Android 4.x and I have no time to maintain two versions (for Android 4.x and 5.x).

I wouldn't call it an easy way, but you can mimic real animated shadows on any device above Android Cupcake. Here's how it works:
draw your view to an off-screen bitmap with LightingColorFilter set to 0,0
blur the black shape (the off-screen bitmap) using the ScriptIntrinsicBlur class and elevation value as radius
draw the bitmap beneath the view
It requires adding custom elevation attributes, custom views capable of rendering shadows, and using render script and the compatibility library (for older devices).
If you'd like to see that solution in action, check my library:
https://github.com/ZieIony/Carbon

Related

Android Number Picker "shadows"

The number pickers in Android contain a shadow above the previous and next value(s). Is there a attribute to make this shadow transparent?
If an image is being used as the background, these shadows are distracting.
Edit:
Running emulator with Android version 6.0.
Setting the solidColor attribute makes the shadows appear in different colors. Unfortunately, setting it to transparent still keeps the black fading appearance.
How this renders during emulation with the background image:
I think it's just the Android version is too old. I didn't do any customization to numberpicker and it still looks flat (no ugly shadow like that). My test device running 6.0

Android shadows pre Lollipop

In android 5.0+ exists property elevation, which add shadows for views. Who can suggest how make compatiblity with older versions 5.0< ? 9-patch not good idea, because in my app shadow everywhere and views different (with rounded corners, not rounded and e.t.c).
you can use card view from support. but if you meant Ripple animation you should draw it by yourself

how we can apply system rendered shadows to a view

I am trying to implement shadows for my views. I have given a sample image which describe what I want, I need a google standard methods for implementing shadows, I have tried with drawable images but as you know which is not standard method.
You mean like this?
Defining Shadows and Clipping Views
https://developer.android.com/training/material/shadows-clipping.html
You have to use CardView as view container (which is in an official support library) and set properties:
app:cardElevation="4dp" // shadow
app:cardUseCompatPadding="true">
where app namespace is:xmlns:app="http://schemas.android.com/apk/res-auto"
I'm guessing that you're thinking about older devices.
You can render shadows using RenderScript blurs and layer composition. This method is available since Android Froyo. It's pretty easy to create such shadows from scratch. You have to:
Draw a View as a black shape to an offscreen bitmap.
Blur that bitmap with ScriptIntrisincBlur
Draw that bitmap with alpha
Draw the original View on top of that shadow.
You have to specify a blur radius based on View's elevation. You may also offset the shadow a bit. For implementation details see:
https://github.com/ZieIony/Carbon/blob/master/carbon/src/main/java/carbon/shadow/ShadowGenerator.java
And
https://github.com/ZieIony/Carbon/blob/master/carbon/src/main/java/carbon/widget/LinearLayout.java#L128
In general, with API21 you can use the view.setElevation() method.
Better if you use the ViewCompat.setElevation() method.
Pay attention, it creates a shadow only in devices with API21+.
If you would like to create an elevation for other views for device with api<21, you have to use a custom drawable.
Currently the only standard view that supports elevation in all devices, is the CardView.
In this case you can use the setCardElevation() method.

Set content to edge of Cardview pre 5.0

I'm using the CardView layout from the v7 support library as a primary design pattern in my app. It works great on Lollipop as you'd expect and looks like this:
If I run this same app on 4.4 or below the content in the card doesn't go to the edge as seen here:
Is there a way that I can get the content to go to the edges on all versions of Android and not just 5.0?
Thanks
Unfortunately the creators of the CardView didn't come up with an efficient way to create rounded corners that would work for any layout on devices running earlier versions that Lollipop. Because of that, padding is simply added to the content of the cards. To prevent this padding use this method:
cardView.setPreventCornerOverlap(false);
If you do this however, the ImageView's corners will be drawn over the rounded corners of the CardView. A solution to this problem is to create a custom ImageView (or Drawable, or Picasso transfromation...) that draws rounded corners. Check this Gist on how to do that.
Since you are using a common layout for cards with a header image, you could check this library by the same author which creates the rounded corners without padding.

Draw specific shadow in Android

I'm looking for way how to implement specific shadow (like on picture) in my android app Android, with using xml (I can't use 9 patch for this).
This is white rectangle with the same shadow in all directions.
Any idea?
As far as I know, there's no way to create a shadow efficiently with XML without using 9-patch image. You can try playing with shapes and gradients, but the result won't look good.
I also did some tests by adding several shapes with transparent borders, but again the result is not good.
Can you explain your situation (why can't you use 9-patch, which is well supported by android).

Categories

Resources