Android Layout, how to make a radius and border like css - android

I want to make some think like this in android layout background, is there any solution ?
I have 2 borders, one at top and the other is bottom, and they have different colors, and also I don't want to have any borders at left and right
CSS Code is:
#shape{
border-radius : 2px;
border-top : 1px solid #070709;
border-bottom : 1px #383841;
/*...*/
}
NOTE: Border has radius too.

Create an XML in drawable folder and put this code in it:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#android:color/darker_gray" />
<corners android:radius="5dp" />
</shape>
</item>
<item android:top="3dp">
<shape android:shape="rectangle" >
<solid android:color="#383841" />
<corners android:radius="5dp" />
</shape>
</item>
<item
android:bottom="3dp"
android:top="3dp">
<shape android:shape="rectangle" >
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
This layer list contains 3 items
1: color on top with round corners.
2: color on bottom with round corners.
3: color on rest of the view.
You can set this as background to any view.

Related

Custom drawable background for Android EditText

I would like to create a custom drawable background for an EditText component in my Android layout. This should include a lower border spanning the entire length of the EditText, and 2 connected vertical lines at either end which take up 25% of the height, as per below:
The text itself will then use android:gravity="left|center" with a small amount of margin on the left/top/bottom.
I've been able to create a custom border on other input text elements where it was desirable to include all borders using a shape such as below:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#android:color/transparent"/>
<corners
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"/>
<stroke
android:color="#color/lightgrey"
android:width="1dp"/>
</shape>
However, now I don't want a full rectangle, only 1 full side + 2 partial sides.
Two steps to achieve that:
Keep only the bottom border by removing the top, right, and left borders by adding negative insets with android:left|right|top.
Add new rectangle for the side items: adjust left/right relevant gravity, and limit the height to which size you want (here it's set to 8dp):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom border -->
<item
android:left="-50dp"
android:right="-50dp"
android:top="-50dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/lightgrey" />
</shape>
</item>
<!-- Left border -->
<item
android:height="8dp"
android:gravity="left|bottom">
<shape android:shape="rectangle">
<solid android:color="#color/lightgrey" />
<size android:width="1dp" />
</shape>
</item>
<!-- Right border -->
<item
android:height="8dp"
android:gravity="right|bottom">
<shape android:shape="rectangle">
<solid android:color="#color/lightgrey" />
<size android:width="1dp" />
</shape>
</item>
</layer-list>
Here is the result

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>

EditText border color on threesides only with shape.xml

Hi I want the edittext border colour only on left,top and right with blue colour but bottom should be transparent or grey.
How can I achieve this using shape.xml
As of now I am using the below code but unable to make the bottom line grey
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle"
>
<stroke android:width="1dp"
android:color="#49b1bb"
/>
<corners android:radius="0dp" />
</shape>
Following code will help you to achieve your goal:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Border -->
<item>
<shape>
<solid android:color="#49b1bb" >
</solid>
</shape>
</item>
<!-- Body -->
<item
android:left="2dip"
android:right="2dp"
android:top="2dp">
<shape>
<solid android:color="#ffafafaf" >
</solid>
</shape>
</item>
</layer-list>
What exactly above code is doing is it simply create border for all side first then after that It is adding layer on bottom border so that it is not visible to user.
Happy coding!!!!!!!!!!

android layer list won't show both a gradient and a solid

I don't understand what is going on with this code.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="10dp">
<shape android:shape="rectangle" >
<solid android:color="#ff8898A4" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#ffD6DDD8"
android:startColor="#ffB1BBC3" />
</shape>
</item>
</layer-list>
If I move the solid below the gradient the gradient doesn't show. But the way it is now the solid won't show. I didn't have any trouble with two solid. What am I doing wrong?
You are placing the solid shape item over the gradient shape item.
You need to add some padding to the gradient shape. You are doing the opposite adding the padding to the solid shape versus the gradient shape, try this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#ff8898A4" />
</shape>
</item>
<item android:bottom="10dp">
<shape android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#ffD6DDD8"
android:startColor="#ffB1BBC3" />
</shape>
</item>
</layer-list>
The second rectangle covers the first completely. Items in a layer-list are drawn on top of each other beginning with the first. If the don't have any offset, they all have the same size.

Android xml shape drawable - how to draw a u-form?

I need to create a xml shape drawable that draws a rectangle without the top-line (a "u-form"). What I am able to do is draw a rectangle, like so:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/detailrow_bg_normal" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:radius="1dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<padding
android:bottom="2dip"
android:left="1.5dip"
android:right="1.5dip"
android:top="8dip" />
<stroke
android:width="1dip"
android:color="#color/detailtable_border" />
But how - if possible, can I define the same shape without the top (or bottom) line?
You could try using a layer-list. See if something like this would work:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#color/detailtable_border" />
</shape>
</item>
<item android:left="1.5dp" android:right="1.5dp" android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/detailrow_bg_normal" />
</shape>
</item>
</layer-list>
This (should) fill the rectangle with the border color, then overlay it with the default background color, leaving the appropriate amount of the right/left/bottom border color showing.
try this code and also refer this link:::
I achieved a good solution with this one:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item android:top="-1dp" android:right="-1dp" android:left="-1dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#ffffff" />
</shape>
</item>
</layer-list>
This works well in case you need a transparent color but still an open stroke color (In my case i only needed a bottom line). If you need a background color you can add a solid shape color as in above answer.
You might be able to achieve this using two shapes and a LayerList, but I think it is both a better solution and easier to use a NinePatch-drawable.

Categories

Resources