Tuesday, July 9, 2013

Implement custom shape for background

custom shape for background


To implement our custom shape, create xml file to define our shape in res/drawable/ folder, /res/drawable/myshape.xml.
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" >
    <solid 
        android:color="#e0e0e0" />
    <stroke 
        android:width="5dip" 
        android:color="#505050"/>
</shape>


Apply the shape on view with android:background="@drawable/myshape" in layout xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textStyle="bold|italic"
        android:layout_gravity="center_horizontal"
        android:background="#B0B0B0" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:gravity="center_horizontal"
        android:background="@drawable/myshape"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:gravity="center"
        android:background="@drawable/myshape" />

</LinearLayout>


No comments: