Shape Drawable

Shape Drawable resources let you define simple primitive shapes by defining their dimensions, back-

Each shape consists of a type (specified via the shape attribute), attributes that define the dimensions of that shape, and subnodes to specify padding, stroke (or outline), and background color values.

Android currently supports the following shape types as values for the shape attribute:

> oval A simple oval shape.

> rectangle Also supports a <corners> subnode that uses a radius attribute to create a rounded rectangle.

> ring Supports the innerRadius and thickness attributes to let you specify, respectively, the inner radius of the ring shape and its thickness. Alternatively, you can use innerRadiusRatio and/or thicknessRatio to define the ring's inner radius and thickness as a proportion of its width (where an inner radius of a quarter of the width would use the value 4).

Use the <stroke> subnode to specify an outline for your shapes using width and color attributes.

You can also include a <padding> node to offset the positioning of your shape on the canvas.

More usefully, you can include a subnode to specify the background color. The simplest case involves using the <solid> node, including the color attribute, to define a solid background color.

The following section describes the GradientDrawable class and how to specify a gradient fill for your Shape Drawables.

Listing 4-17 shows a rectangular Shape Drawable with a solid fill, rounded edges, 10dp outline, and 10dp of padding around each edge. The result is shown in Figure 4-3.

STING 4-17: A solid red Drawable resource

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="" android:shape="rectangle"> <solid android:color="#f0600000"/> <stroke android:width="10dp" android:color="#00FF00"/> <corners android:radius="15dp" /> <padding android:left="10dp" android:top="10dp"


Available for download on

Available for download on android:right="10dp" android:bottom="10dp"

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment