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: .http://developer.android.com/reference/android/widget/ImageView.html#setSelected(boolean)

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(R.id.image_comment_btn);

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:

imgBtnComment.setSelected(true);

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=”http://schemas.android.com/apk/res/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”/>

</selector>

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:

imgBtnComment.setSelected(false);

Your comment button should now be blue.

 

Advertisements

Android for Beginners

If you browse the internet for android coding stuff, you will find a ton of documentation written in high-level gobblegobble that sometimes doesn’t make a lot of sense.

That’s probably because android docs and tutorials, for example, even on the authoritative site http://developer.android.com/, are more targeted at seasoned coders and not really for our mild beginners. Sometimes, you just need someone to do some translation for you on some of the methods android offers so that a beginner, like yourself (and myself, to an extent), can understand.

In the last 8 months where I have been learning android myself, I have made a lot of headway in understanding the code. I have also completed a coursera course on Programming Mobile Applications on Android Handheld Systems which ended in April 2014 and passed that with flying colours.

In the next few posts, I want to share my discoveries with the rest of the world and with all you lovely readers 🙂 – on what I have discovered on my Android Adventures. These posts will be written at a basic level so that beginners can gain a basic understanding of the concepts inside the vast system of Android.

Stay tuned for my Beginners: How-To Guide to Android.