0% found this document useful (0 votes)
10 views

Android

Uploaded by

zainfayyaz498
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Android

Uploaded by

zainfayyaz498
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

What is an android?

Android is an open source and Linux-based Operating System for mobile


devices such as smartphones and tablet computers. Android was developed by
the Open Handset Alliance, led by Google, and other companies.
Android offers a unified approach to application development for mobile devices
which means developers need only develop for Android, and their applications
should be able to run on different devices powered by Android.

What is the architecture of android?

android architecture or Android software stack is categorized into five parts:

1. linux kernel
2. native libraries (middleware),
3. Android Runtime
4. Application Framework
5. Applications

1) Linux kernel

It is the heart of android architecture that exists at the root of android


architecture. Linux kernel is responsible for device drivers, power management, memory
management, device management and resource access.

2) Native Libraries

On the top of linux kernel, their are Native libraries such as WebKit, OpenGL,
FreeType, SQLite, Media, C runtime library (libc) etc.

The WebKit library is responsible for browser support, SQLite is for database,
FreeType for font support, Media for playing and recording audio and video formats.

3) Android Runtime

In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which
is responsible to run android application. DVM is like JVM but it is optimized for mobile
devices. It consumes less memory and provides fast performance.
4) Android Framework

On the top of Native libraries and android runtime, there is android framework.
Android framework includes Android API's such as UI (User Interface), telephony,
resources, locations, Content Providers (data) and package managers. It provides a lot of
classes and interfaces for android application development.

5) Applications

On the top of android framework, there are applications. All applications such as
home, contact, settings, games, browsers are using android framework that uses android
runtime and libraries. Android runtime and native libraries are using linux kernal.

Different Versions of android OS

Android versions, name, and API level

Code name Version numbers API level Release date

No codename 1.0 1 September 23, 2008

No codename 1.1 2 February 9, 2009

Cupcake 1.5 3 April 27, 2009

Donut 1.6 4 September 15, 2009

Eclair 2.0 - 2.1 5-7 October 26, 2009

Froyo 2.2 - 2.2.3 8 May 20, 2010

Gingerbread 2.3 - 2.3.7 9 - 10 December 6, 2010

Honeycomb 3.0 - 3.2.6 11 - 13 February 22, 2011

Ice Cream Sandwich 4.0 - 4.0.4 14 - 15 October 18, 2011


Jelly Bean 4.1 - 4.3.1 16 - 18 July 9, 2012

KitKat 4.4 - 4.4.4 19 - 20 October 31, 2013

Lollipop 5.0 - 5.1.1 21- 22 November 12, 2014

Marshmallow 6.0 - 6.0.1 23 October 5, 2015

Nougat 7.0 24 August 22, 2016

Nougat 7.1.0 - 7.1.2 25 October 4, 2016

Oreo 8.0 26 August 21, 2017

Oreo 8.1 27 December 5, 2017

Pie 9.0 28 August 6, 2018

Android 10 10.0 29 September 3, 2019

Android 11 11 30 September 8, 2020

Android Project Structure


The android project contains different types of app modules, source code files, and
resource files. We will explore all the folders and files in the android app.

1. Manifests Folder
2. Java Folder
3. res (Resources) Folder
• Drawable Folder
• Layout Folder
• Mipmap Folder
• Values Folder
4. Gradle Scripts
1. Manifests Folder
Manifests folder contains AndroidManifest.xml for creating our android application.
This file contains information about our application such as the Android version,
metadata, states package for Kotlin file, and other application components. It acts as an
intermediator between android OS and our application.

2. Java folder
The Java folder contains all the java and Kotlin source code (.java) files that we create
during the app development, including other Test files.

3. Resource (res) folder


The resource folder is the most important folder because it contains all the non-code
sources like images, XML layouts, and UI strings for our android application.

res/drawable folder
It contains the different types of images used for the development of the
application. We need to add all the images in a drawable folder for the application
development.

res/layout folder
The layout folder contains all XML layout files which we used to define the user
interface of our application. It contains the activity_main.xml file.

res/mipmap folder
This folder contains launcher.xml files to define icons that are used to show on the
home screen. It contains different density types of icons depending upon the size of the
device such as hdpi, mdpi, xhdpi.

res/values folder
Values folder contains a number of XML files like strings, dimensions, colors, and
style definitions. One of the most important files is the strings.xml file which contains the
resources.

4. Gradle Scripts folder


Gradle means automated build system and it contains a number of files that are
used to define a build configuration that can be applied to all modules in our application.
In build.gradle (Project) there are buildscripts and in build.gradle (Module) plugins and
implementations are used to build configurations that can be applied to all our
application modules.

What are the components of an android application?


There are following four main components that can be used within an Android
application −

Main Components
1. Activities

They dictate the UI and handle the user interaction to the smart phone screen.

2. Services

They handle background processing associated with an application.

3. Broadcast Receivers

They handle communication between Android OS and applications. Broadcast


Receivers simply respond to broadcast messages from other applications or from the
system.

4. Content Providers

They handle data and database management issues. A content provider component
supplies data from one application to others on request.

Additional Components
There are additional components which will be used in the construction of above
mentioned entities, their logic, and wiring between them. These components are −

1. Fragments

Represents a portion of user interface in an Activity.

2. Views

UI elements that are drawn on-screen including buttons, lists forms etc.

3. Layouts

View hierarchies that control screen format and appearance of the views.
4. Intents

Messages wiring components together.

5. Resources

External elements, such as strings, constants and drawable pictures.

6. Manifest

Configuration file for the application.

AndroidManifest.xml file in android


The AndroidManifest.xml file contains information of your package, including
components of the application such as activities, services, broadcast receivers, content providers
etc.

It performs some other tasks also:

o It is responsible to protect the application to access any protected parts by providing


the permissions.
o It also declares the android api that the application is going to use.
o It lists the instrumentation classes. The instrumentation classes provides profiling and
other informations. These informations are removed just before the application is
published etc.

What is an activity?
An activity represents a single screen with a user interface just like window. It has a
lifecycle that includes methods such as onCreate(), onStart(), onResume(), onPause(), onStop(),
and onDestroy().

What is an activity life cycle?


onCreate()
This is the first callback and called when the activity is first created.

onStart()
This callback is called when the activity becomes visible to the user.
onResume()
This is called when the user starts interacting with the application.

onPause()
The paused activity does not receive user input and cannot execute any code and called
when the current activity is being paused and the previous activity is being resumed.

onStop()
This callback is called when the activity is no longer visible.

onDestroy()
This callback is called before the activity is destroyed by the system.

onRestart()
This callback is called when the activity restarts after stopping it.

Android Explicit Intent


Android Explicit intent specifies the component to be invoked from activity. In other
words, we can call another activity in android by explicit intent.

We can also pass the information from one activity to another using explicit intent.

MainActivity.java
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
intent.putExtra("Key","Enter Data Here");
startActivity(intent);

AnotherActivity.java
Intent intent = getIntent();
String s=intent.getStringExtra("Key"));

Resource Type
Animation resources
Define pre-determined animations.
Tween animations are saved in res/anim/ and accessed from the R.anim class.
Frame animations are saved in res/drawable/ and accessed from
the R.drawable class.
Color state list resource
Define a color resource that changes based on the View state.
Saved in res/color/ and accessed from the R.color class.
Drawable resources
Define various graphics with bitmaps or XML.
Saved in res/drawable/ and accessed from the R.drawable class.
Layout resource
Define the layout for your application UI.
Saved in res/layout/ and accessed from the R.layout class.
Menu resource
Define the contents of your application menus.
Saved in res/menu/ and accessed from the R.menu class.
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
Define the look and format for UI elements.
Saved in res/values/ and accessed from the R.style class.
Font resources
Define font families and include custom fonts in XML.
Saved in res/font/ and accessed from the R.font class.
More resource types
Define other primitive values as static resources, including the following:

Bool

XML resource that carries a boolean value.

Color

XML resource that carries a hexadecimal color value.


Dimension

XML resource that carries a dimension value with a unit of measure.

ID

XML resource that provides a unique identifier for application


resources and components.

Integer

XML resource that carries an integer value.

Integer array

XML resource that provides an array of integers.

Typed array

XML resource that provides a TypedArray, which you can use for an
array of drawables.

How to use these resources?


Accessing Resources in Code

When your Android application is compiled, a R class gets


generated, which contains resource IDs for all the resources available
in your res/ directory. You can use R class to access that resource
using sub-directory and resource name or directly resource ID.

Example

To access res/drawable/myimage.png and set an ImageView you will use


following code −

ImageView imageView = (ImageView)


findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

Here first line of the code make use of R.id.myimageview to get ImageView
defined with id myimageview in a Layout file. Second line of code makes use
of R.drawable.myimage to get an image with name myimage available in
drawable sub-directory under /res.

Example

Consider next example where res/values/strings.xml has following definition-

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


<resources>
<string name="hello">Hello, World!</string>
</resources>

Now you can set the text on a TextView object with ID msg using a resource ID as
follows −

TextView msgTextView = (TextView) findViewById(R.id.msg);


msgTextView.setText(R.string.hello);
Example

Consider a layout res/layout/activity_main.xml with the following definition −

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />

<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />

</LinearLayout>

This application code will load this layout for an Activity, in the onCreate()
method as follows −
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Accessing Resources in XML

Consider the following resource XML res/values/strings.xml file that


includes a color resource and a string resource −

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


<resources>
<color name="opaque_red">#f00</color>
<string name="hello">Hello!</string>
</resources>

Now you can use these resources in the following layout file to set the text
color and text string as follows −

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


<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="@string/hello" />

EditText
Purpose: Used for accepting user input in the form of text.
Key Attributes:
android:inputType: Specifies the type of input expected (text, number, email, etc.).
android:hint: Provides a hint or placeholder text.
android:maxLength: Limits the maximum number of characters that can be entered.
android:password: Indicates whether the input should be treated as a password
(characters are masked).
Example:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Enter text here"/>

TextView
Purpose: Used for displaying text to the user.
Key Attributes:
android:text: Sets the text to be displayed.
android:textSize: Specifies the size of the text.
android:textColor: Defines the color of the text.
android:gravity: Controls the alignment of the text within the TextView.
Example:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="18sp"
android:textColor="#000000"
android:gravity="center"/>
Button
Purpose: Used to trigger an action when clicked.
Key Attributes:
android:text: Sets the text displayed on the button.
android:onClick: Specifies the method to be called when the button is clicked.
Example:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="onButtonClick"/>

You might also like