Search code examples
androidtextviewhorizontalscrollview

android - hide horizontal scrollview when textView is clicked


I'm new to android. I'm developing an app. I have a textView as the header and a horizontalscrollview with the content. When I press the textView I got the horizontalscrollview appears below.

Java Code for the above method...

final HorizontalScrollView hsc = (HorizontalScrollView) findViewById(R.id.dailyUseItemsHS);
            TextView tv = (TextView) findViewById(R.id.daily_use_items_header);

            hsc.setAlpha(0.0f);

            tv.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                        hsc.setVisibility(View.VISIBLE);
                        hsc.animate().alpha(1.0f);
                    }
        });
}

Now when I press the textview again I need the horizontalscrollview to hide.


Solution

  • Remove hsc.setAlpha(0.0f); and make the dailyUseItemsHS invisible in the view xml

    In your onClick Event :

      tv.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
             if(hsc.getVisibility() == View.INVISIBLE)
                 {
                     hsc.setVisibility(View.VISIBLE);
                 }
                 else if (hsc.getVisibility() == View.VISIBLE){
                     hsc.setVisibility(View.INVISIBLE);
                 }
            });