9

I have a chain in a ConstraintLayout which consists in a TextView, an ImageView and another TextView (doesn't need autosizing). I'd like them three to be vertically centered in the parent container (i.e. same distance to left/right).

I want the TextView to autosize so the text doesn't overflow. I can't use autosize with a layout_width set to wrap_content (nothing happens and the text doesn't resize). I can't set a fixed layout_width either since the extra space when the text isn't very long throws off the alignment.

Is there anyway to do this via XML or should I implement my own autosizing behavior?

This is a picture of what I'm trying to achieve (a center-aligned chain):

Nicolás de Ory
  • 614
  • 9
  • 31

1 Answers1

2

For TextView Auto-sizing, use android:layout_width="0dp".

But make sure you apply the ViewComponents Left/Start and Right/End Constraints.

This trick is basically used instead of android:layout_width="fill_parent" in ConstraintLayout.

Using this, you'll achieve Auto-sizing and it will consume only the space falling under the specified constraints and no over-lapping and no spilling-out.

Happy Dev
  • 81
  • 4