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>
Related
I'm trying to implement this view
As you can see it had a circle cutting to the left and right of the rectangle
I tried to use 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">
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp" />
<size
android:width="300dp"
android:height="200dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>
<item
android:left="-10dp"
android:right="290dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#00000000" />
</shape>
</item>
<item
android:left="290dp"
android:right="-10dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#00000000" />
</shape>
</item>
<item
android:left="10dp"
android:right="10dp"
android:top="50dp">
<shape android:shape="line">
<stroke
android:width="1dp"
android:color="#EEEEEE"/>
</shape>
</item>
</layer-list>
but the result was not showing the cutting circle to the left and right
Is there any way to implement these cutting without loosing the shadow effect of the cart
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
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
I'm trying to create a layer of 3 circles that are all inner. And inside I will have a image of flowers.
The background can change...
If it sat on a blue background it will be blue shades. So I need to somehow adjust the transparrencies to make this work.
The center MUST ALWAYS be white completely though. I then want to add my image in the center.
I created a layer list to achieve this but I can't get the circles to be transparent and I can only get two circles to appear?
How would I get the bitmap to appear in the center?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Larger circle in back -->
<item>
<shape android:shape="oval">
<solid android:color="#726"/>
<size
android:width="35dp"
android:height="35dp"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#00f"/>
<size
android:width="25dp"
android:height="25dp"/>
</shape>
</item>
<!-- Smaller circle in front -->
<item>
<shape android:shape="oval">
<!-- transparent stroke = larger_circle_size - smaller_circle_size -->
<stroke android:color="#android:color/transparent"
android:width="5dp"/>
<solid android:color="#f00"/>
<size
android:width="10dp"
android:height="10dp"/>
</shape>
</item>
</layer-list>
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="#b5ed9259" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="5dp"
android:color="#android:color/transparent" />
<solid android:color="#cff27527" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="oval">
<stroke
android:width="5dp"
android:color="#android:color/transparent" />
<solid android:color="#fbda5807" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<shape android:shape="oval">
<stroke
android:width="5dp"
android:color="#android:color/transparent" />
<solid android:color="#FFFFFF" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:drawable="#drawable/ic_fav"
android:gravity="center"
android:left="5dp"
android:right="5dp"
android:top="5dp"/>
</layer-list>
OUTPUT
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#56ECFC"/>
<size android:width="400dp" android:height="400dp"/>
<stroke android:color="#62DEF5" android:width="30dp"/>
<stroke android:color="#4CCCF7" android:width="25dp"/>
</shape>
</item>
`
try this using below code you can draw 4 circles
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- Larger blue circle in back -->
<item
>
<shape android:shape="oval">
<solid android:color="#00f"/>
<size
android:width="30dp"
android:height="30dp"/>
</shape>
</item>
<!-- Smaller red circle in front -->
<item
>
<shape android:shape="oval">
<!-- transparent stroke = larger_circle_size - smaller_circle_size -->
<stroke android:color="#android:color/transparent"
android:width="5dp"/>
<solid android:color="#f00"/>
<size
android:width="25dp"
android:height="25dp"/>
</shape>
</item>
<item android:top="2dp"
android:right="2dp"
android:bottom="2dp"
android:left="2dp"
>
<shape android:shape="oval">
<!-- transparent stroke = larger_circle_size - smaller_circle_size -->
<stroke android:color="#android:color/transparent"
android:width="5dp"/>
<solid android:color="#f000"/>
<size
android:width="20dp"
android:height="20dp"/>
</shape>
</item>
<item
android:top="5dp"
android:right="5dp"
android:bottom="5dp"
android:left="5dp"
>
<shape android:shape="oval">
<!-- transparent stroke = larger_circle_size - smaller_circle_size -->
<stroke android:color="#android:color/transparent"
android:width="5dp"/>
<solid android:color="#fff0"/>
<size
android:width="20dp"
android:height="20dp"/>
</shape>
</item>
</layer-list>