mrtpk
mrtpk

Reputation: 1437

What happens to text in TextView object, when its visibility is set to View.GONE?

I have a TextView object in an activity(not a fragment) which I fills some text using SetText(). In some point of time, I change the visibility of the TextView object to View.GONE. After sometime, when TextView object's visibility is set to View.VISIBLE, the text set dynamically is not shown in TextView. I tried to understand what happens using Log, and found out that the content of TextView object is empty string. Hence my question, What happens to text in TextView object, when its visibility is set to View.GONE ?

synopsis:

fill text in textview using setText() --> change visibility to View.GONE --> change visibility to View.VISIBLE --> getText() returns empty string

question : why?

Upvotes: 2

Views: 1646

Answers (2)

Mohit Dixit
Mohit Dixit

Reputation: 196

The Textview is invisible, and it doesn't take any space for layout purposes. So there is no data loss,it just invisible with no space acquire for layout purposes. so it must be some other setText() operation in your code or may be you recreate TextView object.

Upvotes: 1

Shaishav
Shaishav

Reputation: 5312

The setVisibility() is a method of the View class and the docs says that when the visibility is set to View.GONE:

This view is invisible, and it doesn't take any space for layout purposes.

Hence, the View instance (TextView object here) is very much alive and thriving and there is no reason for its private fields (mText in case of TextView) to be destroyed. Its just invisible to the layout manager. In your case one of the culprits for the said behavior is a probable re-initialization of the TextView object (via a call to onCreateView() for instance).

Upvotes: 4

Related Questions