I have a fragment that inflates an xml layout. My requirement is to update the text size on all my views inside my fragment when my Activity is resumed. I tried
fragment.getView().invalidate();
which didn't seem to do the work. I also tried
fragment.getView().requestLayout();
which didn't work either.
On another activity, I have a ListFragment which needs to do the same thing. I tried
listfragment.getListView().invalidate();
which did the trick, refreshing my list view and redrawing all the items inside it.
I don't understand why one works but not the other.
I have also seen people recommending initiating a fragment transaction and replacing the current fragment with a new one, and it has kept me wondering
Why should I create a whole new fragment and replace my current fragment when all I need is to refresh the text on the views that my fragment contains.
Fragment transaction method will prevent me from defining my fragment in the layout xml of my activity and I will have to programatically insert the fragment at the right position.
Is there any simple approach to this?
TextView
like text size is not the sort of the thing you should need to callinvalidate()
for; the framework does this for you when the property changes. Perhaps show the code where you are updating the text properties; the problem is more likely there. The only time you should ever need toinvalidate()
a view manually is if you create a custom property Android doesn't know about. – GeorgeannageorgeannesetRawTextSize()
in the TextView sources (line 2406)...what gets called after the property is updated? android.googlesource.com/platform/frameworks/base/+/refs/heads/… Also, as an aside,invalidate()
does not force views to adjust themselves,requestLayout()
does that. – Georgeannageorgeanne