Showing posts with label Layout Resources. Show all posts
Showing posts with label Layout Resources. Show all posts

Sunday, February 28, 2010

Layouts in Android

The way you construct the user interface in Android is pretty interesting. You can construct the UI widgets programmatically. But Android presents a decent way to construct the UI which is XML-based layouts.

The layout of an activity can be constructed by a XML file. These files are considered resources as they reside in res/layout folder. Each xml file consists of the declarations of widgets and their containers. The xml file is constructed in a hierarchical way, there is a tag that defines the layout of the widgets, inside this tag there are nested xml tags that define widgets, each widget tag has attributes that define the widget’s properties.

Let’s take a look at a simple layout xml file called main.xml which resides in res/layout directory:


android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Linear Layout"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is a button 1"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is a button 2"

/>






This layout defines a textView and a button. We see that the root xml node is
node which defines the layout of all controls inside it.
We use the following code to construct the UI based on this xml definition
setContentView(R.layout.main);
Also the TextView node contains attributes like
layout_width
which can have the values fill_parentwhich denotes the the width of the control should occupy all the available width, also it could have the value wrap_contentwhich denotes the control should occupy only width according to the width of its content which is the text inside the textView

The Button node has an extra attribute which is the android:id attribute, why this attribute is used in the button and not used in the TextView? The answer is that the TextView represents a label (just a static text to display) so there is no need to give it an id cause we will not reference it in the code, however if we need to display dynamic text in the TextView we can give it an id
To reference a control from the code we do it like this:
Button btnLaunch;
btnLaunch=(Button)findViewById(R.id.btnLaunch);

notice that the android:id attribute should be in the format of @+id/Control’sID as shown above, if you write it like android:id=”btnLaunch" the IDE would denote an error and you won’t be able to reference the control from the code.

Why to use XML-layouts and when to construct the layout programmatically ?
  • Using xml layouts achieves separation between the interface and the application logic, instead of writing a bulk of code that constructs the UI, defining it as XML is easier and less confusing.
  • You can save using code for constructing more complicated UI such as populating check-box columns in a grid.
  • It is a trend to construct the UI with XML definitions like in Microsoft’s XAML or even HTML, so people who are familiar with technologies like these would find it easy to deal with Android.
Finally we’re going to explain in more details all about layout containers and controls


We have many layout forms:
  1. Linear layout: manages controls in horizontal or vertical way. 
  2. Table layout: manages the controls in a group of columns and rows.
  3. Relative layout: manages the controls relative to one another or to the parent.
  4. Absolute layout: manages the controls in absolute position by coordinates.
  5. Frame layout: manages the controls in a dynamic way.
  6. Scroll layout: manages the controls in a scrollable layout.

Saturday, February 27, 2010

Android Layout properties

Here is a list of some of the layout properties used in different layout managers in android


Property

Possible Values

Notes

android:layout_width

  • "wrap_content”:adjusts the size of the control to fit according to its contents. 
  • “fill_parent”: adjusts the size of the content to fit all the available space of the parent.
  • numeric value of pixels(px), points(pt), inches(in), millimeters(mm), Density(dp) density-independent pixels based on 160 dpi (dot per inch), Scale (sp) Scale-independent pixels (dimensions that allow for user sizing; helpful for use in
    fonts).


android:layout_height

Same as android:layout_width



android:orientation

Vertical or horizontal

Used as a property for the container in LinearLayout

android:layout_weight

Numeric value: determines the ratio by which widgets share empty soace in the container. The lower value occupies more space


android:layout_gravity

  • top:widget positioned at the top of the container.
  • Center: positioned at the center of the container both vertically and horizontally.
  • center_horizontal: positioned at the horizontal center of the container.
  • Center_vertical: positioned at the vertical center of the container.
  • Bottom: positioned at the bottom of the container.
  • fill_horizontal: grows the vertical size of the widget if needed, so it completely fills the container.
  • fill_vertical: grows the horizontal size of the widget if needed, so it completely fills the container.
  • fill: grows the size if needed of the widget both horizontally and vertically so it fills the container.
  • Clip_vertical: Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
  • Clip_horizontal: Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.


android:gravity

Same as android:layout_gravity

Used for the content of a widget

android:padding

  • Apply padding value to the four edges of the widget.
  • numeric value of pixels(px), points(pt), inches(in), millimeters(mm), Density(dp) density-independent pixels based on 160 dpi (dot per inch), Scale (sp) Scale-independent pixels (dimensions that allow for user sizing; helpful for use in
    fonts).


android:paddingTop

  • Applies padding value to the top of the widget.
  • Values same as android:padding


android:paddingBottom
  • Applies padding value to the bottom of the widget.
  • Values same as android:padding


android:paddingRight

  • Apply padding value to the right of the widget.
  • Values same as android:padding


android:paddingLeft
  • Apply padding value to the left of the widget.
  •  Values same as android:padding


android:layout_alignParentTop
True|false: used in relative layout, aligns the widget’s top edge with the top of the container

android:layout_alignParentBottom
True|false: used in relative layout, aligns the widget’s top edge with the bottom of the container

android:layout_alignParentLeft
True|false: used in relative layout, aligns the widget’s left edge with the left of the container

android:layout_alignParentRight
True|false: used in relative layout, aligns the widget’s right edge with the right of the container

Sunday, February 7, 2010

Relative layout

Relative layout lays out widgets based on their position relative to each other. You can place a widget in a position relative to another widget’s position or relative to the container.


As we said in relative layout widgets can be placed

  1. Relative to the container.
  2. Relative to other widgets
Relative to the container :
The widgets are placed in position relative to their container like by setting the following properties:
  • android:layout_alignParentTop|Bottom|Right|Left to true. This aligns the Top|Bottom|Right|Left side of the widget with the Top|Bottom|Right|Left side of the container.
  • android:layout_centerVertical: the widget should be positioned vertically in the center of the container.
  • android:layout_centerHorizontal: the widget should be positioned horizontally in the center of the container.
  • android:layout_centerInParent: the widget should be positioned both vertically and horizontally in the middle of the container.





<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_alignParentTop"
android:layout_alignParentTop="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_alignParentBottom"
android:layout_alignParentBottom="true"
/>


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_alignParentLeft"
android:layout_alignParentLeft="true"
/>



<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_alignParentRight"
android:layout_alignParentRight="true"
/>


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_centerVertical"
android:layout_centerVertical="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_centerHorizontal"
android:layout_centerHorizontal="true"
/>


&lt;Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_centerInParent"
android:layout_centerInParent="true"
/>

Position Relative to other widgets’ positions:
There are four properties that determine the position of the widget in relation to other widgets:
  • android:layout_above: the widget should be placed above the referenced widget. 
  • android:layout_below: the widget should be placed below the referenced widget.
  • android:layout_toRightOf: the widget should be placed to the right of the referenced widget.
  • android:layout_toLeftOf: the widget should be placed above the referenced widget.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/btn2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1 is below button2"
android:layout_below="@id/btn2"
android:id="@+id/btn1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3 below button 1"
android:layout_below="@id/btn1"
android:id="@+id/btn3"
/>


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/btn2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1 is to the right of button2"
android:layout_toRightOf="@id/btn2"
android:id="@+id/btn1"
/>

As we saw that there are properties that define the alignment of a widget relative to the container, there are also five properties that determine the position of the widget in relation to other widgets:
  1. android:layout_alignTop|Bottom|Right|Left: indicates that the widget’s Top|Bottom|Left|Right should be aligned with the Top|Bottom|Right|Left of the widget referenced in the property. 
  2. android:layout_alignBaseLine: indicates that the two widget’s baselines should be aligned.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/btn2"
android:layout_centerVertical="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1 is aligned to the top & Bottom of button2"
android:layout_alignTop="@id/btn2"
android:layout_toRightOf="@id/btn2"
android:layout_alignBottom="@id/btn2"
/>

Notes:
  • When you reference a widget in a relative layout property, you must assure that this widget has been already defined in the layout.
  • When you use the value fill_parent to assign athe height of a widget in a relative container, it could occupy all the available space so that any further controls defined in the xml file would not appear.
  • When referencing another control in relative layout property, we use the notation @id/widgetID”.

Saturday, February 6, 2010

Linear layout

Linear layout is like a box that contains controls inside it. Controls are drawn one after each other either horizontally or vertically according to the Orientation of the layout.


When dealing with Linear layout there are five properties that we can deal with:
  1. Oientation.
  2. Fill model.
  3. Weight
  4. Gravity.
  5. padding.
Orientaition:
Orientation property determines whether the controls would be put in a horizontal way like in a row or in a vertical way like a column. The layout orientation is set by the property android:orientation


Vertical orientation:



<button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="this is a button 1"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is a button 2"

/>




Horizontal Orientation


<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is a button 1"

/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is a button 2"

/>




If you want to set the orientation programmatically you can use this code
android.widget.LinearLayout mainLayout=new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
Fill Model:
The widgets inside linear layout have width and height properties. These properties can have three values:
  1. A numeric value in pixels or inches that gives the width or height properties an absolute value. 
  2. They can have the value wrap_content meaning the widget should occupy it’s natural size unless there is no space then android can use word wrap to make the widget fit.
  3. They can have the value fill_parent meaning the widget should occupy all the available space of the closing container.
To set the fill model programmitaclly use this code:
Button b=(Button)findViewById(R.id.btn);
b.setWidth(LayoutParams.WRAP_CONTENT);
b.setHeight(LayoutParams.FILL_PARENT);
This is an example to two buttons one with width set to fill_parent and the other set to wrap_content
Weight:
The weight property determines the ratio by which controls share free space. For example if we have two buttons and the weight of both is set to 1 (this is the default value) then the free space will be divided equally between them.
But if the value of the weight of one of them is 2 and the other is one, then the first button will occupy space half as that occupied by the second and so on.


<button
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="weight set to 2"
/>
<button
android:id="@+id/btn"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="weight set to 1"
/>


To set the weight of a widget programmatically it’s a little bit different, we use this code:
Button b=(Button)findViewById(R.id.btn);
LayoutParams params=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT,3);
b.setLayoutParams(params);

the widget does not have a method to set the weight directly, instead you define
LayoutParams params=new android.widget.LinearLayout.LayoutParams 
object and use the widget. setLayoutParams(params) method.
The LayoutParams class has many constructors including this one
public LinearLayout.LayoutParams (int width, int height, float weight)
Gravity:
By default widget are positioned in the top-left of the screen, but if you want to change this you can use the layout_gravity property


<button
android:layout_gravity="left"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="left">
/<button
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="center"
/>
<button
android:layout_gravity="center_vertical" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="center_vertical"
/>
<button
android:layout_gravity="center_horizontal" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="center_horizontal"
/>
<button
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="right"
/>

<button
android:layout_gravity="bottom"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="bottom"
/>




Or something like this


<button
android:layout_gravity="fill_vertical" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="fill_vertical"
/>
<button
android:layout_gravity="fill_horizontal" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="fill_horizontal"
/>
<button
android:gravity="right"
android:layout_gravity="clip_horizontal" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="clip_horizontal"
/>
<button
android:gravity="top"
android:layout_gravity="clip_vertical" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="clip_vertical"
/>



Notice that values clip_horizontal and clip_vertical are clear if only the android:gravity property is defined. If this property is not defined then the control will be clipped on both edges.

When using android:layout_gravity="clip_horizontal" with android:gravity="right" the control’s left edge will be clipped and if the android:gravity="left" the right edge will be clipped.

When using android:layout_gravity="clip_vertical" with android:gravity="top" the control’s bottom edge will be clipped and if the android:gravity="bottom" the top edge will be clipped.

So what is the difference between the android:layout_gravity and the android:gravity properties:

The android:layout_gravity sets the position of the view in the container, while android:gravity sets the position of the content of the view.


For example if the android:layout_gravity=”right” then the view would be placed in the right position in the container while if the view’s android:gravity=”right” then the text of the view would be placed at the right.


To set the android:gravity property programmatically you can use this code:

Button btn=(Button)findViewById(R.id.btn);
btn.setGravity(Gravity.RIGHT);

Padding:

The android:padding property sets the padding between widgets. if you specify the padding property to the container then the container with all of its widgets would be shifted by the value, if you specify it to a single widget then the contents of that widget would be shifted by the specified value.

If you use the android:padding property then this would apply the padding values to the four edges of the widget. If you need to be more specific you can use :
android:paddingTop or android:paddingLeft or android:paddingRight or android:paddingBottom


<button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="button 1"
/>
<button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingleft="40px"
android:text="button 2"
/>