How to show only top and bottom border - android

This is my XML code which works fine, the only problem is it's showing border on all four sides while I want only to show border on the top and on the bottom only. How can I do that?
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke
android:width="3dip"
android:color="#B1BCBE"
/>
<padding
android:bottom="0dip"
android:left="0dip"
android:right="0dip"
android:top="0dip"
/>
</shape>

Try the 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:bottom="5dp" android:top="5dp" >
// add android:right="5dp" and android:left="5dp" for border on left and right
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
Edit:
Modify the below according to your requirements by changing the color, stroke width and padding
<?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="5dip"
android:color="#B1BCBE"
/>
<padding
android:bottom="0dip"
android:left="0dip"
android:right="0dip"
android:top="0dip"
/>
</shape>
</item>
<item android:bottom="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Snap

use:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Describes the line -->
<item android:top="-1dp" android:bottom="-1dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#ffffff" />
</shape>
</item>
</layer-list>

Try this:
<padding
android:bottom="0dip"
android:left="-1dip"
android:right="-1dip"
android:top="0dip"
/>

Its best not to mention the height and width attributes in layer-list as height and width attributes of item is supported only from API 23.
You can use below Drawable that worked below Android 4.0 and above 5.0.
<?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/blue_light" /> <!-- this is stroke color-->
</shape>
</item>
<item android:bottom="5dp" android:top="5dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark_blue" /> <!-- this is main color-->
</shape>
</item>
</layer-list>

Refer this code -
<?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="#cccccc"/>
</shape>
</item>
<item android:top="2dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#efefef"/>
</shape>
</item>
</layer-list>

Related

Drawable using Stroke only on the rightside of the solid

How would I use a stroke on only the right side of my drawable WITHOUT using insets?
<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">
<size
android:width="20dp"/>
<solid android:color="#color/colorWhite" />
<stroke
android:width="1dp"
android:color="#color/colorPink">
</stroke>
</shape>
</item>
</layer-list>
Edit: Cannot use negative insets because of overlap issue
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#ff0000" />
</shape>
</item>
<item android:right="2dp">
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
or use padding:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#ff0000" />
<padding android:right="1dp"/>
</shape>
</item>
<item>
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
or:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#ffffff"/>
</shape>
</item>
<item android:gravity="right">
<shape>
<size android:width="1dp"/>
<solid android:color="#ff0000"/>
</shape>
</item>
</layer-list>
or:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-2dp"
android:left="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#android:color/white" />
<stroke
android:width="1dp"
android:color="#ff0000" />
</shape>
</item>
</layer-list>
You can achieve that by drawing a pink layer and then, drawing a white layer 1dp distant from the right border:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pink layer -->
<item android:width="20dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorWhite" />
</shape>
</item>
<!-- white layer. It'll cover the pink layer except the last dp -->
<item android:right="1dp" android:width="19dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorWhite" />
</shape>
</item>
</layer-list>
Maybe, you can adjust

How to draw a circular stroke inside a circle?

I'm trying to achieve this effect, but I can't make the white circular stroke inside the circle. In my implementation, the white stroke appears outside the circle
<?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="oval">
<corners android:radius="10dip" />
<stroke
android:width="5dip"
android:color="#ffffff" />
<solid android:color="#f50000" />
</shape>
</item>
</layer-list>
Expected output:-
I accomplished this using a <layer-list> and an <inset> oval shape:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#f50000"/>
</shape>
</item>
<item>
<inset
android:insetTop="5dp"
android:insetLeft="5dp"
android:insetRight="5dp"
android:insetBottom="5dp">
<shape android:shape="oval">
<stroke
android:color="#fff"
android:width="5dp"/>
</shape>
</inset>
</item>
</layer-list>
You can control the inset amount and the stroke width by changing the values in the second item
here you go. Just modify a little your file to have two items and add a padding on the second.
I hope it helps:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<solid android:color="#f50000" />
</shape>
</item>
<item
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp">
<shape
android:shape="oval">
<stroke
android:width="2dp"
android:color="#ffffff" />
</shape>
</item>
</layer-list>
You need an xml like this (optimized):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<solid android:color="#007" />
</shape>
</item>
<item
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp">
<shape
android:shape="oval">
<stroke
android:width="1dp"
android:color="#f00" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
</layer-list>
or xml like this (not optimized):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#007" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="8dp"
android:color="#android:color/transparent" />
<solid android:color="#f00" />
<size
android:width="45dp"
android:height="45dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="10dp"
android:color="#android:color/transparent" />
<solid android:color="#007" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
</item>
</layer-list>
for this result:

How to create EditText background %100 transparent with bottom line

I need to create Edittext drawable background with %100 percent transparent and has only colored bottom line 2dp. Is there any way to make this component.
The bellow code with constant background. I need transparent 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="#color/colorPrimary2" />
<padding android:bottom="2dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/background" />
</shape>
</item>
</layer-list>
Here you go:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" /> <!--background color -->
</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="#000" />
<!-- color and size of the border -->
</shape>
</item>
</layer-list>
There is color for a transparency in android. Try 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="#FFffffff" />
<padding android:bottom="2dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/background" />
</shape>
</item>
</layer-list>

how to set image in proper position in layers in layer-list?

I am trying to set image in center of circle like this
(trying it on android 4.1.2)
but when I run the app it shows image like this
but when i am writing code in xml it shows right image like
Here is the xml code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-1dp"
android:left="-1dp"
android:bottom="-1dp">
<shape>
<solid android:color="#50000000" />
<corners android:radius="50dp" />
</shape>
</item>
<item android:height="20dp" android:width="20dp">
<shape>
<corners android:radius="50dp" />
<stroke android:color="#color/white"
android:width="1dp"/>
<solid android:color="#50000000"/>
</shape>
</item>
<item android:width="8dp"
android:height="8dp"
android:gravity="center"
android:drawable="#drawable/news">
</item>
</layer-list>
You can try this,
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#50000000"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="1dp"
android:color="#color/colorAccent"/>
<solid android:color="#50000000"/>
</shape>
</item>
<item
android:bottom="5dp"
android:drawable="#drawable/news"
android:gravity="center"
android:left="5dp"
android:right="5dp"
android:top="5dp">
</item>
</layer-list>

Shadow-like drawable, using "layer-list"

Background
I'm trying to make a (fake) shadow below a view, so that the layers would be vertical in this manner:
a line of color "#43000000" with a height of "1dp"
below the line, a gradient that starts from the top with color "#1A000000" and ends with color "#00000000", and is of a height of "8dp".
The problem
For some reason, I can't draw the line correctly.
No matter what I do, the line for some reason takes the whole space, while the gradient seems fine.
What I tried
I tried using "line" as the shape and also "rectangle". I also tried using "stroke" instead of "solid", tried adding padding and margins...
Here's the XML of the drawable file:
<?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="1dp" />
<solid android:color="#43000000" />
</shape>
</item>
<item android:top="1dp">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#1A000000" />
<size android:height="8dp" />
</shape>
</item>
</layer-list>
Again, this is not the only thing I've tried. It's just my first attempt.
The question
How can I fix this XML ? what is causing it to happen?
Here's what I've found to be working:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="8dp">
<shape android:shape="rectangle" >
<size android:height="1dp" />
<solid android:color="#43000000" />
</shape>
</item>
<item android:top="1dp">
<shape
android:dither="true"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#10000000" />
<size android:height="8dp" />
</shape>
</item>
</layer-list>
Tried with this,may it fulfill your need with gradient
<?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="#e040fb"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#E1BEE7"/>
<corners android:radius="2dp" />
<gradient
android:angle="270"
android:endColor="#e1bee7"
android:startColor="#e040fb" />
</shape>
</item>
</layer-list>
You can change the heights in this and get what you want
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="10dp"
android:gravity="bottom">
<shape
android:shape="oval">
<solid android:color="#color/shadow_color" />
</shape>
</item>
<item android:right="20dp" android:left="20dp" android:bottom="12dp"
android:top="10dp">
<shape
android:shape="rectangle">
<solid android:color="#color/border_color"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:right="30dp" android:left="30dp" android:bottom="23dp"
android:top="20dp">
<shape
android:shape="rectangle">
<solid android:color="#color/base_color"/>
<corners android:radius="15dp"/>
</shape>
</item>
</layer-list>

Categories

Resources