Using Android API Level : 7

  • 0

Using Android API Level : 7

Category : Uncategorized

In this example we shall see a very simple ways to work with
Shape API from Android platform.

As shown below, the image depicts a rectangular shape of a text view
on screen. In this example there is a customized user defined shape
that is showing a text label on screen.

The main Activity class of this example has the onCreate method overriden
in order to set the background of the textview with a shape as drawable.

* This code is provided on “AS IS” basis without guarantee/warranty
* of any kind. Use of this code subjected to user’s own risk.
* @Author: Ishtek at
* @Date : 27th Feb 2013.
package com.example.customshapeexample;

import android.os.Bundle;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.textview_background);
TextView txtView = (TextView) findViewById(;

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(, menu);
return true;

This activity_main XML file is the configuration for using the with some parameters values.


Following configuration XML file is the descriptive way of defining
Shape with gradient, stroke, corners, padding etc.


For the text mentioned on the Button, following style can be used for
showing appropriate style items.

If every items of this example are setup properly, then the runtime
screen of this example would look something like this, as shown below:

By slightly altering background specific configuration file, the shape
of TextView’s background can be made as shown below:

Here the shape is changed to oval in the “textview_background.xml” file.

Leave a Reply