0% found this document useful (0 votes)
34 views25 pages

Configure ADT & Create Android Virtual Device

MAD

Uploaded by

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

Configure ADT & Create Android Virtual Device

MAD

Uploaded by

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

Practical No – 3

Configure Android Development Tools (ADT) plugins and Create Android Virtual Device.

VIII. Resources Required.

Sr No Instruments Specification Quantity Remark


1 Computer System Intel corei7, 8 GB 1 Net connection
DDR4 RAM, 1 TB HDD required to install the
emulator.
2 Android Studio Latest Version 1

IX.Practical Related Question

1. List Basic Requirement for Configure the Android OS.

Ans.

 Microsoft Windows 7/8/10 (32-bit or 64-bit)


 3 GB RAM minimum, 8 GB RAM recommended (plus 1 GB for the Android Emulator)
 2 GB of available disk space minimum, 4 GB recommended (500 MB for IDE plus 1.5 GB for Android
SDK and emulator system image)
 1280 x 800 minimum screen resolution.

2. Why byte code cannot run on Android.?

And.

ava bytecode in Java Archive (JAR) files is not executed by Android devices. Instead, Java classes are compiled into a
proprietary bytecode format and run on Dalvik (or compiled version thereof with newer ART), a specialized virtual
machine (VM) designed for Android. Unlike Java VMs, which are stack machines (stack-based architecture), the Dalvik
VM is a register machine (register-based architecture).this provides the highly optimized version of the byte code which
is compatible and easy to run on hand devices that has less memory and processor as compare to PC.

X. Exercise.

1. What is build type in gradle ?

Ans. Build Type refers to build and packaging settings like signing configuration for a project. For example, debug and
release build types. The debug will use android debug certificate for packaging the APK file. While, release build type will
use user-defined release certificate for signing and packaging the APK.

Source: https://frameboxxindore.com/android/what-is-a-build-type-in-gradle-in-android.html

2. Explain the build process in android .

Ans. The Android build system compiles app resources and source code, and packages them into APKs or Android App
Bundles that you can test, deploy, sign, and distribute. The output of the build is the same whether you are building a
project from the command line, on a remote machine, or using Android Studio.
Practical No – 4

Develop a program to display Hello World on the screen.


VIII. Resources Required.

Sr No Instruments Specification Quantity Remark


1 Android enable smart 2 GB RAM minimum 1 Net connection
phone/ android required to install the
version supporting emulator.
emulator

IX. Practical Related Question.

1. List the files used to write the Hello World program.

Ans.
- java : The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets
converted to a Dalvik executable and runs your application. Following is the default code generated by the application
wizard for Hello World! application −
- manifests :

Whatever component you develop as a part of your application, you must declare all its components in a manifest.xml
which resides at the root of the application project directory. This file works as an interface between Android OS and
your application, so if you do not declare your component in this file, then it will not be considered by the OS
- drawable : A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can
retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable
and android:icon .

- The strings.xml file is located in the res/values folder and it contains all the text that your application uses.
For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is
responsible for their textual content

- build.gradle an auto generated files.


- activity_main.xml :
- layout :The UI of our application will be designed in this file and it will contain Design and Text modes. It will exist in
the layouts folder and the structure of activity_main.xml file in Design mode

2. What is an activity in android programming

Ans. Android system initiates its program with in an Activity starting with a call on onCreate() callback method. There is
a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity
as shown in the below Activity life cycle diagram.

In android, Activity represents a single screen with a user interface (UI) of an application and it will acts an
entry point for users to interact with an app.
Generally, the android apps will contain multiple screens and each screen of our application will be an
extension of Activity class. By using activities, we can place all our android application UI components in a
single screen.

X. Execise.

1.Write a program to display Hello World.

Android Layout File (activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tutlane.helloworld.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Android Main Activity File (MainActivity.java)


package com.tutlane.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Android Manifest File (AndroidManifest.xml)


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.helloworld" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>

2. Write a program to display Student name and marks.


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/studName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student name: Amol Chaudhari"
android:textSize="24sp" />

<TextView
android:id="@+id/studMarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Marks: 75.00"
android:textSize="24sp" />
</LinearLayout>

MainActivity.java
package com.example.studentdetailsapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Practical No – 5.

Develop a program to implement a linear layout and absolute layout.

VIII. Resources Required.

Sr No Instruments Specification Quantity Remark


1 Android enable smart 2 GB RAM minimum 1 Net connection
phone/ android required to install the
version supporting emulator.
emulator

IX. Practical Related Question.

1. name any 3 layout manager.

Ans. A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as
determining the policy for when to recycle item views that are no longer visible to the user. By changing the
LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid,
staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use.

 LinearLayout.
 RelativeLayout.
 FrameLayout.
 GridView.
 ListView.

2. What is Card View ?

Ans. CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner
layout along with a specific elevation. CardView is the view that can display views on top of each other.

The main usage of CardView is that it helps to give a rich feel and look to the UI design. This widget can be easily seen in
many different Android Apps. CardView can be used for creating items in listview or inside Recycler View. The best part
about CardView is that it extends Framelayout and it can be displayed on all platforms of Android.
X. Execise.

1.Write a program to display name age and mobile number linearly (vertically )on screen using linear layout.

activity_main.xml
<?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:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
< TextView
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
< TextView
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"/>
</LinearLayout>

MainActivity.java
package com.tutlane.linearlayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


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

Develop a program to implement a Frame layout and Table layout and Relative Layout.

VIII. Resources Required.

Sr No Instruments Specification Quantity Remark


1 Android enable smart 2 GB RAM minimum 1 Net connection
phone/ android required to install the
version supporting emulator.
emulator

IX. Practical Related Question.

1. List different attributes which can be used with any layout manager.

Ans.
android:id: It uniquely identifies the Android Layout.
android:hint: It shows the hint of what to fill inside the EditText.
android:layout_height: It sets the height of the layout.
android:layout_width: It sets the width of the layout.
android:layout_marginTop: This is the extra space on the top side of the layout.
android:layout_marginBottom: This is the extra space on the bottom side of the layout.
android:layout_marginLeft: This is the extra space on the left side of the layout.
android:layout_marginRight: This is the extra space on the right side of the layout.
android:layout_gravity : This specifies how child Views are positioned.
android:layout_weight: This specifies how much of the extra space in the layout should be allocated to the View.
android:layout_x: This specifies the x-coordinate of the layout.
android:layout_y : This specifies the y-coordinate of the layout.
android:layout_width: This is the width of the layout.
android:paddingLeft: This is the left padding filled for the layout.
android:paddingRight: This is the right padding filled for the layout.
android:paddingTop: This is the top padding filled for the layout.
android:paddingBottom: This is the bottom padding filled for the layout.

2. What is Grid layout ?


Ans. Android GridView shows items in two-dimensional scrolling grid (rows & columns) and the grid items are
not necessarily predetermined but they automatically inserted to the layout using a ListAdapter

GridView Attributes:

Sr.No Attribute & Description

android:id
1
This is the ID which uniquely identifies the layout.
android:columnWidth
2
This specifies the fixed width for each column. This could be in px, dp, sp, in, or mm.

android:gravity
3 Specifies the gravity within each cell. Possible values are top, bottom, left, right, center,
center_vertical, center_horizontal etc.

android:horizontalSpacing
4 Defines the default horizontal spacing between columns. This could be in px, dp, sp, in, or
mm.

android:numColumns
5 Defines how many columns to show. May be an integer value, such as "100" or auto_fit
which means display as many columns as possible to fill the available space.

android:stretchMode

Defines how columns should stretch to fill the available empty space, if any. This must be
either of the values −

 none − Stretching is disabled.


6
 spacingWidth − The spacing between each column is stretched.

 columnWidth − Each column is stretched equally.

 spacingWidthUniform − The spacing between each column is uniformly


stretched..

android:verticalSpacing
7
Defines the default vertical spacing between rows. This could be in px, dp, sp, in, or mm.

X. Execise.

1. Write a program to display user basic information with location in a table using Table layout.

Ans.

Following will be the content of res/layout/activity_main.xml file –

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


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TableRow android:background="#0079D6" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="UserId" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Location" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Suresh Dasari" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hyderabad" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rohini Alavala" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guntur" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Trishika Dasari" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guntur" />
</TableRow>
</TableLayout>
1. Write a program to display Frame layout.

Ans.

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<TextView android:text="LeftTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="RightTop"
android:layout_gravity="top|right" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CentreTop"
android:layout_gravity="top|center_horizontal" />
<TextView android:text="Left"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Right"
android:layout_gravity="right|center_vertical" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Centre"
android:layout_gravity="center" />
<TextView android:text="LeftBottom"
android:layout_gravity="left|bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="RightBottom"
android:layout_gravity="right|bottom" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CenterBottom"
android:layout_gravity="center|bottom" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">

<ImageView
android:id="@+id/imgvw1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/img" />

<TextView
android:id="@+id/txtvw1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#286F24"
android:padding="10dp"
android:text="Login Details"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_marginLeft="100dp"/>

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:background="#ECEEE8"
android:padding="10dp"
android:hint="Enter your email" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#ECEEE8"
android:padding="10dp"
android:hint="Enter password"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "Submit"
android:layout_marginTop="240dp"
android:layout_marginLeft="110dp"/>
</FrameLayout>

Practical No – 16

Develop a program to implement Date Picker and Time Picker.

VIII. Resources Required.

Sr No Instruments Specification Quantity Remark


1 Android enable smart 2 GB RAM minimum 1 Net connection
phone/ android required to install the
version supporting emulator.
emulator

IX. Practical Related Question.


1. Write an XML time picker tag with its attributes.

Ans. <TimePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner OR clock "
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
2. List and explain the methods of time picker class.

Sr.No Method & description

1 is24HourView()
This method returns true if this is in 24 hour view else false

2 isEnabled()
This method returns the enabled status for this view

3 setCurrentHour(Integer currentHour)
This method sets the current hour

4 setCurrentMinute(Integer currentMinute)
This method sets the current minute

5 setEnabled(boolean enabled)
This method set the enabled state of this view

6 setIs24HourView(Boolean is24HourView)
This method set whether in 24 hour or AM/PM mode

7
setOnTimeChangedListener(TimePicker.OnTimeChangedListener
onTimeChangedListener)
This method Set the callback that indicates the time has been adjusted
by the user

3. List and explain any 5 methods of date pickers class.


X. Execise.

1. Write a program to implement a time picker with spinner mode.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context=".MainActivity">

<TimePicker
android:id="@+id/simpleTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="#090"
android:padding="20dp"
android:timePickerMode="spinner" />

<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Time Is ::"
android:textColor="#090"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

TextView time;
TimePicker simpleTimePicker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate the view's
time = (TextView) findViewById(R.id.time);
simpleTimePicker = (TimePicker) findViewById(R.id.simpleTimePicker);
simpleTimePicker.setIs24HourView(false); // used to display AM/PM mode
// perform set on time changed listener event
simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// display a toast with changed values of time picker
Toast.makeText(getApplicationContext(), hourOfDay + " " + minute, Toast.LENGTH_SHORT).show();
time.setText("Time is :: " + hourOfDay + " : " + minute); // set the current time in text view
}
});
}}

2. Write a program to display and select data and time on click of the select date and select time buttons respectively.

activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_date"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"
android:id="@+id/btn_date"
android:layout_alignBottom="@+id/in_date"
android:layout_toRightOf="@+id/in_date"
android:layout_toEndOf="@+id/in_date" />

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_time"
android:layout_below="@+id/in_date"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:id="@+id/btn_time"
android:layout_below="@+id/btn_date"
android:layout_alignLeft="@+id/btn_date"
android:layout_alignStart="@+id/btn_date" />

</RelativeLayout>

MainActivity.java
ublic class MainActivity extends AppCompatActivity implements
View.OnClickListener {

Button btnDatePicker, btnTimePicker;


EditText txtDate, txtTime;
private int mYear, mMonth, mDay, mHour, mMinute;

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

btnDatePicker=(Button)findViewById(R.id.btn_date);
btnTimePicker=(Button)findViewById(R.id.btn_time);
txtDate=(EditText)findViewById(R.id.in_date);
txtTime=(EditText)findViewById(R.id.in_time);

btnDatePicker.setOnClickListener(this);
btnTimePicker.setOnClickListener(this);

}
@Override
public void onClick(View v) {

if (v == btnDatePicker) {

// Get Current Date


final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this,


new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
if (v == btnTimePicker) {

// Get Current Time


final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

// Launch Time Picker Dialog


TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {

txtTime.setText(hourOfDay + ":" + minute);


}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
}
Practical No – 17

Develop a program to implement an Activity.

VIII. Resources Required.

Sr No Instruments Specification Quantity Remark


1 Android enable smart 2 GB RAM minimum 1 Net connection
phone/ android required to install the
version supporting emulator.
emulator

IX. Practical Related Question.

1. Draw the activity lifecycle diagram.


2. Give the hierarchy of file where you store the activity file.

Ans. res/layout folder

3. write the difference between

i. onStop() and onDestroy() and


onStop() is called when the activity is has already lost the focus and it is no longer in the screen. Once onStop() is
called then onRestart() can be called.
onDestroy() is last in the order after onStop(). onDestory() is called just before an activity is destroyed and after
that it is gone it is not possible to resurrect this
ii. onPause() and onResume()
onResume() Called just before the activity starts interacting with the user. At this point the activity is at the top
of the activity stack, with user input going to it. Always followed by onPause().

onPause() Called when the system is about to start resuming another activity. This method is typically used to
commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU,

X. Execise.

1. write a program to create HelloWorld Activity using all life cycle methods to display message using log. d

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.activitylifecycle.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}

Output:

You will not see any output on the emulator or device. You need to open logcat.

You might also like