0% found this document useful (0 votes)
39 views14 pages

Ex.1 Activity Life Cycle-Updated

The document provides an overview of the anatomy of an Android app project created in Android Studio. It describes the key files and folders in the project structure including the manifest file, Java activity file, layout files, and resource folders. It also provides basic steps for creating a new project, adding a layout, coding the activity, and running the app on a real device or emulator.

Uploaded by

Swetha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views14 pages

Ex.1 Activity Life Cycle-Updated

The document provides an overview of the anatomy of an Android app project created in Android Studio. It describes the key files and folders in the project structure including the manifest file, Java activity file, layout files, and resource folders. It also provides basic steps for creating a new project, adding a layout, coding the activity, and running the app on a real device or emulator.

Uploaded by

Swetha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Anatomy of Android

What you’ll learn after finishing this exercise

➢ Learn the android activity life cycle


➢ How to use basic layout effectively
➢ How to create an Emulator and run the project.

A.Create a New Project


1. In Android Studio, create a new project:
a. If you don't have a project opened, in the Welcome to Android Studio window,
click Start a new Android Studio project.
b. If you have a project opened, select File > New Project, as shown below

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:

app →manifests →AndroidManifest.xml

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" />

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


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

</manifest>

app →java →com.example.myfirstapp →MainActivity.java

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!"

app →res →layout →activity_main.xml

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.

app →res →drawable

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>

app →res →values→strings.xml


strings.xml for string values, and accessed from the R.string class, as shown below
<resources>
<string name="app_name">BasicActivityLifeCycle</string>
</resources>

app →res →values→dimens.xml


dimens.xml for dimension values, and accessed from the R.dimen class, as shown below
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

app →res →values→styles.xml

8|Page
styles.xml for styles, and accessed from the R.style class, as shown below
<resources>

<!-- Base application theme. -->


<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

B.Layout
1. Click app → res → layout →activity_main.xml, statement will be as shown
below

<?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_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<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;

public class MainActivity extends AppCompatActivity {

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;

public class MainActivity extends AppCompatActivity {

String tag = "Events";

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

public void onStart() {


super.onStart();
Log.d(tag, "In the onStart() event");
}

public void onRestart() {


super.onRestart();
Log.d(tag, "In the onRestart() event");
}

public void onResume() {


super.onResume();
Log.d(tag, "In the onResume() event");
}

public void onPause() {


super.onPause();
Log.d(tag, "In the onPause() event");
}

10 | P a g e
public void onStop() {
super.onStop();
Log.d(tag, "In the onStop() event");
}

public void onDestroy() {


super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
}

D.Basic Steps to run a project


Run on a Real Device
Set up your device as follows:
1. Connect your device to your development machine with a USB cable. If you're
developing on Windows, you might need to install the appropriate USB driver for your
device. For help installing drivers, see the OEM USB Drivers document.
2. Enable USB debugging on your device by going to Settings →Developer options.

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.

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 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

Manager, or by clicking the AVD Manager icon in the toolbar.


2. In the Your Virtual Devices screen, click Create Virtual Device.

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

Output will be as shown below,

14 | P a g e

You might also like