Click here to Skip to main content
14,028,325 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8K views
101 downloads
4 bookmarked
Posted 8 Apr 2015
Licenced CPOL

Understanding of the Android DrawingState

, 8 Apr 2015
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Yesy
Software Developer (Senior)
China China
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 5! Pin
jediYL17-Apr-15 17:31
professionaljediYL17-Apr-15 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 8 Apr 2015
Article Copyright 2015 by Yesy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid