Android Drawable overlay shape - android

I'm trying to create a drawable overlay shape control that will basically have a solid WHITE box at the top and then the remaining segment of the screen will have a gradient from a solid white down to a translucent white. We use this to lay over a background image to create a solid white area on top and then fading into the a background image towards the bottom.
Using this code it gets basically what I want, except I want a true solid white area under the Company Logo area (sorry I had to blank out the Company Logo and the App Name in the screen shot) with the gradient starting below the solid white.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#ffffffff"
android:centerColor="#00ffffff"
android:endColor="#00ffffff"
android:centerY="0.6"
android:type="linear" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffffffff"
android:centerColor="#ffffffff"
android:endColor="#00ffffff"
android:centerY="-0.17"
android:gradientRadius="100%p"
android:type="radial" />
</shape>
</item>
</layer-list>
Which produces a display similar to this:
But I really want a more solid white area to be directly under the Company Logo area and then with the gradient starting after that point. But you can see that the gradient is starting at the top of the screen.
I've tried to do something like the following but it's not working. Adding the first item in the list does give me a solid area at the top, but for some reason that I'm probably just not understanding is that the top item fills in the entire screen with the solid white color. I tried moving that first item to the bottom but that still gave me the full solid white screen.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape
android:shape="rectangle">
<size android:height="100dp" />
<solid android:color="#ffffffff"/>
</shape>
</item>
<item android:top="100dp" >
<shape
android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#ffffffff"
android:centerColor="#00ffffff"
android:endColor="#00ffffff"
android:type="linear" />
</shape>
</item>
<item android:top="100dp" >
<shape
android:shape="rectangle">
<gradient
android:startColor="#ffffffff"
android:centerColor="#ffffffff"
android:endColor="#00ffffff"
android:centerY="-0.17"
android:gradientRadius="100%p"
android:type="radial" />
</shape>
</item>
</layer-list>
Can anyone tell me what I'm doing wrong or give me a better way to do this.
Just for additional information. The actual image is being applied to the background of the base LinearLayout view. Then another view is overlayed on top of the base view with this drawable set as the background on it's LinearLayout so it acts as an overlay mask on top of the background image.
I appreciate any help I can get on this.

You can add two Linear Layouts children to one Relative Layout parent. Have the top Linear Layout have the background of white, and the bottom one your gradient.

Related

Corner Color of a Shape in android

I know the solid color and also the corner section but
I want to know how can I fill color of the corner
Try using stroke in your layout file. stroke can give you the border colour and width.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:color="#color/yourcolour"
android:width="4dp"
/>
//Other adjustments
</shape>
Let me know if this is what you were looking for
Or if you want something like in your image try the below
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle">
<solid android:color="#android:color/holo_red_dark"/>
<stroke android:color="#android:color/black"
android:width="10dp"
/>
</shape>
</item>
<item >
<shape android:shape="rectangle">
<solid android:color="#android:color/holo_blue_bright"/>
<stroke android:color="#android:color/black"
android:width="10dp"
/>
<corners android:bottomLeftRadius="150dp"/>
</shape>
</item>
</layer-list>
It seems you talking about that red colour on above image.
In that case, there is no property which give you such result.
You need to make it logically., Like take 2 views with diff background,
Red background with black colour stroke.
Sky background overlaping on 1st red view.
And simply make top view background as rounded with black color stroke. It will provide you the desired design layout.
Make sure you give same stroke to both view so that itlook to be same.
Happy coding.

how to make corner based gradient drawable

I'm trying to make gradient background that placed only bottom right.
(Sorry for images because I don't have enough rep for inline)
https://i.stack.imgur.com/ebgWn.jpg
First, I made radial gradient but not perfect
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerX="0.9"
android:centerY="1"
android:endColor="#00000000"
android:gradientRadius="40%p"
android:startColor="#BF000000"
android:type="radial" />
</shape>
It looks like:
https://i.stack.imgur.com/JOqFf.jpg
Then, I made linear gradient but still not perfect.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="315"
android:endColor="#CD000000"
android:startColor="#00FFFFFF"
android:type="linear" />
</shape>
https://i.stack.imgur.com/a0y9C.jpg
Because gradientRadius parameter doesn't work with linear gradient. Are there any starting/ending point parameter for this gradient? Thanks for help!
Best i have tried with
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:centerX="0.8"
android:centerY="1"
android:endColor="#00000000"
android:gradientRadius="80%p"
android:startColor="#BF000000"
android:type="radial" />
And then Wrap your TextView and Tick Icon together inside a layout and set the above background to it..
If you want the exact same effect as you provided in image then you better use a Image or vector file for it .. But the approach will be same setting the background to only Wrapped layout not the Root.

How is a bottom line added with this layer-list drawable?

I want to have a bottom line in a view. The following drawable somehow adds a bottom border:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<!-- Set the border color of your layout here -->
<solid android:color="#color/red" />
</shape>
</item>
<!-- This is for border bottom but
you can change this according to your need -->
<item android:bottom="2dp" >
<shape
android:shape="rectangle">
<!-- Set the background color of your layout here -->
<solid android:color="#color/green" />
</shape>
</item>
</layer-list>
The result of this is:
Problem:
1) I don't understand how this works at all. It seems this is some trick using margins to get a red bottom border but I don't really get it.
2) I need to be able to add a bottom border but I don't want to set any specific background color for the whole view. Is that possible?
This is telling the system from where to start this item layout.
Since here we have bottom 2dp so this layout start 2dp from bottom.
Change bottom to end,start or other options for more understanding.
For 2) I need to be able to add a bottom border but I don't want to set any specific background color for the whole view. Is that possible?
replace your drawable with below code:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
<!--Uncomment this if you wnat to set any background color to
your rectangle
<solid android:color="#ffffff" />-->
<stroke
android:width="1dp"
android:color="#color/red" />
</shape>
</item>

Android - Layer-List Rounded Rectangle Issue

I've created a layer-list drawable with overlapping round rectangles to create a shadow but started to noticed a weird issue with the bottom layer peeking thru when the corner radius is used. So as a test, I just layered two rounded rectangles and the behavior exists. Is this a possible issue?
If you look at A, you'll notice the corners displaying the bottom layer's color. I switched the top layer's color to white and as you can see there is something going on here as you can see the corners showing up when they should not. Below is the drawable,
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape>
<solid android:color="#ff0000" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#ffffff" />
<corners android:radius="5dp" />
</shape>
</item>

Set width of Android Drawable item?

What I wanted was while background with red part on the left. Something like this.
I tried many things (is there any WYSIWIG editor? I think I ran this hundreds of times.) Specifying width like in the following article did not work.
how to specify width and height of a drawable item
The following article only tells me that I cannot use percentage, now how to apply width.
Android Drawable: Specifying shape width in percent in the XML file?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid
android:color="#FFFFFFFF"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#55FF0000" />
<size android:width="100px" />
</shape>
</item>
</layer-list>
Rather than specify the size of the red box, you would define a red layer and then set the left margin of the white layer equal to the amount of red to show.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#55FF0000" />
</shape>
</item>
<item android:left="100px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFFFF"/>
</shape>
</item>
</layer-list>
If you know the desired negative space, you could even swap the two and set the right margin of the red (now top) layer to the amount of white that should be visible.

Categories

Resources