Beginners: How-To Guide for ImageView’s setSelected() method

In this post I will discuss with the setSelected method from the ImageView class. Documentation for this class can be found here: .

In the WordPress android app, we see setSelected method in action as each time you touch the comment button to leave a comment in a post, the comment button turns from blue to orange. If you click on the orange comment button, it becomes blue again. 

This transition from one colour to another is actually completed using a very simple piece of android code. Firstly, we need to gets a references to the imgBtnComment (which is the blue comment button).

final ImageView imgBtnComment = (ImageView) getView().findViewById(;

Once you have that, you can use the line below to switch from the blue comment button to orange comment button once an event has occurred, such as a touch on the button, typically detected through the use of a listener. We use the following setSelected method code for the ImageView:


This will then tell android to use the orange comment button image as we are setting the setSelected to true and so it will look for the associated drawable with the attribute called “state_selected” . See xml for the drawable below:

<?xml version=”1.0″ encoding=”utf-8″?>
<selector xmlns:android=””&gt;

<item android:drawable=”@drawable/ic_comment_active” android:state_pressed=”true”/>
<item android:drawable=”@drawable/ic_comment_active” android:state_selected=”true”/>
<item android:drawable=”@drawable/ic_comment”/>


From the above, you can see that there are two drawables defined. One is for either “state_pressed” and “state_selected” and the other is the normal blue comment button / the one that is displayed when no one has touched it – therefore it is not “active”.

If you want it the imageView to appear as a blue comment button, you can use the following setSelected code:


Your comment button should now be blue.