android – round rect border for layouts-ThrowExceptions

Exception or error:

in my application I’m going to use a lot of layout “borders” which are a simple rounded rectangle. To test how it would look like I created a .png file with transparent background:

enter image description here

I just set the image as my layout background. The problem is that I need different proportions for other layouts and that would be a waste of time to create such images in Photoshop for all my layouts.

The questions: how can I do the same with the android API (maybe an XML shape) but allow the rectangle to be stretched to any dimension and still remain sharp?

How to solve:

You can use a drawable

Have bkg.xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <stroke
        android:width="3dp"
        android:color="#0FECFF"/>
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"/>
    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape>

Then set background

  android:background="@drawable/bkg" //to required view

Snasp shot of graphical editor when set drawable background to textview.

enter image description here

###

A Shape drawable should do it. For example:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp" />
    <solid android:color="@android:color/transparent" />
    <stroke android:width="2dp" android:color="#0000ff" />
</shape>

Leave a Reply

Your email address will not be published. Required fields are marked *