Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (retired 2014) Pretty Little Things Positioning Text Using a Linear Layout

Jed Carey
Jed Carey
12,678 Points

A better solution?

When you are talking about centering the textView1 element, you suggest putting invisible views either side of it to make it centered. Would it not be a better solution to set gravity to "center horizontally" on the Linear layout?

I have only learnt about android development up to this video at this point. I could be very wrong, but doing this seemed to accomplish the same thing, without creating extra view elements.

1 Answer

I believe a part of this was already explained by Ben in the corresponding video.

So given that we have the text view inside the linear layout, with gravity set to center horizontally.

In linear layout the default orientation, or the point from where the elements start to align is the left side

If you simply center the text horizontally it will be centered no doubt but will still be shifted to the left side of the screen.

Adding dummy view placeholders on either side makes sure that the text is right in the middle of the screen, or as desired in the middle of the crystal ball, so that it gives you the effect that the text is floating inside of the crystal ball.

Again, there is no hard and fast like rule for this. But a simple solution would be to actually try it out and see for yourself what effect is any would occur if you omit the views

Hope this helps