Android Expandable/Collapsible Views

Expandable/Collapsible Views (text, images, etc.) in Android are useful when we want to display all the available options on one screen, without the user having to scroll all the way to find the one he/she is interested in. What we are doing is trying to maximize the use of the available space on the screen by giving an overall view of its content. The Android SDK provides the ExpandableListView class, but sometimes that option is overkill or unfit for our intents & purposes, and we may decide to roll out our own mechanism.

help-content1 help-content2

This example uses a TextView, but any View can be expanded/collapsed the same way.

<!-- activity_info layout file -->
<!-- Clickable title -->
<TextView   android:id="@+id/help_title_gest"

<!--content to hide/show -->
<TextView   android:id="@+id/txt_help_gest"

Notice that we have included the onClick handler in the XML, and that we are using styles. A quick reminder: if you are doing serious Android development, and are using neither styles, includes nor fragments to maximize re-usability, you are creating your own hell.

That said, writing the corresponding code is pretty straightforward:

// inside Activity
 TextView txt_help_gest;

 protected void onCreate(Bundle savedInstanceState) {

    txt_help_gest = (TextView) findViewById(;
    // hide until its title is clicked

* onClick handler
public void toggle_contents(View v){
      txt_help_gest.setVisibility( txt_help_gest.isShown()
                          ? View.GONE
                          : View.VISIBLE );

Note that we used View.GONE and not View.INVISIBLE. We would usually want to use the former for two main reasons.  Firstly, because  we wouldn’t want our expandable View to take up space while invisible. Secondly, using View.GONE provides at least to some extent an expanding/collapsing effect, since the space occupied expands & retracts along with its contents.

Now, if we wanted to go even further with visual effects, we would use Android animations, in this case an up & down sliding effect. We already wrote about animations in an earlier article, so we’ll skip the details here. The reader can also consult this article by Ravi Tamada for an excellent rundown of different visual effects.

For the slide-down effect, we can create  res/anim/slide_down.xml  this way:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=""

 android:toYScale="1.0" />


The down movement is illustrated by android:fromYScale going from zero to one in about 200 milliseconds on a linear change rate. Now we’ll write the corresponding method in an utility class Fx:

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;


 * @param ctx
 * @param v
 public static void slide_down(Context ctx, View v){

  Animation a = AnimationUtils.loadAnimation(ctx, R.anim.slide_down);
  if(a != null){
     if(v != null){

We’ll do the same for sliding up (left to the reader as an exercise), and our toggle method above will become:

* onClick handler
public void toggle_contents(View v){

    Fx.slide_up(this, txt_help_gest);
    Fx.slide_down(this, txt_help_gest);

That’s it. We now have a nice expandable/collapsible text section triggered by clicking on its title.

This article is also available at DZone and Java Code Geeks.


  1. #1 by inferKNOX on February 2, 2014 - 11:10 am

    I would like to implement this to a new project using Eclipse (Kepler), however, I’m completely new and am unsure which files you are editing, or why when I add the code to the and activity_main.xml, I get errors everywhere.
    Would you be willing to give a bit of a more in depth explanation on how to go about implementing this? Have looked at numerous expandable list views and this is the closest, visually, to what I’m trying to incorporate.

    • #2 by Aamir on May 13, 2014 - 1:20 pm

      Thanks very clear description very easy to understand

  2. #3 by Marurban on February 23, 2014 - 9:40 pm

    Great tip, but this line is unnecessary:
    txt_help_gest.setVisibility( txt_help_gest.isShown() ? View.GONE : View.VISIBLE );

  3. #4 by Mikael on May 3, 2014 - 2:50 pm

    thanks, proved usefull

  4. #5 by Bruno on June 11, 2014 - 6:25 pm

    Great Post thanks!
    But could you explain how u did the design of the layout. It looks great with the wallpaper and the textviews seperated with the lines….
    I am bad in layout designing😛

  5. #6 by SuperDuperNoob on June 19, 2015 - 3:31 am

    Thanks man! Will try this on the project I’m working on.

  6. #7 by Aurangzeb on September 30, 2015 - 11:17 am

    How to create Title on runtime this is a fix layout 6 or 7 .
    How do i use with adapter? Creating views on runtime .

  7. #8 by Tausif Shahbaz on September 15, 2016 - 7:56 am

    Hi. Can you please mention the style/txt_help code too. Thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: