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 Creating the Screen Layout Adding a Button

Joe Di Trolio
Joe Di Trolio
3,367 Points

How does the XML code know where to place an object such as the "button" in the design?

We could achieve the same result as dragging the object by typing it in the 'text view', however, shouldn't there be coordinates in the code to tell the emulator where it should go? Thank you : )

2 Answers

Gnani Pasupula
Gnani Pasupula
3,915 Points

In XML you should use padding to move the widget(button) rather than dragging the widget in Design. Like this: Ex: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingTop="20dp" />

The XML only knows what you tell it. If you tell it you want a LinearLayout, it will give you a LinearLayout. As a default when you start putting items inside the LinearLayout it will put the first item in the top left, then the second item below the first. It orders the items in the same order you write it out in the XML text. Then you can configure where you want it placed exactly, create margins and paddings etc.

Different layouts have different rules to their behaviour. You really need to play around to get a grasp of it. Create a layout, try LinearLayout and RelativeLayout. Drop in a couple of items like buttons or text views then move them around in the design window. Then look at the text window to see how the written XML has changed.