Android Shapes – Example

In Android you can use Shape Drawables to define background, borders and gradients for your Views. You find the possible types here.

To define a shape create a XML file in your drawable folder. For example:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <stroke android:width="2dp" android:color="#FFFFFFFF" />
     <gradient android:startColor="#DD000000" android:endColor="#DD2ECCFA" 
            android:angle="225"/> 

    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape> 
		

Assign it for example to a LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:background="@drawable/myshape">
	<EditText android:layout_width="fill_parent" android:text="1"
		android:layout_height="wrap_content" android:id="@+id/editText1"
		android:inputType="numberSigned"
		android:layout_margin="5dip" android:gravity="center">
		<requestFocus></requestFocus>
	</EditText>
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/button1"
		android:text="@string/start"></Button>
</LinearLayout>

And you get:

instead of

I hope this helps.

You can follow me on Twitter.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Android. Bookmark the permalink.

7 Responses to Android Shapes – Example

  1. Moritz Post says:

    There are actually more elements than just shapes to be used as drawables. Popular examples: 9-patches, state lists etc…

    The full list can be reviewed here: http://developer.android.com/guide/topics/resources/drawable-resource.html

  2. Lars Vogel says:

    @Moritz: Thanks for the pointer.

  3. Fairuz says:

    when I type a command android:background=”@drawable/myshape”, the error message in main.xml
    error!
    UnsupportedOperationException: null
    Exception details are logged in Window > Show View > Error Log

    where does the fault?
    thaks for ur answer before

  4. Lars Vogel says:

    @Fairuz I think that is a bug in the tooling. If you run the example (e.g. in the emulator) you should be fine.

  5. Matteo Luigi Riso says:

    Could I achieve the same result by code and not from XML? E.g.: I want to change the gradient randomly, each time my app starts (I know: it’s a dumb example).

  6. Fairuz says:

    is there any way to fix thats problem ?

Comments are closed.