Over a million developers have joined DZone.

Adding a Foreground Selector to a View/ViewGroup on Android

DZone's Guide to

Adding a Foreground Selector to a View/ViewGroup on Android

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Everyone has already seen the Android cards touch feedback. The selector is drawn in the foreground instead of in the background (as we usually implement it). This effect is in fact pretty simple to implement, and is already implemented in some cases.

Here is a screenshot of the press state effect in the Google Play app:

I. If Your View is a FrameLayout

This is the easiest way to add a foreground selector because there is a method for that! Indeed, you just have to pass your selector as android:foreground in your xml or programmatically, calling setForeground(Drawable).

II. If Your View is Not a FrameLayout

Don’t worry, this is pretty simple. Basically, we just have to set right state to the selector (pressed, focused, etc.), set the bounds and draw it after the view itself. In that way, the selector will be drawn after, and as a consequence, over the view.

Changing the State

In the View class, a method is called each time the state of the view changes. This method is drawableStateChanged() (DOC HERE).

protected void drawableStateChanged() {



Updating the Drawable Bounds

A method is called each time the size of the view changes. This method is onSizeChanged(int, int, int, int) (DOC HERE).

protected void onSizeChanged(int width, int height, int oldwidth, int oldheight) {
  super.onSizeChanged(width, height, oldwidth, oldheight);

  mForegroundSelector.setBounds(0, 0, width, height);

Drawing the Selector

There are 2 cases:

1. Your view is not a ViewGroup

The selector has to be drawn after calling onDraw(Canvas canvas)

protected void onDraw(Canvas canvas) {


2. Your view is a ViewGroup

The selector has to be drawn after all his children, that means after calling dispatchDraw(Canvas canvas)

protected void dispatchDraw(Canvas canvas) {


Drawing an Animated Drawable

If your drawable is animated, there is a bit more to do. Let’s suppose that we have a selector with the attribute android:exitFadeDuration. That means when the selector changes its state, the old state will fade out.

We first have to move the draw method of the selector from onDraw() (for views) or dispatchDraw() (for ViewGroups) to the draw(Canvas) method, just like this:

public void draw(Canvas canvas) {


Then we have to Override jumpDrawablesToCurrentState to indicate our selector to do transition animations between states, and verifyDrawable to indicate the view we are displaying our own drawable.

protected boolean verifyDrawable(Drawable who) {
  return super.verifyDrawable(who) || (who == mForegroundDrawable);

public void jumpDrawablesToCurrentState() {

But what is doing jumpToCurrentState()? Let’s see a bit of source code, in the DrawableContainer class.

public void jumpToCurrentState() {
    boolean changed = false;
    if (mLastDrawable != null) {
        mLastDrawable = null;
        changed = true;
    if (mCurrDrawable != null) {
    if (mExitAnimationEnd != 0) {
        mExitAnimationEnd = 0;
        changed = true;
    if (mEnterAnimationEnd != 0) {
        mEnterAnimationEnd = 0;
        changed = true;
    if (changed) {

We can notice that jumpToCurrentState() calls invalidateSelf(). And here is the invalidateSelf() method source code:

 * Use the current {@link Callback} implementation to have this Drawable
 * redrawn.  Does nothing if there is no Callback attached to the
 * Drawable.
 * @see Callback#invalidateDrawable
 * @see #getCallback() 
 * @see #setCallback(android.graphics.drawable.Drawable.Callback) 
public void invalidateSelf() {
    final Callback callback = getCallback();
    if (callback != null) {

We clearly see that if the callback is not set, the drawable won’t be redrawn. So let’s set a callback when we init our selector.

private void init(Context context) {
  mForegroundDrawable = getResources().getDrawable(R.drawable.myselector);

  //set a callback, or the selector won't be animated

Your selector should fade out now!


Retrieve the Default Background, and Set it as the Foreground

You can get the default background selector of your theme and set it as your foreground selector if you want:

TypedArray a = getContext().obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground});
mForegroundDrawable = a.getDrawable(0);
if (mForegroundDrawable != null) {

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}