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

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.

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" />