-1

I want to expand the view on clicking the + button and this + sign changes to - when the view expanded.Again when i click the - button the view should be shrink. Inside the view i have some TextView field.Please anyone suggest me.I am new to android.

  • 2
    Why don't you search in google ? – Nambi Jun 28 '17 at 10:27
  • I search but not found anywhere as i expected – Rishikesh Rahi Jun 28 '17 at 10:28
  • Welcome to StackOverflow. Please take the [tour](http://stackoverflow.com/tour) have a look around, and read through the [HELP center](http://stackoverflow.com/help), then read [How to Ask Question](http://stackoverflow.com/help/how-to-ask), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) and provide a [MCVE : Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). If people around can easily read and understand what you mean, or what the problem is, they'll be more likely willing to help :) – Dwhitz Jul 15 '17 at 16:30

2 Answers2

0

You can visible or gone particular layout on button click event at runtime like below code:

findViewById(R.id.yourButtonId).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainView.setVisibility(View.GONE);
            }
        });
Shreeya Chhatrala
  • 1,441
  • 18
  • 33
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

This answer solves the question.

public class ExpandableTextView extends TextView implements OnClickListener
{   
private static final int MAX_LINES = 5;
private int currentMaxLines = Integer.MAX_VALUE;

public ExpandableTextView(Context context)
{
    super(context);
    setOnClickListener(this);
}
public ExpandableTextView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    setOnClickListener(this);
}

public ExpandableTextView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    setOnClickListener(this);
}

@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
{
    /* If text longer than MAX_LINES set DrawableBottom - I'm using '...' icon */
    post(new Runnable()
    {
        public void run()
        {
            if (getLineCount()>MAX_LINES)
                setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_more_text);
            else
                setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

            setMaxLines(MAX_LINES);             
        }
    });
}


@Override
public void setMaxLines(int maxLines)
{
    currentMaxLines = maxLines;
    super.setMaxLines(maxLines);
}

/* Custom method because standard getMaxLines() requires API > 16 */
public int getMyMaxLines()
{
    return currentMaxLines;
}

@Override
public void onClick(View v)
{
    /* Toggle between expanded collapsed states */
    if (getMyMaxLines() == Integer.MAX_VALUE)
        setMaxLines(MAX_LINES);
    else
        setMaxLines(Integer.MAX_VALUE);
}

}
Abhi
  • 3,431
  • 1
  • 17
  • 35