aftab
aftab

Reputation: 1141

set Background Image and xml Resource

I am using the following below code to round the corners of RelativeLayout. I save this as mybackground.xml in drawable folder.
It's working fine for rounding the corner but the problem is that I also want to add an transparent image as a background of my RelativeLayout. How can I achieve both things? How can I use an image and a drawable xml (for rounding the corner) at the same time for a RelativeLayout ...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" android:padding="10dp">
   <corners android:bottomRightRadius="30dp"
            android:bottomLeftRadius="30dp"
            android:topLeftRadius="30dp"
            android:topRightRadius="30dp" />
</shape>

Upvotes: 6

Views: 20626

Answers (4)

Sabri Mevis
Sabri Mevis

Reputation: 2431

This one works. If you dont round corners of your pic you will not be able to see rounded corner too. Some friends talked about this in previous answers. They should round their image to see round effect.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/listviewback2">
        <shape android:shape="rectangle" android:padding="10dp">
            <corners
                android:bottomRightRadius="35dp"
                android:bottomLeftRadius="35dp"
                android:topLeftRadius="35dp"
                android:topRightRadius="35dp"/>
        </shape>
    </item>
</layer-list>

Upvotes: 2

Sankar Ganesh PMP
Sankar Ganesh PMP

Reputation: 12027

use Layer List and Item tag for setting Image and use solid tag and set the color as #AA000000 for transparent as shown below

<?xml version="1.0" encoding="utf-8"?>
<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" android:padding="10dp">
      <solid android:color="#AA000000"/>
      <corners android:bottomRightRadius="30dp"
               android:bottomLeftRadius="30dp" 
               android:topLeftRadius="30dp"
               android:topRightRadius="30dp" />
    </shape>
  </item>
  <item>
    <bitmap android:src="@drawable/yourfilename"/>
  </item>
</layer-list>

Upvotes: 18

Henry Pootle
Henry Pootle

Reputation: 1216

You can use Layer-List

It will be 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" android:padding="10dp">
    <corners android:bottomRightRadius="30dp"
             android:bottomLeftRadius="30dp" android:topLeftRadius="30dp"
             android:topRightRadius="30dp" />
    </shape>
  </item>
  <item><!-- your transparent image --></item>
 </layer-list>

Upvotes: 6

jamapag
jamapag

Reputation: 9318

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<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" android:padding="10dp">
            <corners android:bottomRightRadius="30dp"
                android:bottomLeftRadius="30dp" android:topLeftRadius="30dp"
                android:topRightRadius="30dp" />
        </shape>
    </item>
   <item>
       <bitmap android:src="@drawable/background"/>
   </item>
</layer-list>

Upvotes: 1

Related Questions