Android UI Design Layouts and Drawable Images

Customizing Android Application Styles and Themes

Style resources let your applications maintain a consistent look and feel by specifying the attribute values used by Views. The most common use of themes and styles is to store the colors and fonts for an application. You can easily change the appearance of your application by simply specifying a different style as the theme in your project manifest. To create a style, use a style tag that includes a name attribute, and contains one or more item tags. Each item tag should include a name attribute used to specify the attribute such as font size or color being defined. The tag itself should then contain the value, as shown in the skeleton code below:
<?xml version=”1.0” encoding=”utf-8”?>
<resources> <style name=”StyleName”>
<item name=”attributeName”>value</item>
</style>
</resources>


Styles support inheritance using the parent attribute on the style tag, making it easy to create simple variations.
The following example shows two styles that can also be used as a theme; a base style that sets several text properties and a second style that modifies the first to specify a smaller font.
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
<style name=”BaseText”>
<item name=”android:textSize”>14sp</item>
<item name=”android:textColor”>#111</item>
</style>
<style name=”SmallText” parent=”BaseText”>
<item name=”android:textSize”>8sp</item>
</style>
</resources>


Android NinePatch for Image Drawables

Drawable resources include bitmaps and NinePatch for stretchable PNG images. They are stored as individual files in the res/drawable folder. The resource identifier for a bitmap resource is the lowercase filename without an extension. The preferred format for a bitmap resource is PNG, although JPG and GIF files are also supported. NinePatch for stretchable images are PNG files that mark the parts of an image that can be stretched. NinePatch images must be properly defined PNG files that end in .9.png. The resource identifier for NinePatches is the filename without the trailing .9.png. A NinePatch is a variation of a PNG image that uses a 1-pixel border to define the area of the image that can be stretched if the image is enlarged. To create a NinePatch, draw single-pixel black lines that represent stretchable areas along the left and top borders of your image. The unmarked sections won’t be resized, and the relative size of each of the marked sections will remain the same as the image size changes.

Android UI Design Layouts

Layout resources let you decouple your presentation layer by designing user-interface layouts in XML rather than constructing them in code. The most common use of a layout is to define the user interface for an Activity. Once defined in XML, the layout is “inflated” within an Activity using setContentView, usually within the onCreate method. You can also reference layouts from within other layout resources, such as layouts for each row in a ListView. Using layouts to create your screens is best-practice UI design in Android.

The decoupling of the layout from the code lets you create optimized layouts for different hardware configurations, such as varying screen sizes, orientation, or the presence of keyboards and touch screens. Each layout definition is stored in a separate file, each containing a single layout, in the res/layout folder. The filename then becomes the resource identifier. A thorough explanation of layout containers and View elements is included in the next chapter, but as an example, the following code snippet shows the layout created by the New Project Wizard. It uses a LinearLayout as a layout container for a TextView that displays the “Hello World” greeting.
<?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”>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Hello World!”
/>
</LinearLayout>


Related Topic How to Design Tweened and Frame by Frame Animations
What are All the Main Components of Creating Resources
How to Add Facebook Layout or Wallpapers to your Profile
How to Organize Android Build System Project Structure
Purpose of res/ Resource Directory and It's Sub-directories

nScraps.com © 2011   Privacy Policy  Terms of Service  Feedback