Tag Archive for TableLayout

User Interface: Layouts

Layouts are visual elements designed to control the distribution, position and dimensions of the controls that are inserted inside. These components extend to the base class ViewGroup, like many other container components, ie, able to contain other controls. In the next post we will see what kinds of offers android layout. The types are as follows.

FrameLayout

This is the simplest of all layouts of Android. A FrameLayout places all its child in line with its upper left corner, so that each control will be hidden by the next control (unless the latter have transparency). Therefore, commonly used to display a single control on the inside, as a container (placeholder) simple one replaceable element, such as a picture.
The components included in a FrameLayout may establish their propiedadesandroid: layout_width and android: layout_height, which may take the values ​​”fill_parent” (so that the child takes control the size of its layout container) or “wrap_content” (so that the child takes control the size of its content).
example:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
< EditText android:id= "@+id/TxtName"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"/>
 </ FrameLayout>

LinearLayout

The next Android layout in the level of complexity is the LinearLayout. This layout stacked one after the other child elements either horizontally or vertically as property set android: orientation.
As a FrameLayout, the elements contained in a can set its properties LinearLayout android: layout_width and android: layout_height to determine its size in the layout. But in the case of a LinearLayout, we have another parameter to play with, property android: layout_weight.

Example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>
   < Button android:id = "@+id/BtnAccept"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"/>
 </ LinearLayout >
This property will allow us to give the elements in the layout dimensions proportional between them. This is harder to explain than to understand with an example. If we include in a vertical LinearLayout two text boxes (EditText) and one of them will establish a layout_weight = “1″ and the other one layout_weight = “2″ will get the effect that the entire layout area was occupied by the two boxes text and also the second is double (ratio of weight properties) as high as the first.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1"/>
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1"/>
 </ LinearLayout >

 

Thus, despite the apparent simplicity of this layout proves to be versatile enough to be useful to us on many occasions.

TableLayout

A TableLayout allow us to distribute child elements in tabular form, defining the rows and columns needed, and the position of each component within the table.

The table structure is defined in a similar way as in HTML, ie, indicating the rows that make up the table (TableRow objects), and within each row of columns required, except that there is no object especially to define a column (something like unTableColumn) but directly insert the necessary controls within each component of TableRow and inserted (which can be a simple or even another ViewGroup) corresponds to a column of the table. Thus, the final number of rows in the table correspond to the number of items TableRowinsertados, and the total number of columns will be determined by the number of components of the row that contains more components.

Generally, the width of each column will correspond to the width of the largest component of that column, but there are a number of properties that will help us to change this behavior:

  • android: stretchColumns. Indicate the columns that can expand to absorb the space left by the other columns to the right of the screen.
  • android: shrinkColumns. Indicate the columns can be collapsed to make room for the rest of the columns that you can exit on the right of the palntalla.
  • android: collapseColumns. Indicate the columns of the table that you want to completely hide.

All these properties TableLayout can receive a list of indices of columns separated by commas (example: android: stretchColumns = “1,2,3″) or an asterisk to indicate that it should apply to all columns (example: android: stretchColumns = “*”). Another important feature is the possibility that a given cell can occupy the space of several columns of the table (colspan attribute similar to HTML). This property is indicated by android: layout_span specific component to be taken that space. Consider an example with several of these elements:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:stretchColumns="1">
   <TableRow>
      <TextView android:text= "Cell 1.1" />
      <TextView android:text= "Cell 1.2" />
   </TableRow>
   <TableRow>
      <TextView android:text= "Cell 2.1" />
      <TextView android:text= "Cell 2.2" />
   </TableRow>
   <TableRow>
      <TextView android:text= "Cell 3"
       android:layout_span= "2" />
   </TableRow>
</TableLayout>
RelativeLayout

A TableLayout to distribute child elements in tabular form, defining the rows and columns needed, and the position of each component within the table. The table structure is defined in a similar way as in HTML, ie, indicating the rows that make up the table (TableRow objects), and within each row of columns required, except that there is no object especially to define a column (something like unTableColumn) but directly insert the necessary controls within each component of TableRow and inserted (which can be a simple or even another ViewGroup) corresponds to a column of the table. Thus, the final number of rows in the table correspond to the number of items TableRowinsertados, and The last type of layout that we will see is the RelativeLayout. This layout allows you to specify the position of each element relative to its parent or any other element in the layout itself. Thus, by including a new element X can indicate for example that should be placed under the element Y and right-aligned layout father.

Let’s see this in the following example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"/>
   < Button android:id = "@+id/BtnAccept"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/TxtName"
     android:layout_alignParentRight="True"/>
 </ RelativeLayout >

 

In the example, the button is placed BtnAccept text box below TxtName (android: layout_below = “@ id / TxtName”) and right-aligned layout parent (android: layout_alignParentRight = “true”), and left a margin to the left of 10 pixels (android: layout_marginLeft = “10px”). Like these three properties, we have an endless RelativeLayout properties for each control placed right where we want. Here are the main [I think their names perfectly explain the function of each]. Position relative to other control:

  • android:layout_above.
  • android:layout_below.
  • android:layout_toLeftOf.
  • android:layout_toRightOf.
  • android:layout_alignLeft.
  • android:layout_alignRight.
  • android:layout_alignTop.
  • android:layout_alignBottom.
  • android:layout_alignBaseline.

Position relative to parent layout:

  • android:layout_alignParentLeft.
  • android:layout_alignParentRight.
  • android:layout_alignParentTop.
  • android:layout_alignParentBottom.
  • android:layout_centerHorizontal.
  • android:layout_centerVertical.
  • android:layout_centerInParent.

Options range (also available for other layouts):

  • android:layout_margin.
  • android:layout_marginBottom.
  • android:layout_marginTop.
  • android:layout_marginLeft.
  • android:layout_marginRight.

Spacing or padding options (also available for other layouts):

  • android:padding.
  • android:paddingBottom.
  • android:paddingTop.
  • android:paddingLeft.
  • android:paddingRight.

 

Seo Packages