Android Shape Border with Gradient

Android shape border with gradient

try something 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" >
<gradient
android:angle="360"
android:centerColor="#e95a22"
android:endColor="#ff00b5"
android:gradientRadius="360"
android:startColor="#006386"
android:type="sweep" />

<stroke
android:width="2dp"
android:color="#ff207d94" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<solid android:color="#fff" />
</shape>
</item>

</layer-list>

Android shape with gradient border and shadow

Here you go

Create your layout

<?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="match_parent"
android:background="#c8c0c0"
android:orientation="vertical">

<LinearLayout
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
android:background="@drawable/my_rectangle">

</LinearLayout>

</RelativeLayout>

Create my_rectangle.xml file inside drawable folder

<?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:angle="270"
android:endColor="#000000"
android:startColor="#FFFFFF" />
<corners android:radius="10dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#e95d11" />
<corners android:radius="10dp" />
<size
android:width="50dp"
android:height="50dp" />
</shape>
</item>
</layer-list>

The result

Sample Image

Notes

  1. I made it a 120 * 120 square, change the dimensions to make it rectangle
  2. I made the radius of the rounded corner as 10dp, change it if you want
  3. I made the padding as 5dp, you may change it as well

Cheers

is an android shape with a gradient border and transparent middle possible?

you can achieve this using jetpack compose then you can use interoperability API

@Composable
fun SetRing() {
Box(
modifier = Modifier
.background(Color.Transparent)
.border(
2.dp,
Brush.linearGradient(
colors = listOf(
Color.Yellow,
Color.Blue
)
),
shape = RoundedCornerShape(5.dp)
)
.padding(6.dp)
) {
Text(text = "Hiphop")
}
}

interoperability API,
in XML file

<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_interest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

[output]

Sample Image

Android Gradient border?

I understand that you want what can be described as a corona around the disk with the disk a solid color and the corona starting with the color black at the disk's edge and transitioning to transparency radially.

I think you will need to implement a drawable for the corona with a RadialGradient using the constructors that allows the specification of colors and color stops from the center of the disk.

Mark Allison has a blog post entitled RadialGradient – Gradients that discusses the use of RadialGradients and their implementation.

You can look at a similar implementation that is transparent in the center and starts with black at the edge of the disk and transitions to a separate color. You would have to compute the starting position of the black as a fraction of the overall size of the image.




The following is a custom drawable that produces the desired effect:

class CoronaDrawable : Drawable() {
private val mPaint = Paint()
private var mRadius = 0f
private val mColors =
intArrayOf(Color.TRANSPARENT, Color.TRANSPARENT, Color.BLACK, Color.BLACK and 0xFFFFFF)
private val mStops = floatArrayOf(0f, 0.85f, 0.85f, 1.0f)

override fun onBoundsChange(bounds: Rect) {
super.onBoundsChange(bounds)
mRadius = min(bounds.width(), bounds.height()) / 2f
mPaint.shader = shaderFactory(mRadius, mRadius, mColors, mStops)
}

override fun draw(c: Canvas) {
c.drawCircle(mRadius, mRadius, mRadius, mPaint)
}

override fun setAlpha(alpha: Int) {
}

override fun setColorFilter(filter: ColorFilter?) {
}

override fun getOpacity(): Int {
return PixelFormat.OPAQUE
}

private fun shaderFactory(centerX: Float, centerY: Float, colors: IntArray, stops: FloatArray) =
RadialGradient(
centerX, centerY, min(centerX, centerY), colors, stops, Shader.TileMode.CLAMP
)
}

which looks like this in the Android Studio layout designer as the background to a simple View:

Sample Image

The key take-away here is that the blank area in the center is set to transition from transparent to transparent or, otherwise, no change. This gives the empty center. At 85% of the radius the color abruptly changes to black and transitions to a transparent black.

For API 24+, you can place this drawable into a drawable resource file as follows and use it like a regular drawable.

corona.xml

<drawable class="com.example.radialgradientwithstops.CoronaDrawable"/>

You will have to determine how best to incorporate this drawable into your layer list or other means of presentation in your app.

Set gradient on stroke android

You should do something like this. Use layer-list with 2 shapes. First one is for gradient stroke and second one is for solid.

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="oval" >
<gradient
android:angle="360"
android:startColor="#543456"
android:endColor="#ff00b5"
android:type="linear" />
<size android:width="24dp"
android:height="24dp"/>
</shape>
</item>

<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="oval" >
<solid android:color="#fff" />
</shape>
</item>

</layer-list>

This code looks like this
Sample Image

How can I create gradient border color

Try This:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
<!-- create gradient you want to use with the angle you want to use -->
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:centerColor="@android:color/holo_blue_bright"
android:endColor="@android:color/holo_red_light"
android:startColor="@android:color/holo_green_light" />
<corners android:radius="@dimen/dp_10"/>

</shape>
</item>
<!-- create the stroke for top, left, bottom and right with the dp you want -->
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<!-- fill the inside in the color you want (could also be a gradient again if you want to, just change solid to gradient and enter angle, start, maybe center, and end color) -->
<solid android:color="#fff" />
<corners android:radius="@dimen/dp_10"/>
</shape>
</item>

</layer-list>

chose the color you want..

Android: gradient in border of button

I finilly decided to use nine patch images.

Android, Create gradient border?

Customized as per your design, create a drawable file:

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="8px">
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#08000000" />
<corners android:radius="4dp" />
<padding
android:bottom="3px"
android:left="3px"
android:right="3px"
android:top="3px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#09000000" />
<corners android:radius="4dp" />
<padding
android:bottom="2px"
android:left="2px"
android:right="2px"
android:top="2px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#10000000" />
<corners android:radius="4dp" />
<padding
android:bottom="2px"
android:left="2px"
android:right="2px"
android:top="2px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#11000000" />
<corners android:radius="4dp" />
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#12000000" />
<corners android:radius="4dp" />
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#13000000" />
<corners android:radius="4dp" />
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#14000000" />
<corners android:radius="4dp" />
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#15000000" />
<corners android:radius="4dp" />
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#16000000" />
<corners android:radius="4dp" />
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#17000000" />
<corners android:radius="4dp" />
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px" />
</shape>
</item>
</layer-list>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#FFF" />
<corners android:radius="4dp" />
<padding
android:bottom="15dp"
android:left="15dp"
android:right="15dp"
android:top="15dp" />
</shape>
</item>
</layer-list>

And set as background of your layout

Create gradient border in android?

You can achieve this by using a layerlist and messing with the padding. You'll need 3 elements:

1: A border.xml shape, which is just a solid shape in the color of your border: border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000"/>
</shape>

2: The 'inner' shape, the shape where you want the border to appear around: inner.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ff00"/>
</shape>

3: A layer list, which will put these 2 on top of eachother. You create the border by setting the padding on the inner shape: layerlist.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/border"/>
<item android:drawable="@drawable/inner"

Specify here where you want to have a stroke(top, left, right, bottom)

android:top="3dp" android:right="0dp" android:bottom="3dp"
android:left="3dp" />

Set this as the background of your TextView, Layout etc. (where you want the stroke to appear)

Or just create a 9Patch image with the borders.



Related Topics



Leave a reply



Submit