Wai Yan Hein
Wai Yan Hein

Reputation: 14771

Customizing XML drawable resource with image for background in Android

I am developing an Android app. In my app, I am opening an activity like a dialog. For that activity background, I want to set the image for whole background, but with border radius. So I created the background XML resource like this.

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

</layer-list>

I set that resource as background of LinearLayout. When I open activity, I get something like this:

enter image description here

As you can see there is no border radius at the corners. Besides, what I want to do is I also want to set the scaleType as cropCenter for the image. So is it possible to be done just in XML resource?

Upvotes: 2

Views: 503

Answers (1)

Chathura Jayanath
Chathura Jayanath

Reputation: 619

I can give you a better way, i have use this as an alternative. You can use cardView in android to make your corners with a round shape.

to add cardView use following in your gradle dependencies,

compile 'com.android.support:cardview-v7:25.1.1'

the in your activity that need to open as a dialog, change your xml as follows,

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_dialog"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="10dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/YOUR_IMAGE"/>

</android.support.v7.widget.CardView>

If you are using an activity that extend AppCompatActivity as below,

i.e. DialogActivity extends AppCompatActivity

you need to change your activity theme in android manifests like below,

<activity android:name=".DialogActivity"
            android:theme="@style/Theme.AppCompat.Dialog"></activity>

else

<activity android:theme="@android:style/Theme.Dialog" />

haaaaa, hard part is finished, so now the only thing you need to do is call

startActivity(new Intent(this, DialogActivity.class));

Upvotes: 1

Related Questions