Search code examples
androidandroid-custom-view

Android: custom View change size


I am using OnClickListener on my custom view and this code to enlarge view on click:

@Override
public void onClick(View v)
{
    if (!large)
    {
        this.getLayoutParams().height *= 2;
    } else {
        this.getLayoutParams().height /= 2;
    }

    large = !large;
}

but when I click the view nothing happend, view do not change size?


Solution

  • Chaneg your method like this..then it will work..

    @Override
    public void onClick(View view)
    {
    LayoutParams params = view.getLayoutParams();
         if (!large)
            {
                params.height = 2 * view.getHeight();     
            } else {
                params.height = view.getHeight()/2;
            }
            large = !large;
            view.setLayoutParams(params);
    }