Skip to content
Related Articles

Related Articles

Expandable TextView in Android

View Discussion
Improve Article
Save Article
  • Last Updated : 06 Apr, 2021

ExpandableTextView is an Android library which allows us to easily create a TextView which can expand/collapse when user clicks on it .we can use this feature in many apps such as movie review app or storytelling app and in many other apps. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Expandable TextView in Android Sample GIF


Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Before going to the coding section first do some pre-task

Go to app -> res -> values -> colors.xml file and set the colors for the app.


<?xml version="1.0" encoding="utf-8"?>
    <color name="colorPrimary">#0F9D58</color>
    <color name="colorPrimaryDark">#0F9D58</color>
    <color name="colorAccent">#05af9b</color>

Go to app -> res -> values -> strings.xml file and set the string for the app.


    <string name="app_name">Manabu-GT Expandable Text View </string>
    <string name="expandable_text">We Sanchhaya Education Pvt. Ltd., are registered and headquartered
        at BC 227, 2nd Floor, Matrix Business Tower, B4, Sector 132, Noida, UP-201301, hereinafter 
        referred to as GeeksforGeeks. We also offer paid Courses managed by Sanchhaya Classes Pvt. Ltd.
        with registered office address B-142, Vishwash Park, Uttam Nagar, New Delhi, North Delhi, Delhi, India, 110059.
        At GeeksforGeeks, we value your trust and  respect your privacy. This privacy statement (“Privacy Statement”)
        applies to the treatment of personally identifiable information submitted by, or otherwise obtained from, 
        you in connection with the associated application (“Application”). The Application is 
        provided by GeeksforGeeks (and may be provided by Geeksforgeeks on behalf 
        of a GeeksforGeeks licensor or partner (“Application Partner”). 
        By using or otherwise accessing the Application, you acknowledge that you accept the practices
        and policies outlined in this Privacy Statement.</string>

Go to Gradle Scripts -> build.gradle (Module: app) section and import the following dependencies and click the “sync Now” on the above pop-up.

implementation ‘’

Step 3: Designing the UI

In the activity_main.xml remove the default TextView and change the layout to RelativeLayout and add the ExpandableTextView  and inside it, we add a TextView and ImageButton as shown below. Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--  ExpandableTextView Container -->
        <!-- simple text view  -->
            android:textSize="16sp" />
        <!-- expandable ImageButton -->
            android:background="@android:color/transparent" />

Note: The id of ImageButton must be expanded collapse and id of TextView must be expandable text 

Properties of ExpandableTextView

  • expandableTextView:collapseDrawable: Customize a drawable set to ImageButton to collapse the TextView
  • expandableTextView:expandDrawable: It is used to set drawable to ImageButton to expand the TextView
  • expandableTextView:maxCollapsedLines: The maximum number of text lines allowed to be shown when the TextView gets collapsed (defaults value is 8 )
  • expandableTextView:animDuration: It is used to set the duration of the Animation for the expansion/collapse (defaults to 300ms)
  • expandableTextView:animAlphaStart: The alpha value of the TextView when the animation starts (NOTE) Set this value to 1 if you want to disable the alpha animation (defaults to 0.7f )

Step 4: Coding Part

Open the file and inside on Create()  create and initialize the expandable text view and setText to it from the strings.xml (R.string.expandable_text) as shown below 


// getting reference of  ExpandableTextView
ExpandableTextView expTv = (ExpandableTextView) findViewById(;
// calling setText on the ExpandableTextView so that 
// text content will be  displayed to the user

Below is the complete code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        // getting reference of  ExpandableTextView
        ExpandableTextView expTv = (ExpandableTextView) findViewById(;
        // calling setText on the ExpandableTextView so that
        // text content will be  displayed to the user


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!