Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Weather App (2015) Building the Weather UI Finishing the Lower Half

Instead of using gravity why not use layout_gravity?

I get the same results when I leave contents as wrapped and change layout_gravity to center (both). Is there a particular reason you chose to match_parent and use the gravity attribute? Any known bugs with my method or is it just a matter of personal preference?

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Michael

Gravity aligns the content inside the layout where layout_gravity aligns how the layout should fit inside it's parent. It definitely matters which ones you use, while it may look the same on your device or the emulator it may look completely different on say a 10" tablet.

A good explanation can be found here

http://stackoverflow.com/questions/3482742/gravity-and-layout-gravity-on-android

Hope this helps Daniel

Understood, but if you have that view set to wrap_content isn't layout_gravity going to position the view the same way it would if I had instead used match_parent and gravity?

Daniel Hartin
Daniel Hartin
18,106 Points

It may do for this situation but let's say your text in the text view is longer than one line or it is an imageview with a smaller image than the width of the screen. The text or image would be centered in the view but the view would default to align start leaving a potential large gap on the right. Like I say for this exercise it may well be fine but it is better to set your view planning for different data as it may change in the future and it's less then to worry about

Daniel

Okay thank you!