• SEARCH BOX

Combine Jetty and Spring Application Context

Spring's Logo The goal of this post is to use one xml configuration file which is Spring's applicationContext.xml and load it only once for the duration of the application running in an embedded jetty server. Read more...

Tutorial on Android Layout

A thumbnail of adroid. Android is an open source platform for mobile devices. Not a hardware. It is a software stack as defined by Google. Encase your not familiar with the term, software stack is compose of an Operating System(OS), middle-ware and the key applications. Read more...

Hi, my name's Paul Labis.

I'm a developer in Makati City, Philippines. Focused on building online Java/J2EE based applications. I'm interested in freelance or part-time work from home.

Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Friday, April 16, 2010

Tutorial on Android Views - Part 2

In this article, we will be dealing with another types of Android views. They are the RadioGroup, RadioButton, ProgressBar and AutoCompleteTextView. By learning the following views, it will broaden your understanding on android views that will help you build your own android application. 

Lets start by creating a new android project on eclipse. See the screenshot beside for android project specifics. 
 
RadioGroup and RadioButton
This two views is basically used to let users choose something among available options. RadioButton has two available states. It is either checked or unchecked.  Once a RadioButton is checked, it cannot be unchecked unless its grouped along with other radio button on a RadioGroup. A RadioGroup is a type of view that is used to group one or mmore radio buttons, thereby allows a user to select only one radiobutton or only one option.

Create new android xml named basicviewspart2.xml under res/layout folder then put the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioGroup android:id="@+id/colorOptions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<RadioButton android:id="@+id/red"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Red"
/>
<RadioButton android:id="@+id/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Blue"
/>
<RadioButton android:id="@+id/green"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Green"
/>

</RadioGroup>
</LinearLayout>

 
Then, update BasicViewsPractice.java by adding the following codes:
package com.app.techie;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class BasicViewsPractice extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basicviewspart2);
       
        RadioGroup colorChoices = (RadioGroup) findViewById(R.id.colorOptions);
        colorChoices.setOnCheckedChangeListener(new OnCheckedChangeListener() {
           
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton redOption = (RadioButton) findViewById(R.id.red);
                RadioButton blueOption = (RadioButton) findViewById(R.id.blue);
                RadioButton greenOption = (RadioButton) findViewById(R.id.green);
               
                if(redOption.getId()==checkedId){
                    displayToast("You selected red!");
                }
               
                if(blueOption.getId()==checkedId){
                    displayToast("You selected blue!");
                }
               
                if(greenOption.getId()==checkedId){
                    displayToast("You selected green!");
                }
            }
        });
       
    }

    private void displayToast(String message){
        Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
    }
}

Briefly, the code above simply display a message through Toast. the message indicates what option a user selects. We identified the radiobutton selected using its IDs. The above should look like:

ProgressBar
This view is usually used to indicate that an event, task or an activity is currently ongoing. An example of which is downloading files where progress of download is shown visually though a color bar. This is a good choice to tell the user about the status of the task. For us to understand it better, lets do the activity below.

Lets use same activity created above and update the basicviewspart2.xml by adding the following code:
<ProgressBar
android:id="@+id/normalProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button android:id="@+id/btnDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download"
/>

On our xml code, we added two views. A progress bar with visibility default to gone so at startup it does not appear on display and a button to do some work or task on background. What we want to do is to simulate progress bar. So, if the user clicks the button, a task is started and the progress bar will display and hide when the task is done.

Update BasicActivityPractice.java to look like this:
package com.app.techie;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class BasicViewsPractice extends Activity {
    private static int progress = 0;
    private ProgressBar normalProgressBar;
    private int progressStatus = 0;
    private Handler handler = new Handler();

   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basicviewspart2);
       
        RadioGroup colorChoices = (RadioGroup) findViewById(R.id.colorOptions);
        colorChoices.setOnCheckedChangeListener(new OnCheckedChangeListener() {
           
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton redOption = (RadioButton) findViewById(R.id.red);
                RadioButton blueOption = (RadioButton) findViewById(R.id.blue);
                RadioButton greenOption = (RadioButton) findViewById(R.id.green);
               
                if(redOption.getId()==checkedId){
                    displayToast("You selected red!");
                }
               
                if(blueOption.getId()==checkedId){
                    displayToast("You selected blue!");
                }
               
                if(greenOption.getId()==checkedId){
                    displayToast("You selected green!");
                }
            }
        });
       
        normalProgressBar = (ProgressBar) findViewById(R.id.normalProgressBar);
        Button download = (Button) findViewById(R.id.btnDownload);
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //show the progress bar
                normalProgressBar.setVisibility(0);
                simulateProgress();
            }

            private void simulateProgress() {
                progress = 0;
                progressStatus = 0;
                //do some work in background thread
                new Thread(new Runnable()
                {
                    public void run()
                    {
                        //do some work here
                        while (progressStatus < 10)
                        {
                            progressStatus = doSomeWork();
                        }
                       
                        //hides the progress bar
                        handler.post(new Runnable()
                        {
                            public void run()
                            {
                                //0 - VISIBLE; 4 - INVISIBLE; 8 - GONE
                                normalProgressBar.setVisibility(8);
                            }
                        });
                    }   
                   
                    //do some long lasting work here
                    private int doSomeWork()
                    {
                        try {
                            //simulate doing some work
                            Thread.sleep(500);
                        } catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                        return ++progress;
                    }
                   
                }).start();
            }

           
        });
       
    }

    /**
     * Helper method to display message
     * @param message
     */
    private void displayToast(String message){
        Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
    }
}

The above code retrieves the progress bar we added to layout earlier as well as the button. When the button is clicked, the progress bar visibility is changed to visible mode and a background task simulation took place wherein upon its done, the visibility of the progress bar is changed back to gone. By the way, there are three representations of visibility - 0 for visible, 4 for invisible, and 8 for gone. The activity should look like below.
Moreover, ProgressBar has an attribute style wherein you can change its display. Instead of intermediate mode style like above, you can use a horizontal bar. Take note of the code:
<ProgressBar
android:id="@+id/normalProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
style="?android:attr/progressBarStyleHorizontal"
/>

And then change your activity class code in method simulationProgress() to:
//do some work here
while (progressStatus < 100)
{
   progressStatus = doSomeWork();
                           
   //Update the progress bar
   handler.post(new Runnable()
      {
         public void run() {
           normalProgressBar.setProgress(progressStatus);
         }
      });

}

The result should be like:

AutoCompleteTextView
It is a subclass of textview only that it has an auto complete suggestion feature. Think of a textbox while typing and under it is keyword suggestions.  This basically helps you reduce the typing effort and typo error. 

What we are going to do first is update the android layout xml by adding an autocomplete textview:
<AutoCompleteTextView
android:id="@+id/txtFriends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Afterwards, we add code to our activity class inside onCreate method:
final String[] friends = {
                "Jose Rizal",
                "Britney Spears",
                "Sarah Walker",
                "Sarah Jane",
                "Brook How",
                "Kimberly Some",
                "Kimkrek Shoo"
        };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, friends);
       
        AutoCompleteTextView txtFriend = (AutoCompleteTextView) findViewById(R.id.txtFriends);
        txtFriend.setThreshold(3);
        txtFriend.setAdapter(adapter);

Understanding the code above: We made an array of string where its value will be the value suggested to the user. It is feed into an ArrayAdapter object where the auto complete textview can obtain suggestions. If you notice, we also set threshold to 3. Threshold is used to set the minimum characters before suggestions appear as a drop-down menu. Try running the application and it should look like:

Nutshell
Finally, we are donw with those another three kind of useful Android views. In this tutorial we've learned more basic on views. Another article is coming up taking up more views so stay tuned. Hope this article helps you.

Saturday, April 10, 2010

Android Software Competition by Globe Telecom

I first heard this news from Calen Legaspi, our CEO at Orange and Bronze Software Labs, that Globe Labs will host and conduct its first mobile developer event for 2010. I've been hearing about Android platform since 2008 and now, Globe Labs posted an invitation for all developers to explore the new mobile platform.

The event is an Android software competition open for all Filipino developers. This is an invitation to all developers to build their mobile application on top of Android platform.
 
Big cash prizes are at stake in this competition, as well as the chance for the participating teams to pitch their product to international investors for funding. The competition will run through until July 2010. For more details on contest mechanics, dates and guidelines, please click here.

I think this event is a great opportunity for all developers. Its not just an opportunity to make money, but, an opportunity for learning how to develop an application on a new mobile platform.

To those who are interested to join this competition, visit Globe Labs website at www.globelabs.com to sign-up.

Tutorial on Android Views - Part 1

On my previous articles, we had a discussion on the android's definition and layout. I assume that you have already learn basics on manipulating android layout. In this and on the next article, we will be exploring and dealing more on the various types of commonly used views such as TextView, EditText, Button, ImageButton, CheckBox and ToggleButton. Those widgets are likely the most commonly used in developing android applications.

Lets begin by creating a new project on eclipse:

After which, create an Android XML under res/layout and name it basicviews.xml having written inside is the following code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  >
 
  <TextView
      android:text="Welcome to Android!"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
  />
 
  <EditText
      android:id="@+id/sampleText"
      android:text=""
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
  />
 
  <Button
      android:id="@+id/popupSampleText"
      android:text="Pop up"
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
  />
</LinearLayout>


The code above contains basic views such as textview, edittext and button supplied with an id. The views id will be used later and discussed further in this discussion. We are doing this layout so we will be able to play around with those views. Below is a little description on those view types.
  • TextView - is basically an element that is used to display text on screen or to the user. It is one of the most basic views and most commonly used in developing an android application. Also, do take note that the text inside this view type is not editable.
  • EditText - is another type of view that is used for character/text input such as name or passwords.
  • Button - it represents a push-button widget which can be pushed or clicked by the user to perform an activity, an event or an action.
Going back to views, its id must always start  with "@+id/" specifier followed by a meaningful view name. So, the next step is we want to use or utilize the following views. Lets edit BasicViewsPractice.java by making its code look like below:
package com.app.techie;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class BasicViewsPractice extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basicviews);
       
        Button popUp = (Button) findViewById(R.id.popupSampleText);
        popUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView sampleText = (TextView) findViewById(R.id.sampleText);
               
                if(0 != sampleText.getText().toString().length()){
                    Toast.makeText(getBaseContext(), sampleText.getText().toString(), Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getBaseContext(), "The textbox is empty!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
In the code above, instead of using the main.xml layout, we replace it with basicviews.xml layout on setContentView(...) method to load the layout we just did a while ago. We also instantiated a button as well as a editview. Those views are linked to the corresponding element with the exact id on the xml layout. Moreover, we also added an onClickListener () to the button so whenever the user clicks on it, the text inside the edittext will display on screen through Toast. Try running the application and it should look like this:
There you go! Your application should be similar to above. If the edittext is empty, it should display "textbox is empty" as a message. Moving forward, add the code below on basicviews.xml just right after popupButton view.
<EditText
      android:id="@+id/sampleTextPassword"
      android:text="sample"
      android:password="true"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
  />
 
  <ImageButton
      android:id="@+id/sampleImageButton"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:src="@drawable/icon"
  />

The code added two new views. The edittext character will be replaced with a dot since we set its password attribute to true. The imagebutton is similar to a normal button view. Only that image button has an attribute where you can specify an icon to display wrap inside a button.


Lets do another update by adding the following code:
<CheckBox android:id="@+id/chkSample"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Checkbox Sample"
  />
 
  <CheckBox android:id="@+id/chkStarLook"
    style="?android:attr/starStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Star checkbox"
  />

On the code, the first checkbox will look normal with a square and the second one will appear like a star since we provided a starStyle to its attribute. A checkbox is another special type of button. It has two state and its default is unchecked, otherwise its checked. For us to be able to try out checkbox view state, lets update BasicViewsPractice.java by adding the following code below:
CheckBox chkSample = (CheckBox) findViewById(R.id.chkSample);
        chkSample.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                if(((CheckBox) v).isChecked()){
                    Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getBaseContext(), "Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });


That suffice how you are going to verify a chechbox view state using java code. That same code also applies to the star checkbox. Just change the id of the checkbox on your java code. Try running the code and you should be able to see something like below.
Another view that behaves like CheckBox is the ToggleButton. It has two state, checked and unchecked, just like a CheckBox view. Only that this time, it has a light indicator to display its current state. Try it out by adding a togglebutton on basicviews.xml.
<ToggleButton android:id="@+id/tglSample"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Light switch"
/>


To be able to check its current state, add the following code below on the BasicViewsPractice.java.
ToggleButton tglSample = (ToggleButton) findViewById(R.id.tglSample);
        tglSample.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((ToggleButton) v).isChecked()){
                    Toast.makeText(getBaseContext(), "Light On", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getBaseContext(), "Light Off", Toast.LENGTH_SHORT).show();
                }
            }
        });



As you can see above, its more or less like the code on checking a checkbox view. Try running the code and see for yourself.


Nutshell:

In this tutorial we were able to learn and experiment on textView, edittext, button, imagebutton, checkbox and toggle button. However, there are still more views to learn and they will be discussed in detail my next article. Hope you learned from this article. Enjoy!

Sunday, March 28, 2010

Tutorial on Android Layout

On my previous article, I've discussed more on the theory and definition of an Android. In this article, we will be dealing more on the technical aspect of developing android application layout. I will walk you through various elements that make up the user interface(UI) and how to position different widgets on an android screen.

Terms to remember:
  • Activity - the basic unit of an android application. It contains views and ViewGroup. 
  • View - a widget that has an appearance on screen.
  • Widget - are user interface components like buttons, labels, text boxes, etc.
  • ViewGroup - a special type of View that provides the layout in which you can group views, order the appearance and sequence your views.
Just an additional information; In a typical android project, UI is defined using an XML file located in the res/layout folder. An example is main.xml located in res/layout folder.  During runtime, the .xml file where you defined your UI is loaded using the onCreate() event handler of your activity class and using setContentView() method of the extended Activity class. Moreover, during compilation time, each of the element in the defined UI xml file are compiled into an equivalent Android GUI class wherein its attributes are represented by methods.


Furthermore, Android supports various ViewGroups and among those are the linear, absolute, table, relative and scroll view layouts. This layouts will be discussed in details below.

At this point, I presume you already setup your development environment for programming Android applications. If you haven't, please follow the android setup tutorial.

Get Your Hands Dirty
So, lets begin by creating the sample project on eclipse. Do observe the screen shot below. (Click the image to enlarge)

There is another application called DroidDraw which you can use to design your layout aside from eclipse. Though, eclipse has its Android layout editor, it has limited features. Perhaps, you might want to use DroidDraw instead which supports drag and drop. However, its on your disposal whether which tool could help. On this ongoing tutorial, we will be using only the xml editor on eclipse.

LinearLayout 
It is the layout widget where you are able to arrange your views horizontally or vertically on a single row or single column. To see the how it works, lets create linear.xml under res/layout folder in the project and enter the code below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/sampleText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text 01"
/>

<TextView
android:id="@+id/sampleText02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text 02"
/>

</LinearLayout>
and change the LayoutPractice.java code to:
package com.techie.layout;

import android.app.Activity;
import android.os.Bundle;

public class LayoutPractice extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
}
}
that will result to:
On the code above, you'll notice that it starts with a container <LinearLayout> android xml element that hold and control the order and appearance of the other views elements inside it. That serves as the main container of the views elements <TextView>. You'll also notice that we specified the layouts' orientation to vertical. By its default, it is set to horizontal. Other than that, we also specified the layouts' width and height. That basically are the basic ones that you need to specify in a layout element.

The fill_parent and wrap_content are two constants. You should use fill_parent if you want the full size of the parent container which in our case is the linear layout container. Use wrap_content if you want the size of its content. Try adding a button to linear.xml and specify its width like below:
<Button
          android:id="@+id/sampleButton01"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Sample Button 01"
          />
Experiment by changing its width to wrap_content. Also, you can specify its layouts width and size by pixel(px). See the code below for an example:
<Button
          android:id="@+id/sampleButton01"
          android:layout_width="150px"
          android:layout_height="40px"
          android:text="Sample Button 01"
          /> 

In linear layout, layout_weight and layout_gravity attributes are applicable to views contained within it.  Below is an example of how to use it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
 
    <TextView
          android:id="@+id/sampleText01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Sample Text 01"   
          />
         
    <TextView
          android:id="@+id/sampleText02"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Sample Text 02"
          />
         
      <Button
          android:id="@+id/sampleButton01"
          android:layout_width="150px"
          android:layout_height="40px"
          android:text="Sample Button 01"
          />
  <Button
          android:id="@+id/sampleButton02"
          android:layout_width="150px"
          android:layout_height="40px"
          android:text="Sample Button 02"
          android:layout_weight="0.2"
          android:layout_gravity="right"
          />
  <Button
          android:id="@+id/sampleButton03"
          android:layout_width="150px"
          android:layout_height="40px"
          android:text="Sample Button 03"
          android:layout_weight="0.2"
          android:layout_gravity="left"
          />
  <EditText      
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"       
        android:textSize="18sp"
        android:layout_weight="0.8"       
        />
 
</LinearLayout>


In the above code, we applied layout_gravity to both buttons which aligned them to right and left corner of the parent container. The layout_weight attribute was used to specify the ratio in which the button and editText views occupy the remaining space on the screen.
AbsoluteLayout 
It enables you to specify the exact location of its content views like buttons, textview, and etc.. Lets create another android xml named absolute.xml under res/layout folder of the project. See the code below.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <Button
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:layout_x="12px"
        android:layout_y="361px"
        />
    <Button
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_x="160px"
        android:layout_y="361px"
        />
</AbsoluteLayout>

So, in the above you'll notice that we make use of the view attributes layout_x and layout_y to specify the exact positions of the absolute layouts' contents. To be able to see the changes, do change the code below:
package com.techie.layout;

import android.app.Activity;
import android.os.Bundle;

public class LayoutPractice extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.absolute);
    }
}
It will render the user interface like this:
TableLayout
Like a  normal table tag, it enables you to group views by rows and columns. Every views you place inside a row forms a cell. The cells' width is determined by the largest cell by that column. 


Create table.xml under res/layout of the project folder containing the following code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
 
  <TableRow>
          <TextView
            android:text="First Name"             
              />
          <EditText
              android:layout_width="150px"
              />
  </TableRow>
 
  <TableRow>
          <TextView
            android:text="Last Name"             
              />
          <EditText
              android:layout_width="150px"
              />
  </TableRow>
 
  <TableRow>
          <TextView
            android:text="Are you developer?"             
              />
          <CheckBox
              android:text="Yes"
              />
  </TableRow>

  <TableRow>
          <TextView
            android:text="Password"             
              />
          <EditText
              android:layout_width="150px"
              android:password="true"
              />
  </TableRow>
 
  <TableRow>
          <TextView/>
          <Button
              android:text="Submit"
              />
  </TableRow>
 
</TableLayout>


Don't forget to update the LayoutPractice activity code to
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
    }

So, as you can see above, the code implemented 2 column and 5 rows. The  views width  on column 1 were of same size with the largest view width in that column. Note also that  a view  was added just above the Submit button so that the Submit button will align on the second column. If you remove the textView, it will look like this:
Moreover, on the 5th row, a textView for password was added. The attribute password was set to true so that the text entered will be replaced with a dot.


RelativeLayout

This layout enables you to specify child view's position relating to another view. It makes use of available view attributes alignParentTop, alignParentLeft, alignLeft, alighRight, below, centerHorizontal and etc. Those mentioned attributes are made available to views inside a relative layout for them to be align relative to another view. Take note that it utilize id's of another view. Take a look at the code below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
 
   <TextView
        android:id="@+id/labelComments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Comments"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        />
    <EditText
        android:id="@+id/textComments"
        android:layout_width="fill_parent"
        android:layout_height="170px"
        android:textSize="18sp"
        android:layout_alignLeft="@+id/labelComments"
        android:layout_below="@+id/labelComments"
        android:layout_centerHorizontal="true"
        />
    <Button
        android:id="@+id/buttonSave"
        android:layout_width="125px"
        android:layout_height="wrap_content"
        android:text="Save"
        android:layout_below="@+id/textComments"
        android:layout_alignRight="@+id/textComments"
        />
    <Button
        android:id="@+id/buttonCancel"
        android:layout_width="124px"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_below="@+id/textComments"
        android:layout_alignLeft="@+id/textComments"
        />
 
</RelativeLayout>


ScrollView

It is a frame layout that enables a feature where the user can scroll through a list that basically occupy more space than the available physical space on your android phone. A scrollview can only contain one viewgroup which is normally a linearlayout. The code below has a linear layout inside a scroll view. 

Try this by creating scrollview.xml under res/layout folder of the project.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:id="@+id/widget54"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <LinearLayout
        android:layout_width="300px"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
            />
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 2"
            />
        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 3"
            />
        <EditText
            android:id="@+id/txt1"
            android:layout_width="fill_parent"
            android:layout_height="300px"
            />
        <Button
            android:id="@+id/button4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 4"
            />
        <Button
            android:id="@+id/button5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 5"
            />
        <EditText
            android:id="@+id/txt2"
            android:layout_width="fill_parent"
            android:layout_height="200px"
            />
    </LinearLayout>
</ScrollView>

The above code results to the following:

The above is a neat implementation of scroll view where inside is a linear container having contents that occupy more than the available actual space of an android screen.


Nutshell
In this article, you saw various layout implementations with wigets on top of Android. On the next articles, we will focus on other components of the UI which is the views.  Practice on your newly learned lay-outing skill. So, stay tune and have fun with android! 

I would like to give credit to Wei-Meng Lee who written  a very helpful tutorial on android layout.

Thursday, March 25, 2010

Learning Android By Definition

Android is an open source platform for mobile devices. It is not a hardware. Its basically a software. In fact, It is a software stack as defined by Google. Encase your not familiar with the term, software stack is compose of an Operating System(OS), middle-ware & the key applications.
  • OS is by simply defined as platform between you and the hardware and where all applications run.
  • Middle-ware is/are the components and available package that allows applications to communicate to a network and to one another.
  • Key Applications is/are the actual programs or software that the phones will run
It was the product of the joint effort of Google and Open Handset Alliance(OHA). It was released November 5, 2007 which is shortly just after Apple's first generation iPhone release. Android is based on an opensource operating system called Linux.

Android is equipped with a set of core applications that includes an email client, SMS program, browser, contacts, calendar, maps, and others. Another good thing is that all applications are written using the Java programming language which is also an opensource programming language. See below for features.

Features

  • Application framework enabling reuse and replacement of components
  • Dalvik virtual machine optimized for mobile devices
  • Integrated browser based on the open source WebKit engine
  • Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
  • SQLite for structured data storage
  • Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)
  • GSM Telephony (hardware dependent)
  • Bluetooth, EDGE, 3G, and WiFi (hardware dependent)
  • Camera, GPS, compass, and accelerometer (hardware dependent)
  • Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse ID
Android is an exiting open platform for mobile development. It offer's the ability to build extremely rich and innovative applications. Developers are given freedom to take full advantage of the device hardware, retrieve location information, run silent operations, camera usage, alerts, and much, much more. Finally, the Application Program Interface(API ) is freely and fully accessible for developers. That makes learning so much easier.

Market demand for android software and developers are increasing. No wonder my company,  Orange & Bronze Software Labs, Inc., was encouraging its developers to start learning, mastering and creating software/applications made in Java that runs in an Android phone.

I just started with Android couple of days ago and so far, I find it very interesting & fun to learn. So, I recommend you get started too. Good luck!

Note: To be able to learn more about Android, visit http://developer.android.com/.
 

Author

Followers

Techie Projects

Open Source