• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How relayout view

andypaul1

Lurker
Dec 24, 2012
7
0
Hi,
I am tryout a very simple thing in an activity. It has three components laid out vertically in a linear layout - button, textview1, and textview2. All I want to do is to toggle visibility of textview1 when the button is clicked. So I have the following onclick handler for the button:

[HIGH]public void onClickMe(View view){
TextView thetext =(TextView)findViewById(R.id.thetext);

if(displayText){
thetext.setVisibility(TextView.INVISIBLE);
} else {
thetext.setVisibility(TextView.VISIBLE);
}
findViewById(R.id.optionView).invalidate();
thetext.invalidate();
findViewById(R.id.optionView).getParent().requestLayout();
//thetext.requestLayout();

displayText = !displayText;
}[/HIGH]
The textview does get visible and invisible but the problem is when the textview is made invisible it leaves a big gap between the button and the textview2. I would like the textview2 to be moved the the position where textview1 was. I think it is something to do with relayout but not sure what needs to be done here. I tried multiple things as shown in the code above. But it is not happening. What am I doing wrong?

thank you!
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones