Nicolás de Ory
Nicolás de Ory

Reputation: 622

Autosizing TextView in ConstraintLayout

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):

Upvotes: 10

Views: 3974

Answers (1)

Happy Dev
Happy Dev

Reputation: 101

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.

Upvotes: 3

Related Questions