add shadow to layout programmatically in Android? - android

So I have many LinearLayouts and RelativeLayouts that I need to apply shadows to. But all these layouts will have different background color.
For example, I have 3 LinearLayouts. I need one Blue, One Green and One Red but all will have the same shadows and style (except for the color).
I already have these layouts created so what can I do to apply shadows to them manually?
If I create an XML drawable then I have to create a different drawable for each color. Is there any other way I can do this?
I was thinking about making a common function where I can pass either a LinearLayout or RelativeLayout and apply the shadow effects with a specific solid color as background.
But I am not sure if there is a way to apply shadow programmatically. Please let me know what my choices are.
Thank you for your time.

Sounds like you need Outline. Since you mention that you want to do this programmatically Outline will do the trick. Fetch your layouts by findViewById(), use getOutlineProvider() from your layouts, get the ViewOutlineProvider then use the getoutline() method to set a custom Outline object that you will create as a shadow to any View that you desire. (also check this from the official documentation)
As for what your choices are I would have to say that generally speaking you have 2 choices.
1) Implement the shadows via XML declarations as a property of any other View that you inflate
2) Do it programmatically with Outline.

There is no such attribute in Android, to show a shadow. But possible ways to do it are:
1)Add a plain LinearLayout/RelativeLayout with colors, over which add your actual layout, with margin at bottom and right equal to 1 or 2 dp
2)Have a 9-patch image with a shadow and set it as the background to your Linear layout/RelativeLayout

Related

How to make UI shadow to any view

thank you for answering me
I just want to know how i can make the shadow below this card
I already tried several ways like Elevation... but it didn't work for me.
I searched a lot about it and I found that website :
Shadow generator
But i'm wondering if there is another way, using xml or anything else.
You are half right in that elevation is required for a shadow effect.But note this is only applicable to view that are not buttons. For buttonViews you can add a statelist animator that handles the properties of a button for all its states.Also if you are using something like a textView that does not have any margins you might as well use a background to show the shadow as follows:
android:elevation="30dp"
android:background="#000"
For other views, this means simply adding the elevation attribute as shown above and the background is not required
This link will help:
How to provide shadow to Button

How do you get ripple effect to extend past view bounds?

The touch ripple effect here expands past its bounds. How is this done?
EDIT: I am okay with this working on 5.0+ only.
I've read from a few places that:
If you want to apply the standard ripple effect on Android 5.0: API 21
or more, which should not be limited to your view (unbounded ripple),
just apply to your View background :
android:background="?android:attr/selectableItemBackgroundBorderless"
I have tested it, what is happening here is that the ripple is not on the buttons, but on the background object. So the ripple is not actually extending past the buttons bounds here, but rather extending to the bounds of the background wrapper card object.
To replicate what you see in the picture,
1. Make a new layout. I would use either a relative layout or linearlayout
2. Add the upper EditText ("Add a quick note"), and then and the four lower buttons, all with clear backgrounds, but with gray borders like above.
3. Set your ripple animation on the background of the layout you put the buttons in, and be sure to set android:clickable="true"on your layout!
That gave me the same effect as your picture.
doc already mentioned it
//An unbounded red ripple.
<ripple android:color="#ffff0000" />
https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html

Custom RadioButtons with different color

I am new to android and try to create a simple color picker.
The idea is to have a few filled circles showing a color and the selected one should have an circle around it.
That didn't sound too hard. I draw the circles with <shape>, create two resources, one with only a filled circle, one with the filled circle and an transparent circle with a solid stroke. Then I created a <selector> and set this as the background of my <RadioButton>.
This worked fine for one color, however now I'd like to have the same styles, but with a different color for each <RadioButton>.
The only solotution I could come up with is to create this triplet of xml-files for each color. This would certainly work, but it strikes me as extremely inelegant.
I tried to access the background of the buttons, but it seems I can only access the <selector>, not it's children.
I also tried to create a FrameLayout that would parent the RadioButton and an ImageView so the RadioButton only needs to display the border. This didn't fully work. I could select a RadioButton, but it wouldn't get deselected upon selecting another one.
I guess the best solution is to have a background that can change itself according to the state of the button, just like <selector> does, but with a more comprehensive way of determining which drawable to use, at least with access to the tag-Property of the RadioButton, but I cannot see how to do this.
check this github repo
https://github.com/VishalJogiya/CustomRadioAndShapes
xml layout code
<customradio.vj.com.library.CustomRadio
android:id="#+id/radio9"
android:layout_width="#dimen/thirty_two_dp"
android:layout_height="#dimen/thirty_two_dp"
android:layout_marginBottom="#dimen/eight_dp"
android:layout_marginLeft="#dimen/sixteen_dp"
android:layout_marginRight="#dimen/sixteen_dp"
android:layout_marginTop="#dimen/eight_dp"
custom:radioColor="#AA00FF"
custom:radioShape="simple_circle2" />

Default view padding

I'm just starting to learn android and i had a doubt concerning to view padding. Referring to What controls the default padding between views? , Is the padding included in the size we specify to the view ? All help appreciated!
EDIT: Also does a layout add padding to itself by default ?
In the question you linked to, the padding is caused by the button's background being a nine-patch and having some transparent borders in it. Since the "padding" is part of the background resource, it is include in the image size and you don't need to do anything to make it show up.
However, if you are creating your own Views, or changing the background or foreground resources of the system provided Widgets, you may want to use the android:padding* attributes to specify the various paddings.

How to draw sections in view?

Please help me in the following.
How to divide vertical layout by sections like on the picture http://developer.android.com/design/media/metrics_forms.png ? Its not a ListView, I have just layout with sets of text and editview. I just need to divide it so it looks nicer. Hot to draw it?
Also how to make so EditText looks like input on the picture?)
Thank you.
You are looking for Android Holo Theme, Please check Useful link for generating nine patch images & res drawables and styles for Holo Theme
You can use LinearLayout with vertical orientation, for spacing between the components create one common xml and include it wherever required in your layout.

Categories

Resources