how to expand only textview in android

dawood rizwan picture dawood rizwan · Jan 26, 2015 · Viewed 10.9k times · Source

i am trying to expand textview in android, i have textview with some bulk content, my requirement is to display only two lines from bulk content with finishing (...). if i click it, my full content will has to display. how to achieve this. could you please help me?

public class MainActivity extends ActionBarActivity {       
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);  
   t1 = (TextView) findViewById(R.id.text);
   String str = "If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled." +
                "I made the expandable list view acc to your tutorial but what i have to do if I want to populate Expandable List View with values from database?Means what changes I have to do in ";
    t1.setText(str);                               
    t1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        t1.setMaxLines(Integer.MAX_VALUE);
    }
 });
}
}

Answer

Ichigo Kurosaki picture Ichigo Kurosaki · Jan 26, 2015

You can set android:maxLines property of textview to 2 by default in your layout xml..

In your onCreate() function after setting the text to your textview (t1).
Add following

 boolean isTextViewClicked = false;

t1.setOnClickListener(new OnClickListener() {
    if(isTextViewClicked){
      //This will shrink textview to 2 lines if it is expanded.
       t1.setmaxLines(2);
       isTextViewClicked = false;
    } else {
       //This will expand the textview if it is of 2 lines
       t1.setMaxLines(Integer.MAX_VALUE);
       isTextViewClicked = true;
    }
});

This will expand the textview as per the content