Ex.1 Activity Life Cycle-Updated
Ex.1 Activity Life Cycle-Updated
1|Page
2. In the New Project screen, enter the following values:
a. Application Name: "MyFirstApp"
b. Company Domain: "example.com"
3. Android Studio fills in the package name and project location for you, but you can edit
these if you'd like, Click Next, as shown below
2|Page
4. In the Target Android Devices screen, keep the default values and click Next, as shown
below
The Minimum Required SDK is the earliest version of Android that your app supports,
which is indicated by the API level. To support as many devices as possible, you should set
this to the lowest version available that allows your app to provide its core feature set. If any
feature of your app is possible only on newer versions of Android and it's not critical to the
core feature set, enable that feature only when running on the versions that support it
(see Supporting Different Platform Versions).
5. In the Add an Activity to Mobile screen, select Empty Activity and click Next, as
shown below
3|Page
6. In the Customize the Activity screen, keep the default values and click Finish, as shown
below
4|Page
7. You see the screen as shown below
After some processing, Android Studio opens and displays a "Hello World" app
with default files. You will add functionality to some of these files in the following
tutorial.
Now take a moment to review the most important files. First, be sure that
the Project window is open (select View → Tool Windows →Project) and
the Android view is selected from the drop-down list at the top. You can then see the
following files:
The m anifest
file describes the fundamental characteristics of the app and defines each of its
components. You'll revisit this file as you follow these lessons and add more components
to your app, as shown below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell.basicactivitylifecycle">
<application
android:allowBackup="true"
5|Page
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" />
</manifest>
This file appears in Android Studio after the New Project wizard finishes. It
contains the class definition for the activity you created earlier. When you build and run
the app, the Activity starts and loads the layout file that says "Hello World!"
This XML file defines the layout of the activity. It contains a TextView element
with the text “Hello world!”.
6|Page
Res folder has many other folders as shown above, drawable, layout which we
have discussed above, mipmap and values.
Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists,
shapes, animation drawable. They are saved in res/drawable/ and accessed from
the R.drawable class.
app →res →mipmap
Launcher icons always go into mipmap folder. Images, which are often scaled up (or
extremely scaled down) and whose quality is critical for the app, go into mipmap folder as
well. All other images are usual drawables.
app →res →values
7|Page
Values folder has color, dimens, strings and style xml files, each has a separate
properties, as shown below.
app →res →values→colors.xml
colors.xml files that define a state list of colors. They are saved in res/color/ and accessed
from the R.color class, as shown below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
8|Page
styles.xml for styles, and accessed from the R.style class, as shown below
<resources>
</resources>
B.Layout
1. Click app → res → layout →activity_main.xml, statement will be as shown
below
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to My First App"
android:textSize="30dp"/>
</RelativeLayout>
C.Coding
1. Click app →java →com.example.myfirstapp →MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
9|Page
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2. Add the statements which are highlighted in bold, to see the activity life cycle
operation
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
10 | P a g e
public void onStop() {
super.onStop();
Log.d(tag, "In the onStop() event");
}
Note: On Android 4.2 and newer, Developer options is hidden by default. To make it
available, go to Settings →About phone and tap Build number seven times. Return
to the previous screen to find Developer options.
1. In Android Studio, select your project and click Run from the toolbar.
2. In the Select Deployment Target window, select your device, and click OK.
Android Studio installs the app on your connected device and starts it.
Run on an Emulator
Before you run your app on an emulator, you need to create an Android Virtual
Device (AVD) definition. An AVD definition defines the characteristics of an Android phone,
tablet, Android Wear, or Android TV device that you want to simulate in the Android Emulator.
Create an AVD Definition as follows:
1. Launch the Android Virtual Device Manager by selecting Tools →Android →AVD
11 | P a g e
3. In the Select Hardware screen, select a phone device, such as Nexus 6, and then
click Next.
4. In the System Image screen, choose the desired system image for the AVD and
click Next.
If you don't have a particular system image installed, you can get it by clicking
the download link.
5. Verify the configuration settings (for your first AVD, leave all the settings as they are),
and then click Finish.
For more information about using AVDs, see Create and Manage Virtual Devices.
12 | P a g e
Run the app from Android Studio as follows:
1. In Android Studio, select your project and click Run from the toolbar.
2. In the Select Deployment Target window, select your emulator and click OK.
It can take a few minutes for the emulator to start. You may have to unlock the screen. When
you do, My First App appears on the emulator screen.
13 | P a g e
SCREEN SHOTS:
Select the My First App in the Menu as shown below
Output is viewed in Android Monitor, click the Android Monitor tab in below of the window
14 | P a g e