Wednesday, 1 April 2015

Types of Layouts in XML Android

Welcome to Android TechBuzz.

AbsoluteLayout

           AbsoluteLayout is based on the simple idea of placing each control at an absolute position.  You specify the exact x and y coordinates on the screen for each control.  This is not recommended for most UI development (in fact AbsoluteLayout is currently deprecated) since absolutely positioning every element on the screen makes an inflexible UI that is much more difficult to maintain.  Consider what happens if a control needs to be added to the UI. You would have to change the position of every single element that is shifted by the new control.
Here is a sample Layout XML using AbsoluteLayout.


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button 
     android:id="@+id/backbutton"
     android:text="Back"
     android:layout_x="10px"
     android:layout_y="5px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:layout_x="10px"
     android:layout_y="110px"
     android:text="First Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <EditText
     android:layout_x="150px"
     android:layout_y="100px"
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:layout_x="10px"
     android:layout_y="160px"
     android:text="Last Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
     <EditText
     android:layout_x="150px"
     android:layout_y="150px"
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
</AbsoluteLayout>

FrameLayout

        FrameLayout is designed to display a single item at a time. You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. I have created a simple XML layout using FrameLayout that shows how this works.
<FrameLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android">
 <ImageView 
  android:src="@drawable/icon"
  android:scaleType="fitCenter"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"/>
 <TextView
  android:text="Learn-Android.com"
  android:textSize="24sp"
  android:textColor="#000000"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:gravity="center"/>
</FrameLayout>
Here is the result of this XML.

LinearLayout

LinearLayout organizes elements along a single line. You specify whether that line is verticle or horizontal using android:orientation. Here is a sample Layout XML using LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <Button 
     android:id="@+id/backbutton"
     android:text="Back"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:text="First Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <EditText
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:text="Last Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <EditText
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" /> 
</LinearLayout>

RelativeLayout

RelativeLayout lays out elements based on their relationships with one another, and with the parent container.
<RelativeLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android">
 <Button 
   android:id="@+id/backbutton"
   android:text="Back"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
 <TextView
     android:id="@+id/firstName"
     android:text="First Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/backbutton" />
 <EditText
  android:id="@+id/editFirstName"
  android:width="100px"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/firstName"
  android:layout_below="@id/backbutton"/>
 <EditText
  android:id="@+id/editLastName"
  android:width="100px"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/editFirstName"
  android:layout_alignLeft="@id/editFirstName"/>
 <TextView
  android:id="@+id/lastName"
  android:text="Last Name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toLeftOf="@id/editLastName"
  android:layout_below="@id/editFirstName" /> 
</RelativeLayout>
TableLayout
         TableLayout organizes content into rows and columns. The rows are defined in the layout XML, and the columns are determined automatically by Android. This is done by creating at least one column for each element. So, for example, if you had a row with two elements and a row with five elements then you would have a layout with two rows and five columns
<TableLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android">
 <TableRow>
  <Button 
      android:id="@+id/backbutton"
      android:text="Back"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
 </TableRow>
 <TableRow>
  <TextView
      android:text="First Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_column="1" />
      <EditText
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
 </TableRow>
 <TableRow>
  <TextView
      android:text="Last Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_column="1" />
      <EditText
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" /> 
 </TableRow>
</TableLayout>

Alternate Layouts

           On the LinearLayout page I mentioned that Android will shrink elements when they don’t all fit on the screen. You can reduce the need for this by using alternate layouts for different screen orientations. So, for example, you have a LinearLayout that looks pretty good in Landscape but does not have the room it needs in Portrait mode. Before looking in the res/layout folder for your layout XML Android will check for one of these alternate layouts.
  • res/layout-land – The alternate layout for a landscape UI
  • res/layout-port – The alternate layout for a portrait UI
  • res/lauout-square – The alternate layout for a square UI
I am going to create a folder named layout-land under the res folder and place this XML under the new folder. The XML file should have the same name it has in the layout folder, in my case linear_layout.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <Button 
     android:id="@+id/backbutton"
     android:text="Back"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:text="First Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <EditText
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:text="Last Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <EditText
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" /> 
</LinearLayout>
In the Emulator you can press 7 on your numberpad to change between Landscape and Portrait orientations. When I switch to landscape I see the Layout from the layout-land folder.

Types of Images Formates

Image Formats for the Web

There are four basic formats you will find on the Web. 

  • .gif This is pronounced "jif" or "gif" (hard "G") depending on whom you speak to. I have always said "jif", like the peanut butter. This is an acronym for GraphicsInterchange Format.

  • The format was invented by Compuserve and it's very popular. The reason is that it's a simple format. It's a series of colored picture elements, or dots, known as pixels, that line up to make a picture. Your television's picture is created much the same way. Browsers can handle this format quite easily.

  • .png Pronounced as 'ping', this stands for Portable Network Graphic. This is ultimately the replacement for .gif, with partial transparency options, but browser support is sketchy--some browsers still don't like to display .png files.

  • .jpeg or .jpg (pronounced "j-peg") There are two names to denote this format because of the PC and MAC formats allowing 3 and 4 letters after the dot. JPEG is an acronym for Joint Photographic Experts Group, the organization that invented the format. The format is unique in that it uses compression after it's been created. That's fancy computer talk that means that when the computer is not using a .jpeg image it folds it up and puts it away. For example, if the picture is 10K bytes when displayed, it may be only 4K bytes when stored. Nice trick, huh? It saves on hard drive space, but also tends to require a bit of memory on your part to unfold the image.
    Someone always writes to me to tell me that .gif images also use compression. Yes, they do, but only when they are first created into that format. After that, no compression. JPEG, on the other hand, uses compression throughout its life to fold up smaller than it really is.

  • .bmp (pronounced "bimp") This is a "bitmap." You will probably never place a bitmap as an image, although some browsers do allow it. A bitmap is an image that a computer produces and places for you. A counter is an example. Even though some browers, such as Internet Explorer, will allow you to place a BMP as an image, I wouldn't. Most browsers will not be able to display it. Go with .gif, .jpg or .png.

Import project into Eclipse



Steps to import project into eclipse

  1. File → Import → General → Existing Projects into Workspace → Next
  2. Select root directory: E:\android\projects\FlappyBird
  3. Projects → Select All
  4. Uncheck Copy projects into workspace and Add project to working sets
  5. Finish.


Have a nice to go ahead,