android-autofittextview
android-autofittextview copied to clipboard
TextView line height in fragments is changing on page swapping
Hello! I have a problem with the following code
textView = new TextView(getContext());
textView .setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
// ...
AutofitHelper.create(textView)
.setMinTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
within Fragments (FragmentStatePagerAdapter).
On each fragment there are person names (textView) getting autofitted using your class. That works great until i reach another fragment where the height of the textview is getting changed. When i scroll back to the other fragment, the text size stays the same like before but the height of the textview is increased to the higher one. Looks like all textviews in the fragment will be normalized to the same height or even to the same line height...
Is there a way around that behavior?
Or is it a problem of the Fragment.SavedState instead of AutofitHelper?
I tried to requestLayout() at onPageSelected(int). That works at all but i would need to call requestLayout also on every getViewPager().setCurrentItem() because it is not calling onPageSelected. Maybe theres an easier/better way?
Thank you!
Best Regards, Juergen
Hello grantland!
Ok, i found a (dirty) workaround for now:
new Handler().postDelayed(new Runnable() {
public void run() {
if (textView != null)
textView.requestLayout();
}
}, 100);
Added this code into onViewCreate() of the fragment. On the broken fragments the textview's height is just jumping to the desired size, but at least i can read it.
As far as i found this has nothing to do with savedStates. I debugged the code a litle bit and the problem occures on dynamically invisible created fragments. i.e. setOffscreenPageLimit is set to (2). So, when loading the app, first 5 fragments are beeing loaded, 2 left of the current displayed fragment and 2 on right side after it. Those 5 fragments have no problem with autofittextview. But the 3rd in each direction is not correct measured. When swapping back to the ones that are showing correct initially, they get broken as well - as soon as they get removed by the viewpager and recreated.
If creating the AutoFitTextView() by code instead of using AutoFitHelper, the WRAP_CONTENT height of the textview is like on default height. The Text is correctly sized but cropped within the small view height.
How to avoid that dirty workaround and do it the right way? What did i miss?
Thanks again! Juergen
+1
I see this same issue with a view that's reused as part of a ListView. Inserting the workaround code suggested by @Juweii above (right after I re-set the text on the resized TextView) fixed the problem but is obviously not ideal. Note that I didn't need a delay:
new Handler().post(new Runnable() {
public void run() {
if (textView != null)
textView.requestLayout();
}
});
worked fine too.
Hello everyone.. Is there any other solution for this problem which is more ideal.