Pages

Ads 468x60px

Sunday, 30 June 2013

Android Activities

Android Activities

You will learn how to create Android Activities you create a Java class that extends the Activity base class :

package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
        public class MainActivity extends Activity
        {
               /** Called when the activity is first created. */
              @Override
               public void onCreate(Bundle savedInstanceState)
               {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.main);
                }
          }

Your activity class would then load its UI component using the XML file defined in your res/layout folder. In this example, you would load the UI from the main.xml file:
setContentView(R.layout.main); Every activity you have in your application must be declared in your AndroidManifest.xml file, like this:

<?xml version=”1.0” encoding=”utf-8”?>
     <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
         package=”net.learn2develop.Activities”
        android:versionCode=”1”
        android:versionName=”1.0”>
     <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
    <activity android:name=”.MainActivity” android:label=”@string/app_name”>
<intent-filter>

<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter></activity></application>
<uses-sdk android:minSdkVersion=”9” />
</manifest>

The Activity base class defines a series of events that governs the life cycle of an activity. The Activity class defines the following events:

➤➤ onCreate() — Called when the activity is first created
➤➤ onStart() — Called when the activity becomes visible to the user
➤➤ onResume() — Called when the activity starts interacting with the user
➤➤ onPause() — Called when the current activity is being paused and the previous activity is being resumed 
➤➤ onStop() — Called when the activity is no longer visible to the user
➤➤ onDestroy() — Called before the activity is destroyed by the system (either manually or by the system to conserve memory).
➤➤ onRestart() — Called when the activity has been stopped and is restarting again

By default, the activity created for you contains the onCreate() event. Within this event handler is the code that helps to display the UI elements of your screen.Figure Below shows the life cycle of an activity and the various stages it goes through — from when the activity is started until it ends



The best way to understand the various stages experienced by an activity is to create a new project, implement the various events, and then subject the activity to various user interactions.
 
Reade more >>

Anatomy of an Android Application

Anatomy of an Android Application

You have created your first Hello World Android application, it is time to dissect the innards
of the Android project and examine all the parts that make everything work.
First, note the various files that make up an Android project in the
Package Explorer in Eclipse (see Figure). The various folders and their files are as follows:

➤➤ src — Contains the .java source files for your project. In    
this example, there is one file,  MainActivity.java. The MainActivity.java file is the source file for your activity. You will write the code for your application in this file.


➤➤ Android 2.3 library — This item contains one file, android.jar, which contains all the class libraries needed for an Android application.

➤➤ gen — Contains the R.java file, a compiler-generated file that references all the resources found in your project. You should not modify this file.

➤➤ assets — This folder contains all the assets used by your application, such as HTML, text files, databases, etc.

➤➤ res — This folder contains all the resources used in your application. It also contains a few other subfolders: drawable-<resolution>, layout, and values. Chapter 3 talks more about
how you can support devices with different screen resolutions and densities.

➤➤ AndroidManifest.xml — This is the manifest file for your Android application. Here you specify
the permissions needed by your application, as well as other features (such as intent-filters, receivers, etc.). Chapter 2 discusses the use of the AndroidManifest.xml file in more details
.

The main.xml file defines the user interface for your activity. Observe the following in bold:
 

<TextView
      android:layout_width=”fill_parent”
      android:layout_height=”wrap_content”
      android:text=”@string/hello” />
 
The @string in this case refers to the strings.xml file located in the res/values folder. Hence,
@string/hello refers to the hello string defined in the strings.xml file, which is “Hello World,
MainActivity!”:

 <?xml version=”1.0” encoding=”utf-8”?>
      <resources>
      <string name=”hello”>Hello World, MainActivity!</string>
      <string name=”app_name”>HelloWorld</string>
  </resources>

It is recommended that you store all the string constants in your application in this strings.xml file and reference these strings using the @string identifier. That way, if you ever need to localize your application to another language, all you need to do is replace the strings stored in the strings.xml file with the targeted language and recompile your application. Observe the content of the AndroidManifest.xml file:

<?xml version=”1.0” encoding=”utf-8”?>
   <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
      package=”net.learn2develop.HelloWorld”
      android:versionCode=”1”
      android:versionName=”1.0”>
 <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
     <activity android:name=”.MainActivity” android:label=”@string/app_name”>
      <intent-filter>
    <action android:name=”android.intent.action.MAIN” />
  <category android:name=”android.intent.category.LAUNCHER” />
 </intent-filter></activity></application>
<uses-sdk android:minSdkVersion=”9” />
</manifest>

The AndroidManifest.xml file contains detailed information about the application:
➤➤ It defines the package name of the application as net.learn2develop.HelloWorld.

➤➤ The version code of the application is 1. This value is used to identify the version number of your application. It can be used to programmatically determine whether an application needs to be upgraded.

➤➤ The version name of the application is 1.0. This string value is mainly used for display to the user. You should use the format: <major>.<minor>.<point> for this value.

➤➤ The application uses the image named icon.png located in the drawable folder.

➤➤ The name of this application is the string named app_name defi ned in the strings.xml file.

➤➤ There is one activity in the application represented by the MainActivity.java fi le. The label displayed for this activity is the same as the application name.

➤➤ Within the defi nition for this activity, there is an element named <intent-filter>:

➤➤ The action for the intent fi lter is named android.intent.action.MAIN to indicate that
this activity serves as the entry point for the application.

➤➤ The category for the intent-fi lter is named android.intent.category.LAUNCHER
to indicate that the application can be launched from the device’s Launcher icon.
next lesson discusses intents in more details.

➤➤ Finally, the android:minSdkVersion attribute of the <uses-sdk> element specifi es the minimum version of the OS on which the application will run. As you add more fi les and folders to your project, Eclipse will automatically generate the content of R.java, which at the moment contains the following:

 package net.learn2develop.HelloWorld;
   public final class R
   {
                public static final class attr {   }
                public static final class drawable 
                {
                        public static final int icon=0x7f020000;
                }
      public static final class layout 
       {
             public static final int main=0x7f030000;
        }
      public static final class string
      {
          public static final int app_name=0x7f040001;
         public static final int hello=0x7f040000;
       }
 }

You are not supposed to modify the content of the R.java file; Eclipse automatically generates the content for you when you modify your project.


Finally, the code that connects the activity to the UI (main.xml) is the setContentView() method, which is in the MainActivity.java file:

package net.learn2develop.HelloWorld;
import android.app.Activity;
import android.os.Bundle;
           
 public class MainActivity extends Activity 
  {
       /** Called when the activity is first created. */
       @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
         }
    }
Here, R.layout.main refers to the main.xml file located in the res/layout folder. As you add additional XML files to the res/layout folder, the filenames will automatically be generated in the R.java file. The onCreate() method is one of many methods that are fired when an activity is loaded. Next Lesson discusses the life cycle of an activity in more detail.


Reade more >>

Creating Your First Application 2

Creating Your First Application 2

We will complete previous lesson Creating Your First Application1 , in the last step in previous lesson the figure below explain that step 


6. The main.xml file defines the user interface (UI) of your application. The default view is the Layout  view, which lays out the activity graphically. To modify the UI, click the main.xml tab located at  the bottom 


7 Add the following code in bold to the main.xml file:

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello” />
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”This is my first Android Application!” />
<Button
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”And this is a clickable button!” />
</LinearLayout>

8 To save the changes made to your project, press Ctrl+s.
9 You are now ready to test your application on the Android Emulator. Select the project name in Eclipse and press F11. You will be asked to select a way to debug the application. Select Android Application as shown in Figure  and click OK. 



10. The Android Emulator will now be started (if the emulator is locked, you need to slide the unlock button to unlock it first). Figure 1-26 shows the application running on the Android Emulator.





11. Click the Home button (the house icon in the lower-left corner above the keyboard) so that it now shows the Home screen (see Figure)



12. Click the application Launcher icon to display the list of applications installed on the device. Note that the HelloWorld application is now installed in the application launcher (see Figure)
 
 
How It Works

To create an Android project using Eclipse, you need to supply the information shown in Table  

Table : Project Files Created by Default
properties                                           Description
Project name                                    The name of the project
Application name                             A user-friendly name for your application
Package name                                The name of the package You should
                                                           use  a reverse   domain   name for this
Create Activity                                 The name of the fi rst activity in your application
Min SDK Version                            The minimum version of the SDK that your project is targeting
Reade more >>

Tuesday, 4 June 2013

creating your First Android Application 1

creating your First Android Application

With all the tools and the SDK downloaded and installed, it is now time to start your engine! As in all programming books, the fi rst example uses the ubiquitous Hello World application. This will enable you to have a detailed look at the various components that make up an Android project.

1 Using Eclipse, create a new project by selecting File ➪ Project… (see Figure).


2 - Expand the Android folder and select Android Project (see Figure).




3  - Name the Android project as shown in Figure  and then click Finish.
 

4 The Eclipse IDE should now look like Figure  .
5 In the Package Explorer (located on the left of the Eclipse IDE), expand the HelloWorld project by
clicking on the various arrows displayed to the left of each item in the project. In the res/layout
folder, double-click the main.xml fi le
Reade more >>

.

Education blog