Android Tips, Techniques and Theory

PART 1: Creating Content with XML with Android Studio (No programming required)

With Simple Recipes

Go Back To TOC


GUI Techniques

Qualify Resources

You can use qualify resources to declare specific resources for various things with Android:

By using qualify resources for specific purposes, you can tell Android OS to use the string constants as needed based on these qualifiers at runtime. It also allows for multi-languages supports.

Qualify Resource Examples:

Customizing Component States

You can create custom states for a button or other components by creating a series of XML selector files to represent the various states. This is also called State List Drawable which, as the name implies, is a list of states. A StateListDrawable is a drawable object defined in XML that uses several different images, colors, or gradients to represent the various “states” of a component (e.g., button). Depending upon the component, states include:

Each state is represented by an <item> element inside of their parent <selector> element.

 

SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

 

NOTE: You set either the true or the false option for each state. For example, if the state is enabled then choose “true” and if the state is disabled then choose “false” as the option.

 

SELECTOR ELEMENT:

SELECTOR ATTRIBUTES:
Except for the namespace (e.g., xmlns:android) attribute, the other attributes accept a Boolean value (true or false).

ITEM ELEMENT:

ITEM ATTRIBUTES:
Except for the drawable attribute (e.g., android:drawable), the other attributes accept a Boolean value (true or false).

NOTE: Each of the state is prefixed with android: which is not shown in the table to avoid redundancy and to save space.

State

True-if item is used when the object:

False-if item is used when the object:

state_pressed

is pressed (e.g., when a button is touched/clicked)

is in the non-pressed state (default)

state_focused

has input focus (e.g., when the user selects a text input)

Is in the non-focused state (default)

state_hovered

is being hovered over by a cursor

Is in the  non-hovered state (default)

state_selected

is the current user selection when navigating with a directional control

is not selected.

state_checkable

is checkable

is not checkable

state_checked

is checked

is un-checked

state_enabled

is enabled

is disabled

state_activated

is activated as the persistent selection

is not activated

state_window_
focused

app window is in the foreground

app window is in the background (e.g., notification shade is pulled down or a dialog appears)

NOTE: Often, the hovered drawable may use the same drawable as the focused state.

CAUTION: During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match" but simply the first item that meets the minimum criteria of the state. As a result, like CSS rules, it is best to place the DEFAULT state LAST in the list of <item> elements.

EXAMPLE 1:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- pressed state -->
<item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
  <!-- focused state -->
<item android:state_focused="true" android:drawable="@drawable/button_focused" />
  <!-- hovered state -->
<item android:state_hovered="true" android:drawable="@drawable/button_focused" />

  <!-- default state -->
<item android:drawable="@drawable/button_normal" />
</selector>

This layout XML applies the state list drawable to a Button:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />

EXAMPLE 2:

In this example, we will force a checkbox to behave like a star rating widget with various states implemented.

Notes states included in this example:

Order

Image

State

Comment

1

Star_rating_unchecked.png

Initial state

2

Star_rating_checked.png

When star is clicked on and released

3

Star_rating_checked_and_pressed.png

When star is pressed but is already checked

4

Star_rating_unchecked_and_pressed.png

When star is unchecked and is being pressed on

5

Star_rating_unchecked_and_focused.png

When star is unchecked and is receiving tab focus

6

Star_rating_checked_and_focused.png

When star is already checked and is receiving tab focus

 

EXAMPLE 3:

In this example, we will create a custom button with various states implemented.

Notes states included in this example:

Image

State

Comment

custom_button_normal.png

Initial state

custom_button_enabled.png

When the button  is clicked on

custom_button_disabled.png

When the button is disabled (programmatically)

 

Nine-Patch

There are times when you will want to use a SINGLE image that can scale without distortion. This is accomplished by a process called Nine-Patch.

Material Design

Introduced in version 5 (Lollipop), Material Design is a complete guide for visual, motion and interaction design across multiple platforms (app, Bootstrap) and devices and it has some backward compatible with older versions of Android using the AppCompat v21 support library. To find more information about Material Design, go to http://www.google.com/design/spec/style/color.html or https://www.materialpalette.com.

There are a series of XML file that are responsible for creating the themes:

There are two themes in the AndroidManifest.xml. One for the app (e.g., @style/AppTheme) and one for the default activity (e.g., @style/AppTheme.NoActionBar).

To see these themes CTRL+CLICK on either of them to open a resource file name styles.xml in the values folder that will show each of the theme within style tags. However, you will also see a comment to where you can customize your theme. 

The styles.xml inherits properties from its parent (e.g. Theme.AppCompat.Light.DarkActionBar). If you CTRL+CLICK on it, you will see the values.xml file that was automatically generated by the SDK. You don’t want to edit this file; however, like CSS, you can overwrite properties in this file by modifying the styles.xml file. In fact, the first three <item> tags in the styles.xml are overwriting colors (e.g., colorPrimary, colorPrimaryDark, colorAccent) in the values.xml:

Like the values.xml, if you CTRL+CLICK on a resource color (e.g., @color/colorPrimary) in the styles.xml, it will open the colors.xml file where you can see where the colors were defined.

EXAMPLE: Change Theme Colors