How do I make a horizontally stretchable background in android

53 views Asked by At

I want to make a button background like this

enter image description here

So that when the content gets longer the background only gets stretched form the central region and the circular sides stay CIRCULAR.

enter image description here

I tried to make a 9-patch without a vertical stretch region but the circular sides went oval like in different sizes as you can see in the picture below.

enter image description here

when I specify a vertical stretch region in the center, the background becomes like this which I don't want it to. this is an square with rounded corners.

enter image description here

I also have to say that this background is basically a pink border which has a pink drop shadow both on the inside and the outside. I couldn't find a way to implement this with android drawable as there is no efficient way to have a good shadow effect using it

I can't think of a clean solution for doing so. please help me.

2

There are 2 answers

0
mohammad reza sarsarabi On BEST ANSWER

Alright, I just made a custom view that extends FrameLayout and it has a customizable colored shadow and stroke.

You can also use it to draw an inner background in the center.

enter image description here enter image description here

you can add it in your project from my GitHub page.

https://github.com/mr-sarsarabi/NeonLightBackgroundLayout

1
Muhammad Farhan Arif On

Use the rectangle shape in xml drawable and keep the radius to max.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke
        android:width="3dp"
        android:color="#A5A5A5" />
    <corners android:radius="999dp" />

</shape>