ShapeDrawable with black border on top - android

I'm trying to create a ShapeDrawable for the background of my table cells. At the moment, I have a white rectangle with a 1px black border:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="#fff"/>
<stroke android:width="1dp" android:color="#000"/>
</shape>
I'd like to get this exact thing but with the border only along the top and bottom of the shape. Can someone show how to do this, or link to a page explaining how to use ShapeDrawable? Thanks!

You use a layer-list take a look at this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:top="5dp" android:bottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
Or you may just do a padding on your layout like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_marginTop="5dp"android:layout_marginBottom="5dp"></LinearLayout>
</LinearLayout>

Related

Android border bottom for custom view not working

What I'm trying to do is to have a relativelayout with a gradient on it and a gray bottom border, I'm using the below custom view in my application:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/SIZE_LIST_ITEM_LARGE"
android:scaleType="centerCrop"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape_list_item_gradient" />
<com.rahil.widgets.CustomTextView
android:id="#+id/list_item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="bottom"
android:textColor="#color/COLOR_BLACK"/>
</RelativeLayout>
and this is shape_list_item_gradient:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF"
android:angle="90" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/COLOR_GRAY" />
</shape>
</item>
</layer-list>
but the above code applies border to all borders not just bottom border. I added the separate shape xml to the relativelayout but it didn't work which I assume the view with gradient covers it maybe?? I'm quiet lost. thanks for your help.
The android:bottom attribute is actually the "bottom offset in pixels" as noted here about layer-list drawables. There is no explicit way to create a rectangle that is stroked only on one side.
However you can use the same trick that Krylez suggested here: https://stackoverflow.com/a/19239478/1538674
So something like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF"
android:angle="90" />
</shape>
</item>
<item android:top="-3dp" android:right="-3dp" android:left="-3dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="2dp" android:color="#color/COLOR_GRAY" />
</shape>
</item>
</layer-list>

Add only top and bottom border on LinearLayout [duplicate]

This question already has answers here:
Is there an easy way to add a border to the top and bottom of an Android View?
(25 answers)
Closed 1 year ago.
I would like to add only a bottom and a top border on my Linearlayout.
I have tried to do this :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="1dp"
android:top="1dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#000" />
</shape>
</item>
</layer-list>
But it add a border around the shape..
Could you help me please ?
I think you can create this drawable and use it as background:
<?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="#000"/>
</shape>
</item>
<item android:bottom="1dp" android:top="1dp">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Think of is as drawing a rectangle with border color first and then lay on top of it a rectangle with your background color leaving out 1dp on top and at the bottom.
Make this two file and put this code. you can set border top and bottom border,
main.xml
<TextView
android:text="This is textline"
android:background="#drawable/border_set"
/>
border_set.xml
This file located into full path project_root/res/drawable/border_set.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF000000" />
<solid android:color="#FFDDDDDD" />
</shape>
</item>
<item android:top="1dp" android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#000" />
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Here is the solution. It works even with transparent background.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="-2dp" android:right="-2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/borderColor" />
<solid android:color="#color/backgroundColor" />
</shape>
</item>
</layer-list>
I believe this is the simplest way:
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
This is my version; top border and bottom border are visible, not showing the left or right borders. And the background is transparent.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="1dp">
<item
android:left="-1dp"
android:right="-1dp"
android:top="-1dp"
android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/BlueGrey_colorPrimary" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
</layer-list>
A quick way to achieve this:
Add a Text View to the bottom and/or top of your layout.
Set the TextView's width to "match_parent"
Set the TextView's height to about "1dp" or find the thickness you would like
Set the TextView's background to the color you would like the border to be
I hope this helps!
You can follow this link Is there an easy way to add a border to the top and bottom of an Android View?
I expect that you are solve from this link.
also you can solve How to add border around linear layout except at the bottom?
Its simple. Draw 3 shapes like this.
<?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="#color/menu_line_separator_in" />
</shape>
</item>
<item android:bottom="1.5dp">
<shape android:shape="rectangle" >
<solid android:color="#color/menu_line_separator_out" />
</shape>
</item>
<item android:top="1.5dp">
<shape android:shape="rectangle" >
<solid android:color="#color/menu_line_separator_out" />
</shape>
</item>
</layer-list>

View with Solid Background and Top+Bottom Inner Shadows

Essentially, I am trying to create the following background:
The traditional gradient which use in the drawable that I use for background only supports start color, middle color and end color.
However, as you can see from the mockup, I am trying to create only a slight overlay/shadow at the top and bottom of the shape, with a #50000000 color (black with 50% opacity).
If you're using this inside a Layout view, then you can simply create a View with a gradient background and place it in the beginning and in the end of the Layout.
For example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/parent">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#drawable/gradient" />
<!-- Your other child views -->
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#drawable/gradient" />
</LinearLayout>
And your gradient.xml file will have this:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#FFFFFF" android:endColor="#000000" android:angle="90"/>
</shape>
You can specify the blue background color to the parent layout.
You'll essentially get something like this:
[EDIT]
You can create two drawables - gradient_top.xml and gradient_bottom.xml to get the angle right
I prefer doing this than having to mess around with 9-Patches. Although, having said that, I wish Google would get on with providing built-in support for drop shadows, since they're so common.
Just to build on JoelFernandez solution with a more complete example:
The Container:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="[your_container_height]"
android:background="#drawable/container_bg_color">
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_alignParentTop="true"
android:background="#drawable/container_gradient_top"/>
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_alignParentBottom="true"
android:background="#drawable/container_gradient_bottom" />
<!-- Insert your content here -->
</RelativeLayout>
The Background Color (container_bg_color.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<gradient android:startColor="#4a6fb4"
android:endColor="#color/deepBlue"
android:angle="135"/>
</shape>
The Top Gradient (container_gradient_top.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<gradient
android:startColor="#00222222"
android:centerColor="#11111111"
android:endColor="#44000000"
android:angle="90"/>
</shape>
The Bottom Gradient (container_gradient_bottom.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<gradient
android:startColor="#44000000"
android:centerColor="#11111111"
android:endColor="#00222222"
android:angle="90"/>
</shape>
Result:
Elaborating #ramaral's answer, build this drawable:
<?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="#FF408DAA"/>
</shape>
</item>
<item android:top="0dip" android:bottom="32dip">
<shape android:shape="rectangle">
<gradient android:startColor="#00000000" android:endColor="#50000000" android:angle="90"/>
</shape>
</item>
<item android:top="32dip" android:bottom="0dip">
<shape android:shape="rectangle">
<gradient android:startColor="#50000000" android:endColor="#00000000" android:angle="90"/>
</shape>
</item>
</layer-list>
You would need to set a fixed height in the view, in order to achieve the best result. In my case I was setting the height to "36dip". Notice that the "32dip" is the amount of space from where the gradient ends to the end of the drawable, so that would leave me with a top and bottom gradients of "4dip" (36-32=4 :p)
Start with this:
Create a drawable:
<?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="#408DAA"/>
</shape>
</item>
<item >
<shape android:shape="rectangle">
<solid android:color="#50000000"/>
</shape>
</item>
<item android:top="10dip" android:bottom="10dip">
<shape android:shape="rectangle">
<solid android:color="#408DAA"/>
</shape>
</item>
</layer-list>
Use it as background of any view.
Adjust top, bottom and color="#408DAA" according your needs

How can I create a border around an Android LinearLayout?

I have one big layout, and one smaller layout inside of it.
How do I create a line border around the small layout?
Sure. You can add a border to any layout you want. Basically, you need to create a custom drawable and add it as a background to your layout. example:
Create a file called customborder.xml in your drawable folder:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<stroke android:width="1dp" android:color="#CCCCCC"/>
</shape>
Now apply it as a background to your smaller layout:
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/customborder">
That should do the trick.
Also see:
http://tekeye.biz/2012/add-a-border-to-an-android-layout
How to add border around linear layout except at the bottom?
Creat XML called border.xml in drawable folder as below :
<?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="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
then add this to linear layout as backgound as this:
android:background="#drawable/border"
Try this:
For example, let's define res/drawable/my_custom_background.xml as:
(create this layout in your drawable folder) layout_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:width="2dp" android:height="2dp"
android:color="#FF0000" />
<solid android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
<corners android:radius="1dp" android:bottomRightRadius="5dp"
android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
main.xml
<LinearLayout
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/layout_border" />
</LinearLayout>
Create a one xml file in drawable folder
<stroke
android:width="2dp"
android:color="#B40404" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners android:radius="4dp" />
Now call this xml to your small layout background
android:background="#drawable/yourxml"
This solution will only add the border, the body of the LinearLayout will be transparent.
First, Create this border drawable in the drawable folder, border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#ec0606"/>
<corners android:radius="10dp"/>
</shape>
Then, in your LinearLayout View, add the border.xml as the background like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/border">
you can do it Pragmatically also
GradientDrawable gradientDrawable=new GradientDrawable();
gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));
Then set the background of layout as :
LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);
I'll add Android docs link to other answers.
https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
It describes all attributes of the Shape Drawable and stroke among them to set the border.
Example:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#F00"/>
<solid android:color="#0000"/>
</shape>
Red border with transparent background.
Don't want to create a drawable resource?
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:minHeight="128dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="#android:color/white">
<TextView ... />
</FrameLayout>
</FrameLayout>
Try This in your res/drawable
<?xml version="1.0" encoding="UTF-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<padding android:left="15dp"
android:right="15dp"
android:top="15dp"
android:bottom="15dp"/>
<stroke android:width="10dp"
android:color="#color/colorPrimary"/>
</shape>
</item><item android:left="-5dp"
android:right="-5dp"
android:top="-5dp"
android:bottom="-5dp">
<shape android:shape="rectangle">
<solid android:color="#color/background" />
</shape></item></layer-list>
Nobody seems to be using the method we came up with so I thought I would share, as it involves much simpler code.
Simply nest one layout inside another with padding, and the outer one can be colored, making a frame.
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/llSignatureBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/black"
android:orientation="vertical"
android:padding="4dp">
<com.yourmom.mobiledriverapp.Controls.SignatureArea
android:id="#+id/signatureArea"
android:layout_width="#dimen/signaturearea_width"
android:layout_height="#dimen/signaturearea_height"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp" />
</androidx.appcompat.widget.LinearLayoutCompat>

Square shaped layout border with round inside edges

I’m attempting to create a layout border with corners that are square on the outside and round on the inside. I’ve gathered that I need to create an .xml drawable definition composed of two shapes: one with a stroke width and corner radius and another with a stroke width only:
The drawables
round_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="4dp" android:color="#FF000000" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="4dp" />
<solid android:color="#FFC0C0C0" />
</shape>
square_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#FF000000" />
<solid android:color="#FFC0C0C0" />
</shape>
Each of these works independantly as a border when appliedby itself like so:
android:background="#drawable/round_border"
but when they either or both are added to an item-list drawable like so:
composite_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<layer-list>
<item android:drawable="#drawable/round_border"/>
<!-- <item android:drawable="#drawable/square_border"/> -->
</layer-list>
</shape>
and:
android:background="#drawable/composite_border"
The layout's background is completely black instead of just a black border.
Does anyone know how to make the layer list work for this task?
From Shape Drawable Doc you can see that shape can't have layer-list inside so you should define your composite_border.xml like this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/square_border"/>
<item android:drawable="#drawable/round_border"/>
</layer-list>
note that I changed the order of your items inside the layer-list as stated in the documentation of layer-list
Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top and you want it to be squared from outside
Try this will work fine enough:
solid is background color
stroke is border
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/white"/>
<stroke android:width="1dp" android:color="#ffaaaaaa" />
<size
android:width="15dp"
android:height="15dp"/>
</shape>
Create a xml file like round_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#CCCC33"/>
<size
android:width="35dp"
android:height="35dp"/>
</shape>
In the layout set as background
<LinearLayout
android:id="#+id/layout_wellbeing"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="#drawable/rounded_corner_leuvan"
android:orientation="horizontal" >
</LinearLayout>
square_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp"
android:color="#FF000000"
/>
<solid android:color="#FFC0C0C0" />
</shape>
composite_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<layer-list>
<item android:drawable="#drawable/round_border"/>
<!-- <item android:drawable="#drawable/square_border"/> -->
</layer-list>
</shape>
watch out the comments and the quotation marks! =]

Categories

Resources