Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Saturday, March 10, 2012

Developing a Basic Application on Android: "Welcome Page"

Hi Everyone! Today, I'll show you an Android application which is about welcome pages. As known, before when opened an app, game, web site, DVD movie, etc, a welcome page has shown us by masters of'em. That's why is showing what the brand is. For example, Angry Birds, EA Games, Microsoft Games.

This article is going to be a basic application like "Hello World!". But Before we'll be on welcome page and see the welcome message on it. Let's see how to do it!

Necessary equipment (Install all of them)
  1. Eclipse Editor
  2. SDK (Software Development Kid)
  3. Android: WindowsLinuxMac
First, open the Eclipse editor, then follow this directory: File/New/Project/Android/Android Project
My Project Name : WelcomePage
My Android Version : V2.2
My Package Name : welcome.page
You can fix this properties as you wish.

I think, you know how to do an "hello world" app on Android, so I won't explain this. The important thing of the app for me is relationship of Activities and Threads. For those, we're going to do this pages:
src/WelcomePageActivity.class //This is a default file
src/WelcomePage.class
res/layout/main.xml //This is also a default file
res/layout/page2.xml
AndroidManifest.xml //This is configuration file
Create a class file: 
  1. directory: src/(Right Click)New/Class
  2. call: WelcomePage
  3. Click the "Finish" Button
Code for WelcomePage.java:
package welcome.page;

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

public class WelcomePage extends Activity { //Created WelcomePage Activity
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main); //The content to display from main.xml
 }
}

The "R" is a library which used for xml files. Every xml file can used on it. We'd just used main.xml file for we need to go home page of our Android app after showing welcome page.

Create a xml file:
  1. directory: res/layout/(Right Click)Other/File/Android/Android Xml File
  2. call: page2
  3. Click the "Finish" Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a welcome page!" />

</LinearLayout>

When we look at this page up, should see like that:
page2.xml file view
Create WelcomePageActivity.java file
package welcome.page;

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

public class WelcomePageActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page2); //The content to display from page2.xml file
        
        Thread WelcomeScreen = new Thread() { //Must create a thread, because opening another page is a different event, as sync
         public void run() {
          try { //This is try-catch
           sleep(5000); //Waiting five seconds
           startActivity(new Intent(getApplicationContext(),WelcomePage.class)); 
           //After waiting, shown the content from WelcomePage.java file
          } catch (InterruptedException e) {
           e.printStackTrace();
          }
          finally {
           finish(); //We finish this thread.
          }
         }
        };
        WelcomeScreen.start(); //Starting the thread created.
    }
}

Maybe, you may not get the line, 
"startActivity(new Intent(getApplicationContext(),WelcomePage.class));". 
Just remember that we used main.xml content to get on WelcomePage.java page.

And the last one we have to do, update AndroidManifest.xml file! Remember that we create a new Activity, so must define this Activity. For that reason, just add this code to AndroidManifest.xml file:
And yes, finished! Let's work this on our Emulator! For this, call: 


WelcomePage Project(Right Click)/Run as/Android Project

Welcome page view
After 5 seconds this page, we will see main.xml file:

main.xml file on my application
We'll see you guys next article!