Wednesday, January 18, 2012

StartingPoint class


 We will begin by creating a new Java class named StartingPoint.  This is where we will begin implementing our UI, or User Interface.  



You’re StartingPoint class should look like this:


package com.jesse.blogapp;

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

public class StartingPoint extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);

}

}


 All we do is to extend Activity and Override the onCreate(bundle) method, then set the content View to R.layout.startingpoint.

Next we’ll work on the corresponding startingpoint.xml file.  It should look like this:

<?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"
   android:background="#FFFF00">

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/sp"
       android:textSize="20sp"
       android:textColor="#000000" />

</LinearLayout>


This is straight forward, but I’ll elaborate on a couple of points.  First thing we do is to set the parent LinearLayout background to yellow, which is represented by the hex value “#FFFF00”.  The hex values can be hard to remember, but I found a great site that makes it easy.  You can check it out here.  Next we create a TextView and set the layout_width and layout_height to “wrap_content”.   The View widgets, and most others, must have a width and height value.  Eclipse will give you and error if you forget to add them.   On a side note, if you ever get an error that the R in (R.layout.whatever) cannot be resolved, this is usually because of an error in an .xml file.  They are sometimes hard to track down.  The next thing we did was to change the text value to “@string/sp”.  
 To do this, we’ll have to add a string to our string.xml file.  It should look like this now:

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="hello">Hello World, Main!</string>
   <string name="app_name">Blog App</string>
   <string name="splash">SPLASH SCREEN</string>
   <string name="sp">Starting Point</string>

</resources>


 Our next goal is to get our Splash screen to act like a Splash screen should.  We want it to display for a few seconds then automatically change to our StartingPoint activity.  So, our Main Java class should be changed to look like this:

package com.jesse.blogapp;

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

public class Main extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       Thread sleeper = new Thread(){
       
        public void run(){
       
        try{
       
        sleep(3000);
       
        }catch(InterruptedException e){
       
        e.printStackTrace();
       
        }finally{
       
        Intent i = new Intent("android.intent.action.STARTINGPOINT");
        startActivity(i);
        }
        }
       };
       
       sleeper.start();
       
   }
}


 The most important thing we did was to create a Thread.  The Thread() method allows us to do several things at once without bogging down the system and using up the resources needed to run the Activity.  They are very useful for displaying animated graphics or sound.  
 Next we created the run() method and wrapped our logic in a try bracket.  This is used to catch exceptions so that our app doesn’t crash in the event of something unusual happening.  And we do this by adding the  catch (InterruptedException e).  The “e” could be anything you want, but it’s convention to call it “e”.  We use the “e” which is now our InterruptedException, if one is caught, to printStackTrace.  This just logs the Exception for debugging purposes.  

We use sleep(3000); to get our Splash screen to hold for 3 seconds. The 3000 is in milliseconds.  Finally we will create a new Intent and start our next Activity.  We get the Intent ("android.intent.action.STARTINGPOINT") from our manifest.xml file, which looks like this now:




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.jesse.blogapp"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="10" />

   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name" >
       <activity
           android:name=".Main"
           android:label="@string/app_name" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
       
       <activity
           android:name=".StartingPoint">
           <intent-filter>
               <action android:name="android.intent.action.STARTINGPOINT" />

               <category android:name="android.intent.category.DEFAULT" />
           </intent-filter>
       </activity>       
       
   </application>

</manifest>






 We added a new <activity> and an <intent-filter>.  We named our new activity STARTINGPOINT, and set the category to DEFAULT.   The manifest is actually very simple once you get the hang of it.  It’s just repetitive, adding activities and intent filters over and over.

 Our main.xml file will remain unchanged for now.  We’ll add an image to it later.   If you have any questions feel free to join in and learn with me.  Have a great year, and happy coding!





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.