Android Chapter-2
Android Chapter-2
SURENDRANAGAR
Architecture of Android.
1|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
1. Linux Kernel:-
2. Native Libraries:-
The native libraries such as Media, WebKit, SQLite, OpenGL,
Free Type, C Runtime library (libc) etc. are situated on the top
of a Linux kernel.
3. Android Runtime:-
Android Runtime is the third section of the architecture and
situated on the second layer from the bottom.
2|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
4. Application Framework:-
5. Applications:-
3|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
4|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
5|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
(1) OnCreate()
(2) OnStart()
This method is called when activity becomes visible to the user.
(3) OnResume()
it is called by the system after OnCreate() method is fnished. This method
is called when activity will start interacting with the user.
(4) OnPause()
It is used to initialize fields, register listeners, bind to services etc .This
method is called when current activity is being paused and the previous activity
is being resumed.
(5) OnStop()
This method is called when activity is no longer visible to the user.
(6) OnRestart()
This method is called after your activity has stopped and restarting.
(7) OnDestroy()
This method is called before the activity is destroyed.
6|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
States of an Activity
(1) Running :-
This state defines that the activity is visible and interacts with the user.
(2) Paused :-
This state defines that the activity is still visible but partially hidden and the
instance is running but might be killed by the system.
(3) Stopped:-
This state defines that the activity is not visible and the instance is running
but might be killed by the system.
(4) Killed :-
This state defines that the activity has been terminated by the system call
to its finish() method.
7|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
8|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
The above diagram shows the basic building blocks of an Android application.
Android application in Eclipse or in any development tool have a pre-defined
structure with code and resource organized into a number of folders.
Src The 'src' stands for Source Code. It contains the Java Source
files.
Gen The 'gen' stands for Generated Java Library. This library is
for Android internal use only.
res The 'res' stands for Resource file. It can store resource files such
as pictures, XML files, etc. It contains some additional folders
such as Drawable, Layout and Values.
anim: It is used for XML files that are compiled into animation
objects.
color: It is used for XML files that describe colors.
drawable: It is used to store various graphics files. In Android
project structure,
9|Page
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
layout: It is used for placing the XML layout files, which defines
how various Android objects such as textbox, buttons, etc. are
organized on the screen.
raw: The 'raw' stands for Raw Asset Files. These files are
referenced from the application using a resource identifier in
the R class.
For example, good place for media is MP3 or Ogg files.
values: It is used for XML files which stores various string values,
such as titles, labels, etc.
AndroidManifest.xml This file indicates the Android definition file. This file contains
the information about the Android application such as minimum
Android version, permission to access Android device
capabilities such as Internet access permission, phone
permission etc.
10 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
default.properties This file contains the project settings, such as build the target.
Do not edit this file manually. It should be maintained in a
Source Revision Control System.
MainLayout.xml This file describes the layout of the page. So all the components
such as textboxes, labels, radio buttons, etc. are displaying on
the application screen.
Activity class The application occupies the entire device screen which needs
at least one class inherits from the Activity class. OnCreate()
method initiates the application and loads the layout page.
11 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
Android terminologies
1. XML
2. View
3. Layout
4. Activity
5. Emulator
12 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
6. Manifest file
7. Service
8. Broadcast Receiver
9. Content Providers
13 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
10.Intent
14 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
In this example, we will call the other activity class from another activity class using
explicit intent. Using intent, we will send the data from the first activity class to second
activity class. Second activity class gets this data and displays them in toast message.
15 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="First Activity"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
16 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
android:text="Click"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.77" />
</android.support.constraint.ConstraintLayout>
MainActivity.kt
package example.intent.com.kotlinexplicitintent
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
button.setOnClickListener() {
intent = Intent(this, SecondActivity::class.java)
intent.putExtra("id_value", id)
intent.putExtra("language_value", language)
startActivity(intent)
}
}
}
17 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
second_activity.xml
In the second_activity.xml file add the following code.
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="SecondActivity"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.107" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
18 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
</android.support.constraint.ConstraintLayout>
SecondActivity.kt
package example.intentt.com.kotlinexplicitintent
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_second.*
19 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
}
}
}
For example, if we want to share data using Intent, it invokes the relevant component to
fulfill the request.
20 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
In this example, we will invoke the URL using Implicit Intent clicking on Button.
activity_main.xml
Add the following code in activity_main.xml file. In this activity, we use a Button to
invoke Intent.
21 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
android:layout_height="match_parent"
tools:context="example.intent.com.kotlinimplicitintent.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Your First Activity"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="click to invoke intent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.77" />
</android.support.constraint.ConstraintLayout>
22 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
MaiActivity.kt
Add the following code in the MainActivity.kt class. In this class, we are invoking the
URL on clicking the button using Implicit Intent. To invoke this intent, we are passing
the action type and URL. The startActivity() method is used to start the Intent.
package example.intent.com.kotlinimplicitintent
import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
button.setOnClickListener(){
intent = Intent(Intent.ACTION_VIEW)
intent.setData(Uri.parse("https://www.google.com/"))
startActivity(intent)
/* intent= Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.
com/"))
startActivity(intent)*/
}
}
}
23 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
intent Filter
Implicit intent uses the intent filter to serve the user request.
The intent filter specifies the types of intents that an activity,
service, or broadcast receiver can respond.
Intent filters are declared in the Android manifest file.
Intent filter must contain <action>
activity_main.xml file.
<androidx.constraintlayout.widget.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=".MainActivity">
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.476"
24 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="0.153" />
<Button
android:id="@+id/buttonView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.826" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml File.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.menuapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
25 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
android:theme="@style/Theme.MenuApplication">
<activity android:name=".MainActivity">
<intent-filter>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http"/>
</intent-filter></activity></application></manifest>
26 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
MainActivity.kt file
package com.example.intentfilter
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sendButton.setOnClickListener {
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]")
startActivity(intent)
27 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
buttonView.setOnClickListener {
startActivity(intent)
Android BroadcastReceiver
When any of these events occur it brings the application into action by
either creating a status bar notification or performing a task.
28 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
Resource types
Each page in this section describe the usage, format, and syntax for a certain type
of app resource that you can provide in your project resources directory ( res/).
Animation Resources
Drawable Resources
Layout Resource
Menu Resource
String Resources
Define strings, string arrays, and plurals (and include string formatting and styling).
Saved in res/values/ and accessed from the R.string, R.array, and R.plurals classes.
Style Resource
29 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
Font Resources
Bool
Color
Dimension
ID
XML resource that provides a unique identifier for application resources and
components.
Integer
Integer Array
Typed Array
XML resource that provides a TypedArray (which you can use for an array of
drawables).
30 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
This is the required xml file for all the android application and located inside the root
directory.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manifest.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
31 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
</manifest>
<manifest>
manifest is the root element of the AndroidManifest.xml file. It has package attribute
that describes the package name of the activity class.
<application>
The commonly used attributes are of this element are icon, label, theme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components.
<activity>
<intent-filter>
intent-filter is the sub-element of activity that describes the type of intent to which
activity, service or broadcast receiver can respond to.
32 | P a g e
Prepared By : Bhartiba Parmar
SHREE SWAMI VIVEKANAND COLLEGE
SURENDRANAGAR
<action>
It adds an action for the intent-filter. The intent-filter must have at least one action
element.
<category>
33 | P a g e
Prepared By : Bhartiba Parmar