Caution The Android documentation seems to suggest that rounded corners are possible At least in releases 10 11 and 15 that is not the case The documentation also suggests that the instantiated Java class is Paint Drawable but the code returns a Color Drawa

To define one of these color rectangles, you define an XML element by the node name of drawable in any XML file in the /res/values subdirectory. Listing 3-14 shows a couple of color-drawable resource examples.

Listing 3-14. XML Syntax for Defining Color-Drawable Resources <resources>

<drawable name="red_rectangle">#f00</drawable> <drawable name="blue_rectangle">#0000ff</drawable> <drawable name="green_rectangle">#f0f0</drawable> </resources>

Listings 3-15 and 3-16 show how you can use a color-drawable resource in Java and XML, respectively.

Listing 3-15. Using Color-Drawable Resources in Java Code

// Get a drawable ColorDrawble redDrawable = (ColorDrawable)


//Set it as a background to a text view textView.setBackground(redDrawable);

Listing 3-16. Using Color-Drawable Resources in XML Code

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAlign="center" android:background="@drawable/red_rectangle"/>

To achieve the rounded corners in your drawable, you can use the currently undocumented <shape> tag. However, this tag needs to reside in a file by itself in the /res/drawable directory. Listing 3-17 shows how you can use the <shape> tag to define a rounded rectangle in a file called /res/drawable/my_rounded_rectangle.xml.

Listing 3-17. Defining a Rounded Rectangle

<shape xmlns:android=""> <solid android:color="#f0600000"/> <stroke android:width="3dp" color="#ffff8080"/> <corners android:radius="13dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />

You can then use this drawable resource as a background of the previous text-view example:

// Get a drawable

GradientDrawable roundedRectangle = (GradientDrawable)


//Set it as a background to a text view textView.setBackground(roundedRectangle);

0 0

Post a comment