You are on page 1of 21

Operation Sheet 2.

1 – Developing basic android application

Objectives:

 Understand the step by step process in developing android application


 Perform the basic programming in android studio
 Build, run and test android application

Materials Needed:
o Computer with Windows 7 OS
o Android Studio
o Java Development Kit

Procedure:

Part I: Introduction to Android Programming

Step 1. Create Android Application


The first step is to create a simple Android Application using Android studio. Search for android
in the program menu. When you click on Android studio icon, then click “Start a new
Android Studio Project” it will show screen as shown below:

Native Android app Michael e. UU


Step 2: Type the application name
Native Android app Michael e. UU
You can start your application development by calling start a new android studio project. in a
new installation frame should ask Application name, package information and location of the
project. Type the application name “LearningGuide2”

Step 3: Select Target Android Device

After entered application name, it going to be called select the form factors your application
runs on, here need to specify Minimum SDK, in our tutorial, I have declared as API23: Android
6.0(Mashmallow) −

Native Android app Michael e. UU


Step 4: Choose an Activity

The next level of installation should contain selecting the activity to mobile, it specifies the
default layout for Applications.

Native Android app Michael e. UU


Step 5: Getting ready for writing the application code

At the final stage it’s going to be open development tool to write the application code.

Step 6: After Click on a virtual device icon, it going to be shown by default virtual devices
which are present on your SDK, or else need to create a virtual device by clicking Create new
Virtual device button

If your AVD is created successfully it means your environment is ready for Android application
development.

Native Android app Michael e. UU


Step 7: Dissecting the Code of Activity_main.xml and Main_Activity.java

Before Writing a Hello word code, you must know about XML tags.To write hello word code,
you should redirect to App>res>layout>Activity_main.xml. Then To show hello word, we
need to call text view with layout ( about text view and layout, you must take references at
Relative Layout and Text View ).

B. Expand java. The application name, then click MainActivity.java as shown below

public class MainActivity extends Activity {

Native Android app Michael e. UU


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Step 7: Running the Application


Let's try to run our Hello World! Application we just created. I assume you had created your
AVD while doing environment set-up. To run the app from Android studio, open one of your
project's activity files and click Run icon from the tool bar. Android studio installs the app on
your AVD and starts it and if everything is fine with your set-up and application, it will display
following Emulator window −

Native Android app Michael e. UU


Part II: Adding Component and starting a new activity

Native Android app Michael e. UU


Step 1: Modify to layout based on the interface as shown below. Go to
App>res>layout>Activity_main.xml.

Step 2: Add some UI component based on the interface shown below.

Native Android app Michael e. UU


Remove Hello world textview and Add a button. From the Palette window, click Widgets in the
left pane, and then drag Button into the design editor and drop it near the right side. Create a
constraint from the left side of the button to the right side of the text box.

Step 3: Right click the button and choose “Go to XML”. Modify the android text: “Launch
Profile Activity”.

Native Android app Michael e. UU


Step 4: Create a profile activity

To the left of Android Studio, you will find Project Explorer. Right Click on the app from
the project explorer and select,

New -> Activity -> Empty Activity

Native Android app Michael e. UU


Enter a name for Activity and layout and select finish. Here I
have entered ProfileActivity and activity_profile.

Native Android app Michael e. UU


Step 5: Add some component in the new activity

Let’s add some text to identify it as another Activity. We have added a TextView which displays
the text “Hello User”. Also, we added a Button which helps to go back to our Main Activity.

activity_profile.xml

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


<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android" xmlns:tools="
http://schemas.android.com/tools" android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center"
tools:context="com.learn2crack.activities.ProfileActivity">

Native Android app Michael e. UU


<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Hello User"
android:textAppearance="?android:attr/textAppearanceLarge"/>

<Button
android:id="@+id/bt_go_back"
android:layout_below="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="Go Back"
android:layout_centerHorizontal="true"
style="@style/Widget.AppCompat.Button.Colored"/>

</RelativeLayout>

Step 6: Launch Profile Activity

Open MainActivity.java file and create a method which launches the Activity. Here I have
created a method launchActivity() which has Intent defined and startActivity() method is
called to launch Activity. Now we need to call this launchActivity() method when the button is
pressed, For that we need to attach an OnClickListener to that Button. It can be done by using
the setOnClickListener() method. In this, we call the launchActivity() method.

Native Android app Michael e. UU


Step 7 : Going back to Main Activity

Now you want to Go back to Main Activity. Let’s do this. Similarly, attach an
OnClickListener to the Go Back button. Call the finish() method. The finish() is an Activity
method which destroys the Profile Activity and we will be back in Main Activity.

Native Android app Michael e. UU


Step 8 : Test and run the application

Native Android app Michael e. UU


Part III: Activity lifecycle Test Programming

Step 1: Following is the content of the modified main activity file


src/com.example.helloworld/MainActivity.java. This file includes each of the fundamental life
cycle methods. The Log.d() method has been used to generate log messages:

Native Android app Michael e. UU


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

public class MainActivity extends Activity {


String msg = "Android : ";

/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}

/** Called when the activity is about to become visible. */


@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() event");
}

/** Called when the activity has become visible. */


@Override
protected void onResume()
{ super.onResume();
Log.d(msg, "The onResume() event");
}

Native Android app Michael e. UU


/** Called when another activity is taking focus. */
@Override
protected void onPause() {
super.onPause();
Log.d(msg, "The onPause() event");
}

/** Called when the activity is no longer visible. */


@Override
protected void onStop() {
super.onStop();
Log.d(msg, "The onStop() event");
}

/** Called just before the activity is destroyed. */


@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}

Step 2: Check the Activity class


An activity class loads all the UI component using the XML file available in res/layout folder of
the project. Following statement loads UI components from res/layout/activity_main.xml file.
Every activity you define for your application must be declared in your AndroidManifest.xml file
setContentView(R.layout.activity_main);
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialspoint7.myapplication">

Native Android app Michael e. UU


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Step 3: Test the Activity cycle application

08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event 08-


23 10:32:07.683 4480-4480/com.example.helloworld D/Android :: The onStart() event 08-23
10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event

Let us try to click lock screen button on the Android emulator and it will generate following
events messages in LogCat window in android studio:

08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event


08-23 10:32:53.294 4480-4480/com.example.helloworld D/Android :: The onStop() event

Native Android app Michael e. UU


Let us again try to unlock your screen on the Android emulator and it will generate following
events messages in LogCat window in Android studio:

08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event 08-23


10:34:41.392 4480-4480/com.example.helloworld D/Android :: The onResume() event

Next, let us again try to click Back button on the Android emulator and it will generate
following events messages in LogCat window in Android studio and this completes the
Activity Life Cycle for an Android Application.

Native Android app Michael e. UU

You might also like