How to set the Top Stroke of Button? - android

My question is really simple. That is about Button attrs.
I want to make Button which has only Top stoke. What can I set in layout xml ??
My xml file is
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid android:color="#android:color/transparent"/>
<stroke android:width="1dp" android:color="#color/btn_disable"
android:dashWidth="1dp" android:dashGap="0dp"/>
<padding android:left="9dp" android:top="2dp"
android:right="9dp" android:bottom="2dp"/>
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid android:color="#color/white_pressed"/>
<stroke android:width="1dp" android:color="#color/mint"
android:dashWidth="1dp" android:dashGap="0dp"/>
<padding android:left="9dp" android:top="2dp"
android:right="9dp" android:bottom="2dp"/>
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
<item>
<shape>
<solid android:color="#null"/>
<stroke android:width="1dp" android:color="#color/mint"
android:dashWidth="1dp" android:dashGap="0dp" />
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
</selector>

I cannot determine the exact shape you are looking for, but judging that you only want the top stroke, hide all other strokes, you can use a layer-list to achieve this.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="your_stroke_colour" />
<corners
android:radius="your_radius" />
</shape>
</item>
<item
android:top="1dp"
android:bottom="0dp"
android:left="0dp"
android:right="0dp">
<shape android:shape="rectangle">
<solid android:color="your_color" />
<corners
android:radius="your_radius" />
</shape>
</item>
</layer-list>

Related

Make a layout in drawable that has 3 borders of same color and one is different

Actually I made a drawable resource file for the background of my layout. I want that my layout should have 3 sides of same color and one side of different color.
In this piece of code, all sides of layout has red color. I want 3 should have red color and one should have white color.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<stroke android:color="#color/colorLightRed" android:width="1dp">
</stroke>
</shape>
You can try something like this . Its not the best solution i guess ..
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-2dp">
<shape>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<stroke
android:width="1dp"
android:color="#color/black30">
</stroke>
</shape>
</item>
<item
android:bottom="-2dp"
android:left="-2dp"
android:top="-2dp">
<shape>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<stroke
android:width="1dp"
android:color="#color/green">
</stroke>
</shape>
</item>
You have to create the Layer list in order to do that.Add your colors for Bottom,Top,Left and for right side.
<?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="#3AFB03" />
</shape>
</item>
<!-- This is for border bottom but
you can change this according to your need -->
<item android:right="4dp" >
<shape
android:shape="rectangle">
<!-- Set the background color of your layout here -->
<solid android:color="#000000" />
</shape>
</item>
<item android:left="4dp">
<shape android:shape="rectangle">
<solid android:color="#FFEB3B"/>
</shape>
</item>
</layer-list>
Try this way
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="2dp"
android:right="2dp">
<shape android:shape="rectangle">
<solid android:color="#00BCD4" />
</shape>
</item>
<item
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item
android:left="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#FFFF00" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp">
<shape android:shape="rectangle">
<solid android:color="#4CAF50" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp">
<shape android:shape="rectangle">
<solid android:color="#00BCD4" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
OUTPUT

Radius in making border is given from inside instead of outside?

I am using below XML for creating border for a TextView.
<?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/dark_gray" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark_gray" />
</shape>
</item>
<item
android:bottom="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark_gray" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<corners android:radius="#dimen/padding_4dp"/>
</shape>
</item>
</layer-list>
When I give radius as <corners android:radius="#dimen/padding_4dp"/>, it gives radius to corners but from inside, not from outside. So, as a result, corners remain sharp from outside. Like below:
Am I doing wrong somewhere?
Other items you have added in layer-list are missing corners, add it 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="#color/dark_gray" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark_gray" />
<corners android:radius="#dimen/padding_4dp"/>
</shape>
</item>
<item
android:bottom="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark_gray" />
<corners android:radius="#dimen/padding_4dp"/>
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<corners android:radius="#dimen/padding_4dp"/>
</shape>
</item>
For the remaining two item also add a corner radius.

Not all items in layer-list are showing up

I'm trying to implement a view like what's in this link here: https://imgur.com/a/6b75AMp
This is the layer-list I'm using:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesLightGreen"/>
</shape>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesBackground"/>
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
// The width and color of the border
<stroke
android:width="1dp"
android:color="#3C341F" />
// The desired corner radius. reduce it to keep it less rounded
<corners android:radius="10dp" />
<solid
android:color="#F0ECE0"
/>
</shape>
</item>
</layer-list>
However, the border doesn't show up. If I play around with the order of the items--for example, if I make the vertical stripe the last item--then the border might show up, but the vertical stripe won't. How do I get everything in the layer-list to show up properly?
try the following code you will get a hint
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesLightGreen" />
</shape>
</item>
<item android:left="4dp" android:top="4dp" android:right="4dp" android:bottom="4dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesBackground" />
</shape>
</item>
<item android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp" android:color="#3C341F" />
<corners android:radius="10dp" />
<solid android:color="#F0ECE0" />
</shape>
</item>
I have modified some of your code, use it and chech the output.
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke android:color="#000000" android:width="2dp"/>
</shape>
</item>

Custom shape in android using xml

I am trying to draw a custom shape which I can use as a background for my layout. But I am not able to do so. Is it possible to draw a shape as below using xml in android. I am not getting how to cut the semicircle shape from the vertical centres of rectangle.
Use layer-list to make this custom shape drawable.
/res/drawable/custom_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Transparent Rectangle -->
<item>
<shape android:shape="rectangle">
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp" />
<size
android:width="300dp"
android:height="100dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>
<!-- Left Half-Circle -->
<item
android:left="-10dp"
android:right="290dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#000000" />
</shape>
</item>
<!-- Right Half-Circle -->
<item
android:left="290dp"
android:right="-10dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#000000" />
</shape>
</item>
<!-- Middle Dotted Line -->
<item
android:left="10dp"
android:right="10dp">
<shape android:shape="line">
<stroke
android:width="1dp"
android:dashWidth="10px"
android:dashGap="10px"
android:color="#ff00"/>
</shape>
</item>
</layer-list>
USE:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_shape"/>
OUTPUT:
Hope this will help~
In your case it might be worth to create a resizable bitmap (9-Patch drawable). Follow this guide.
try bellow xml. save it in drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
<solid
android:color="#ffffff"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="1dp"
android:color="#000000"
/>
</shape>
It is better to use 9-Patch images. If you can create an image like above you can use https://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create 9-patch image(define expandable area).
Try this xml file in drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle">
<solid android:color="#color/primary" />
<corners android:radius="50dp"/>
</shape>
Please refer the below xml, it may help you out
<?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/black" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="200dp"
android:height="200dp" />
<solid android:color="#color/colorAccent" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="line">
<solid android:color="#android:color/black" />
<stroke android:width="1dp"
android:dashWidth="10px"
android:dashGap="10px"/>
</shape>
</item>
<item
android:bottom="103dp"
android:left="-10dp"
android:right="205dp"
android:top="103dp">
<shape android:shape="oval">
<size
android:width="3dp"
android:height="3dp" />
<solid android:color="#android:color/black" />
<stroke android:width="1dp" />
</shape>
</item>
<item
android:bottom="103dp"
android:left="205dp"
android:right="-10dp"
android:top="103dp">
<shape android:shape="oval">
<size
android:width="3dp"
android:height="3dp" />
<solid android:color="#android:color/black" />
<stroke android:width="1dp" />
</shape>
</item>
</layer-list>
Try below code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="3dip" />
<stroke
android:width="1dp"
android:color="#ED2A3C" />
</shape>

Creating a light button border

I want create in button with light button border with little 3D shade on edges like this
create an xml in your drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:right="2dp" android:top="2dp">
<shape>
<corners android:radius="0dp" />
<solid android:color="#A09D9D"
/>
</shape>
</item>
<item android:bottom="1dp" android:left="1dp">
<shape>
<gradient android:angle="270"
android:endColor="#color/white"
android:startColor="#color/white" />
<stroke
android:width="0.5dp"
android:color="#CBCBC9" />
<corners
android:radius="0dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
and use this xml as your button background
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#color/white" />
<stroke android:width="1dip" android:color="#color/gray">
</stroke>
</shape>
Set this Xml on background

Categories

Resources