Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Understanding of the Android DrawingState

5.00/5 (2 votes)
8 Apr 2015CPOL2 min read 12.5K   105  
Demonstrate how to use the Android drawingstate

Introduction

Android DrawingState is the view drawing state which is maintained by the Android framework. The framework will update all view items DrawingState correspondingly in the view tree. Android views appearance are driven by the DrawingState in some aspects.

Using the Code

View will transfer the DrawingState to its properties which accept the StateList drawable, for example, View::background, TextView:textColor, ImageView::src, etc.

There are about 10+ kinds of DrawingState, but Android framework doesn't use them all, some of them are reserved for the future. There are some other differences in Android implementation besides the notation.

The below table only lists the most frequent DrawingState.

  Whether user can changed explicitly Transfer to all children(*)
state_activated Y (View::setActivated) Y
state_checked Y (Checkable::setChecked) N
state_selected Y (View::setSelected) Y
state_pressed Y (View::setPressed) Y
state_focused Y (View::requestFocus)(**) N
state_hovered Y (View::setHovered) N
state_enabled Y (View::setEnabled) N
state_checkable N N
state_activate N N
state_window_focused N N
state_single_line N N
state_multple_line N N

* When performing any DrawingState change on ViewGroup, whether these states are also transferred to all children of this ViewGroup.

** According to Android spec, the view only gets state_focused unless the current input mode is not touched, so state_focused may not take effect in most cases.

There are two properties which provide more control between the view parent and its children.

  • View::duplicateParentState: duplicates the drawing state from the direct parent
  • ViewGroup::addStateFromChildren: adds the drawing state from the direct child

TIPS: In Android native ListView & GridView, when performing a click on an item, the framework will update the clicking item’s DrawingState to state_checked or state_activated (depending on whether this item implements Checkable interface, then update state_checked, otherwise update state_activated.

There are 3 kinds of StateList drawables: ColorListDrawable, StateListDrawable, AnimatedStateListDrawable and 1 kind of StateList Animator: StateListAnimator.

TIPS: AnimatedStateListDrawable & StateListAnimator require Android v21+.

Refer to the MainActivity in the sample for the demo of StateListDrawable.

Refer to the SecondActivity in the sample for the demo of StateListAnimator.

Points of Interest

  1. Android may provide CheckedFrameLayout or CheckedLinearLayout, CheckedXXXLayout, which can persist the state_checked in ViewGroup, just like state_activated, state_selected, state_pressed, etc.
  2. Android may provide StateListStyle because there are many limitations currently. User can animate View::alpha, View::scaleX, View::rotate, etc. but can't animate View::visibility, ImageView::src, TextView::text, etc. (via. StateListAnimator)

History

  • 8th April, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)