..
At this point the guide have been presented many arguments and shows the code for the creation of all the main widget.
What is still missing is an in-depth on the positioning of these elements, which is carried through to layout.
We mentioned in the introductory chapters in this part of LinearLayout and guidance that we analyze in greater depth this layout and all others provided by Android.
And 'one of the layouts used in almost all applications Android. The praticolarità of this layout, as we have seen, is to arrange the items included in the Activity window one after another.
Depending on the value orientation of the vertical LinearLayout LinearLayout it comes to (the elements are arranged one after the other from top to bottom) and horizontal LinearLayout (the elements are arranged one after the other from left to right). In order to set the direction of the layout is sufficient, in the file. Xml is used, edit the following property with the value of vertical or horizontal:
android: orientation = ""
As mentioned components added to the layout are arranged in a vertical or orizzonatale depending on the orientation given to the layout. These elements are added to the window until there is space and as soon as it requires the insertion of an additional element, Android still trying to resize it to insert it into the screen. This behavior is limited to a threshold after which you simply add the items are not displayed on the screen.
We do not recommend the reader to never put excess elements in the window as the automatic resizing done by Android often distorts the GUI components included making unpleasant if not, indeed, very usable. In addition, the elements that Android, for lack of space, however, does not show screen reside in memory going to waste precious space.
To show an example of this, let's build a LinearLayout orizzonatale with three buttons, which will then add one quarter to show the scaling and the graphical interface that derives from it. Unlike what we have seen so far, we will create the GUI directly from Java code without the support of an external file. Xml. This is shown to the reader to present this different mode of operation (for convenience we import directly android.widget .*):
LinearLayout LinearLayout layout = new (this);
layout.setOrientation (LinearLayout.HORIZONTAL);
layout.setGravity (Gravity.BOTTOM);
Button button1 = new Button (this);
button1.setText ("Button 1");
Button button2 = new Button (this);
button2.setText ("Button 2");
Button button3 = new Button (this);
button3.setText ("Button 3");
layout.addView (button1);
layout.addView (button2);
layout.addView (button3);
setContentView (layout);
First we declare an object of type LinearLayout, then we set the horizontal orientation of the layout and finally we set the position (bottom of the screen) where we will place our objects. We declare, then three-button layout with addView to join them and finally we use the method to map the layout setContentView the Activity window.
We are now going to add a fourth button (lets you exercise for the reader to write code) to the layout and how we can see from the screenshot below showing the scaling transforms the button interface is not very professional:

| |
ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €. |
| |
CSS (Course)
Web Design and Accessibility according to W3C CSS and XHTML. Starting from 29 €. |
| |
Java (Course)
OOP Programming in Java SUN. -15% Discount until 12/10/2011. |