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
Related
As shown in the image below i cannot clip the corners of a layer corresponding to another layer. Is there any way that it can be done that I am missing here? I want to clip anything that's going out of the corner radius.
Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/darkColor">
<shape android:shape="rectangle">
<solid android:color="#color/color_e95252" />
<corners android:radius="16dp"/>
</shape>
</item>
<item android:id="#+id/lightColor1"
android:bottom="40dp"
android:right="300dp">
<shape android:shape="rectangle">
<solid android:color="#color/color_ea5f5f" />
<corners android:radius="16dp"/>
</shape>
</item>
**The item below is kinda unnecessary**
<item android:id="#+id/lightColor2"
android:right="90dp"
android:top="-40dp"
android:bottom="-10dp"
android:left="10dp"
android:width="300dp"
>
**This is where the problem occurs**
<rotate android:fromDegrees="8">
<shape android:shape="rectangle">
<solid android:color="#color/color_ea5f5f" />
<corners
android:bottomRightRadius="300dp"
android:topLeftRadius="#dimen/dim_16"
android:bottomLeftRadius="50dp"/>
</shape>
</rotate>
</item>
Here you go, don't need that extra xml.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/darkColor">
<shape android:shape="rectangle">
<solid android:color="#color/appThemeColor" />
<corners android:radius="16dp"/>
</shape>
</item>
<item android:id="#+id/lightColor1"
android:bottom="30dp"
android:right="300dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
</shape>
</item>
<item android:id="#+id/lightColor2"
android:top="-50dp"
android:width="300dp"
>
<rotate android:fromDegrees="8">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
<corners
android:bottomRightRadius="250dp"
android:topLeftRadius="60dp"
android:bottomLeftRadius="50dp"/>
</shape>
</rotate>
</item>
</layer-list>
How can I give the custom border to TextView where:
1) left and right border is 2 dp and with the different color with little opacity
2) top and bottom border is 1 dp
3) The text of TextView should be visible. right now What I have created is not showing the text of textview
<?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:bottom="1dp"
android:left="2dp"
android:right="15dp"
android:top="1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#FFDDDDDD" />
<solid android:color="#00000000" />
</shape>
</item>
use alpha color according to your recruitment
<?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="#080808" />
</shape>
</item>
<item
android:bottom="2dp"
>
<shape android:shape="rectangle">
<solid android:color="#76d63f" />
</shape>
</item>
<item
android:bottom="2dp"
android:top="2dp"
>
<shape android:shape="rectangle">
<solid android:color="#d63f60" />
</shape>
</item>
<item
android:bottom="2dp"
android:top="2dp"
android:left="2dp"
android:right="2dp"
>
<shape android:shape="rectangle">
<solid android:color="#3fa9d6" />
</shape>
</item>
</layer-list>
<?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="#76d273" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#76d63f" />
</shape>
</item>
<item
android:bottom="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#d63f60" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
add this code in your drawable(test_drawable) file and set to textview
android:background="#drawable/test_drawable"
Check my code I have integrated the same.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
//for top and bottom borders
<item
android:left="-2dp"
android:right="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#fff70b" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
//for left and right border
<item
android:bottom="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#e90c0c" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
</layer-list>
Please note that opacity should be defined when creating your color only. While writing your color in colors.xml, you can change opacity like shown in below screenshot. Either change the 255 to your chosen opacity or use the bottom bar.
Also, you need to define the solid tag for each border and apply transparent color in order to make your TextView visible.
After applying the code in drawable, you will be able to see some preview like this :
Good Luck..!!
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:
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>
I'm trying to create a border around the whole layer-list and not on the individual shapes in the layer list. I'm following the code from here:
http://gisinc.com/talk/creating-reusable-custom-toggle-button-android-applications/
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="#dimen/settings_toggle_circle_diameter">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="#dimen/settings_toggle_circle_diameter"
android:width="#dimen/settings_toggle_circle_diameter" />
</shape>
</item>
<item android:left="#dimen/settings_toggle_circle_radius" android:right="#dimen/settings_toggle_circle_radius">
<shape android:shape="rectangle">
<solid android:color="#color/black"/>
<size
android:height="#dimen/settings_toggle_circle_diameter"
android:width="#dimen/settings_toggle_circle_diameter" />
</shape>
</item>
<item android:left="#dimen/settings_toggle_circle_diameter">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="#dimen/settings_toggle_circle_diameter"
android:width="#dimen/settings_toggle_circle_diameter" />
</shape>
</item>
</layer-list>
Basically I don't want a border around the individual ovals and rectangle but one border around the whole thing. Is there any way to do this?
I would like to suggest a different approach that I find easier...
<?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="5dp"/>
<padding android:left="15dp" android:right="15dp" android:top="15dp" android:bottom="15dp"/>
<solid android:color="#color/primary"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#color/secondary"/>
</shape>
</item>
<item android:gravity="center">
<bitmap android:src="#drawable/logo_splash"
android:gravity="center" />
</item>
</layer-list>
Three possible options
Using stroke attribute of Shape and some adjustment in logic
User Inner/Outer(border) shapes
Use Nine patch
I modified your code to show border
<?xml version="1.0" encoding="utf-8"?>
<!-- I have assumed 1 dp padding around all the shapes-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First Draw Left Outer circle(white) -->
<item android:right="22dp" android:left="0dp" android:top="0dp" android:bottom="0dp">
<shape android:shape="oval">
<solid android:color="#color/white"/>
<size
android:height="22dp"
android:width="22dp" />
</shape>
</item>
<!-- Draw Left Inner circle(black) -->
<item android:left="1dp" android:top="1dp" android:bottom="1dp" android:right="23dp">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
<!-- Draw Right Outer circle(white) -->
<item android:left="22dp" android:right="0dp" android:top="0dp" android:bottom="0dp">
<shape android:shape="oval">
<solid android:color="#color/white"/>
<size
android:height="22dp"
android:width="22dp" />
</shape>
</item>
<!-- Draw Right Inner circle(black) -->
<item android:left="23dp" android:right="1dp" android:top="1dp" android:bottom="1dp">
<shape android:shape="oval">
<solid android:color="#color/black"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
<!-- Draw Rectangle Center (black) -->
<item android:left="11dp" android:right="11dp">
<shape android:shape="rectangle">
<solid android:color="#color/black"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
<!-- Draw Rectangle Top edge(white) -->
<item android:left="11dp" android:right="11dp" android:bottom="21dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
<!-- Draw Rectangle Bottom edge(white) -->
<item android:left="11dp" android:right="11dp" android:top="21dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>