Phil
Phil

Reputation: 4069

Android Create Drawable XML with Image rounded top corners

I'm trying to create an Android XML Drawable that I can use as the background for one of my LinearLayouts. I need the background to have an image, with the top left and top right corners rounded with a 10dp radius. I've been trying with the code below, but just can not seem to get it working. Is this possible? Any help is greatly appreciated!!

<?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="10dp"
                 android:topRightRadius="10dp">                
        </corners>
    </shape>
</item>
<item>
    <bitmap android:src="@drawable/bg_navbar_blur" />
</item>    

Upvotes: 4

Views: 8113

Answers (2)

IAmTheSquidward
IAmTheSquidward

Reputation: 582

Try using px instead of dp:

<corners android:topLeftRadius="10px"
         android:topRightRadius="10px">                
</corners>

EDIT:

This is the complete XML that I am using to round edges in one of my elements:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <solid android:color="#99000000" ></solid>
   <corners android:radius="8px"></corners> 
   <stroke  android:width="0dp" android:color="#A4C2E0"></stroke>  
</shape>

EDIT 2:

I would try this. Put the image and it's layout (not rounded) in a new XML and place it in a drawable folder. Let's say it's named linearlayout_bg.xml... In your main layout, create a new LinearLayout and apply this following attribute:

android:background="@drawable/linearlayout_bg"

And then use your <corners code. Maybe that'll work?

Upvotes: 6

nouseforname
nouseforname

Reputation: 748

Try adding a solid element to the shape...

my sample:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#0f0" />
            <corners android:radius="30dp" />

        </shape>
    </item>

    <item>
        <bitmap android:src="@drawable/ic_launcher" />
    </item>
</layer-list>

Just tried this and it seems to work perfectly

Upvotes: 0

Related Questions