Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 896
Nicolás de Ory Avatar asked Apr 12 '18 16:04

Nicolás de Ory


People also ask

How to change Text size of TextView in Android studio?

Go to File -> Settings, a new setting dialogue box will appear. Then go to Editor -> General. Now mark the checkbox Change font size with Ctrl + Mouse wheel and click on Apply button. Now to change your editor font size, you just have to press and hold Ctrl and rotate the Mouse wheel.

Can we use RelativeLayout inside ConstraintLayout?

In addition, guidelines can be used to create percentage-based layouts. Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout. However, learning the basics of LinearLayout and RelativeLayout is important before trying to understand how to use it with ConstraintLayout.


1 Answers

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.

like image 85
Happy Dev Avatar answered Oct 20 '22 00:10

Happy Dev