AMP Practical Journal
AMP Practical Journal
0
INTRODUCTION TO ANDROID, INTRODUCTION TO ANDROID
STUDIO IDE, APPLICATION FUNDAMENTALS
Creating a Project:
Step 1: Click on New Project.
1
Step 2: Select Empty Views Activity and click on Next.
Step 3: Give Name to your project, select Language as Kotlin, select Minimum
SDK and click on Finish.
2
activity_main.xml
MainActivity.kt
3
1. Activities: Activities are said to be the presentation layer of our
applications. The UI of our application is built around one or more
extensions of the Activity class. By using Fragments and Views, activities
set the layout and display the output and also respond to the user’s
actions. An activity is implemented as a subclass of class Activity.
4
6. Widgets: These are the small visual application components that you can
find on the home screen of the devices. They are a special variation of
Broadcast Receivers that allow us to create dynamic, interactive
application components for users to embed on their Home Screen.
7. Notifications: Notifications are the application alerts that are used to
draw the user’s attention to some particular app event without stealing
focus or interrupting the current activity of the user. They are generally
used to grab user’s attention when the application is not visible or active,
particularly from within a Service or Broadcast Receiver. Examples: E-
mail popups, Messenger popups, etc.
5
Creating Android Virtual Device:
Step 1: Click on Device Manager and click on Create Virtual Device button
6
Step 2: Select a Hardware (device) and click on Next.
7
Step 4: Click on Finish.
8
MainActivity.kt
OUTPUT:
9
PRACTICAL NO. 1
PROGRAMMING RESOURCE
1. Color:
Launch your android studio and create a new project.
Select app under Android, select res under app, select values under res and
open colors.xml. Define your colours in colors.xml file to use them in your
project.
2. Theme:
Open themes.xml file from themes folder inside values directory of your
android project.
DayNight NoActionBar Theme:
10
DayNight DarkActionBar:
Customizing Theme:
Adding colorPrimary and colorPrimaryDark to DayNight DarkActionBar
Theme:
OUTPUT:
11
3. String:
Open strings.xml from values directory of your android project. Define
your strings in strings.xml file to use them in your project.
4. Drawable:
Copy any image (.jpg, .png, .gif) which you wish to use in the project and
right click on the drawable folder under res directory and click on paste to
add that in drawable.
5. Dimension:
A dimension value defined in XML. A dimension is specified with
a number followed by a unit of measure, such as 10px, 2in, or 5sp. The
following units of measure are supported by Android:
dp: Density-independent pixels: an abstract unit that is based on the
physical density of the screen.
sp: Scale-independent Pixels - This is like the dp unit, but it is also scaled
by the user's font size preference.
pt: Points: 1/72 of an inch based on the physical size of the screen,
assuming a 72 dpi density screen.
px: Pixels: corresponds to actual pixels on the screen.
12
mm: Millimeters: based on the physical size of the screen.
in: Inches: based on the physical size of the screen.
activity_main.xml
13
OUTPUT:
14
PRACTICAL NO. 2
PROGRAMMING ACTIVITIES AND FRAGMENTS
Activity Lifecycle:
In Android, an activity is referred to as one screen in an application. It is very
similar to a single window of any desktop application. An Android app consists of one or
more screens or activities.
Each activity goes through various stages or a lifecycle and is managed by
activity stacks. So when a new activity starts, the previous one always remains below it.
There are four stages of an activity.
1. If an activity is in the foreground of the screen i.e at the top of the stack, then it is said
to be active or running. This is usually the activity that the user is currently interacting
with.
2. If an activity has lost focus and a non-full-sized or transparent activity has focused on
top of your activity. In such a case either another activity has a higher position in multi-
window mode or the activity itself is not focusable in the current window mode. Such
activity is completely alive.
3. If an activity is completely hidden by another activity, it is stopped or hidden. It still
retains all the information, and as its window is hidden thus it will often be killed by the
system when memory is needed elsewhere.
4. The system can destroy the activity from memory by either asking it to finish or simply
killing its process. When it is displayed again to the user, it must be completely
restarted and restored to its previous state.
For each stage, android provides us with a set of 7 methods that have their
own significance for each stage in the life cycle. The image shows a path of migration
whenever an app switches from one state to another.
15
16
Demo Application on Lifecycle of an Activity:
activity_main.xml
MainActivity.kt
17
OUTPUT:
Open Logcat to see output.
After application launched on emulator, onCreate, onStart and onResume are invoked.
After click on the HOME Button, onPause is invoked and after a while onStop is invoked.
After relaunching the application, onRestart, onStart and onResume are invoked.
18
After clicking on the back button, onPause, onStop and onDestroy are invoked.
19
Multiple Activities:
Step 1: Create a new project.
Step 2: Add 2 more activities to the project.
Right click on app Click on New Click on Activity Click on Empty View
Activity
20
activity_main.xml activity_second.xml activity_third.xml
21
activity_main.xml
MainActivity.kt
22
activity_second.xml
SecondActivity.kt
23
activity_third.xml
ThirdActivity.kt
24
OUTPUT:
Lifecycle of Fragment:
In Android, the fragment is the part of the Activity that represents a portion
of the User Interface(UI) on the screen. It is the modular section of the Android activity that
is very helpful in creating UI designs that are flexible in nature and auto-adjustable based on
the device screen size. The UI flexibility on all devices improves the user experience and
adaptability of the application. that can exist only inside an activity as its lifecycle is
dependent on the lifecycle of the host activity. For example, if the host activity is paused,
then all the methods and operations of the fragment related to that activity will stop
functioning, the fragment is also termed a sub-activity. Fragments in Android can be added,
removed, or replaced dynamically i.e., while the activity is running.
Note: <fragment> tag is used to insert the fragment in an android activity layout. By dividing
the activity’s layout multiple fragments can be added in it.
Below is the pictorial representation of fragment interaction with the activity:
25
Types of Android Fragments:
1. Single Fragment: Display only one single view on the device screen. This type of
fragment in android is mostly used for mobile phones.
2. List Fragment: This Fragment is used to display a list-view from which the user can
select the desired sub-activity. The menu drawer of apps like Gmail is the best
example of this kind of android fragment.
3. Fragment Transaction: This kind of fragments in android supports the transition
from one fragment in android to another at run time. Users can switch between
multiple fragments like switching tabs.
Android Fragment Lifecycle:
Methods Description
onAttach() The very first method to be called when the fragment has been
associated with the activity. This method executes only once during
the lifetime of a fragment.
When we attach fragment(child) to Main(parent) activity then it call
first and then not call this method any time(like you run an app and
close and reopen) simple means that this method call only one time.
onCreate() This method initializes the fragment by adding all the required
attributes and components.
onCreateView() System calls this method to create the user interface of the fragment.
The root of the fragment’s layout is returned as the View component
by this method to draw the UI.
You should inflate your layout in onCreateView but shouldn’t
initialize other views using findViewById in onCreateView.
onViewCreated() It indicates that the activity has been created in which the fragment
exists. View hierarchy of the fragment also instantiated before this
function call.
26
onStart() The system invokes this method to make the fragment visible on the
user’s device.
onResume() This method is called to make the visible fragment interactive.
onPause() It indicates that the user is leaving the fragment. System call this
method to commit the changes made to the fragment.
onStop() Method to terminate the functioning and visibility of fragment from
the user’s screen.
onDestroyView() System calls this method to clean up all kinds of resources as well as
view hierarchy associated with the fragment. It will call when you can
attach new fragment and destroy existing fragment Resoruce
onDestroy() It is called to perform the final clean up of fragment’s state and its
lifecycle.
onDetach() The system executes this method to disassociate the fragment from its
host activity.
It will call when your fragment Destroy(app crash or attach new
fragment with existing fragment)
27
PRACTICAL NO. 3
PROGRAMS RELATED TO DIFFERENT LAYOUTS
Linear Layout (Horizontal):
activity_main.xml
28
OUTPUT:
29
Linear Layout (Vertical):
activity_main.xml
30
OUTPUT:
31
Linear Layout (Horizontal & Vertical):
activity_main.xml
32
OUTPUT:
33
Table Layout:
activity_main.xml
34
OUTPUT:
35
Relative Layout:
activity_main.xml
36
OUTPUT:
37
Absolute Layout:
activity_main.xml
38
OUTPUT:
39
Frame Layout:
activity_main.xml
40
OUTPUT:
41
Grid Layout:
activity_main.xml
42
43
OUTPUT:
44
List View:
activity_main.xml:
45
MainActivity.kt
Output:
46
PRACTICAL NO. 4
APPBAR AND MENU
Step 1: Add colour for Appbar in color.xml
color.xml
47
Step 3: Create and Add a menu
(i) Right click on app select New select Android Resource
Directory
(ii) A new window will open, put menu for both Directory name and
Resource type and then press OK.
48
(iii) After that under res, menu directory will appear,
Right click on menu select New select Menu Resource File
(iv) A new window will open, put File name menu and then press OK.
49
Step 4: Add Search and File as menu items in menu.xml
menu.xml
Step 5:
MainActivity.kt
50
OUTPUT:
51
Steps to add menu.xml file:
1. Right click on res folder and then on New, and select Android Resource Directory (A
new window will open)
2. Set directory name as menu and resource type as menu, and then press on OK.
3. After that under res directory, right click on menu folder and then on New, and then
on Menu Resource File (A new window will open)
4. Set file name as option_menu and press OK.
option_menu.xml
activity_main.xml
52
MainActivity.kt
OUTPUT:
53
Steps to add menu.xml file:
1. Right click on res folder and then on New, and select Android Resource Directory (A
new window will open)
2. Set directory name as menu and resource type as menu, and then press on OK.
3. After that under res directory, right click on menu folder and then on New, and then
on Menu Resource File (A new window will open)
4. Set file name as option_menu and press OK.
option_menu.xml
activity_main.xml
54
MainActivity.kt
OUTPUT:
55
PRACTICAL NO. 5
ALERT DIALOG BOX
MainActivity.kt
Output:
56
PRACTICAL NO. 6 (A)
THE ANDROID INTENT CLASS
SENDING DATA FROM ONE ACTIVITY TO ANOTHER ACTIVITY
activity_main.xml
57
MainActivity.kt
58
activity_main2.xml
59
MainActivity2.kt
Output:
60
SENDING DATA FROM ONE APPLICATION TO ANOTHER
APPLICATION
DataSender Application
activity_main.xml
61
MainActivity.kt
62
DataReceiver Application
activity_main.xml
MainActivity.kt
63
AndroidMenifest.xml
64
OUTPUT:
65
PRACTICAL NO. 6 (B)
EVENT LISTENERS
activity_main.xml
66
MainActivity.kt
67
OUTPUT:
68
activity_main.xml
69
MainActivity.kt
70
OUTPUT:
71
72
PRACTICAL NO. 7 (A)
SERVICES
Create a Service
Adding a Kotlin Class to the Project
Right click on com.example.backgroundservice Click on New Click on
Kotlin Class/File
A new window will open, give a name to the new Kotlin Class (MyService)
MyService.kt
73
activity_main.xml
74
AndroidManifest.xml
75
MainActivity.kt
OUTPUT:
76
PRACTICAL NO. 7 (B)
BROADCAST RECEIVERS
Create a BroadcastReceiver
Adding a Kotlin Class to the Project
Right click on com.example.broadcastapplication Click on New Click on
Kotlin Class/File
A new window will open, give a name to the new Kotlin Class
(AirplaneModeReceiver)
AirplaneModeReceiver.kt
77
activity_main.xml
MainActivity.kt
78
OUTPUT:
79
PRACTICAL NO. 8
NOTIFICATION
activity_main.xml
activity_after_notification.xml
80
MainActivity.kt
81
OUTPUT:
82
PRACTICAL NO. 9
MEDIA API
Adding audio to the Project
Right click on app Click on New Click on Android Resource Directory
A new window will open, give Directory name raw and select Resource type
raw and click on OK
83
Copy your audio file (.mp3) and right click on raw folder to paste.
This prompt will come, click on OK.
84
Adding Image to the Project
Copy image and right click on drawable to paste.
This prompt will come, click on OK.
85
activity_main.xml
86
MainActivity.kt
87
88
Output:
89
After clicking on the Pause button After clicking on the Stop button
90
PRACTICAL NO. 10
PERMISSION
AndroidManifest.xml
MainActivity.kt
91
OUTPUT:
92