Unit I and II - VAC - Android Programming
Unit I and II - VAC - Android Programming
1.1 INTRODUCTION
We must remember that the main reason why the personal computer was so successful and so
popular in the world is because of the time when that technology came. At that time, the growth of
the industry and the interest of the people led to the great success of this Operating System. Microsoft
will play a major role in bringing this technology to the masses in a simple and elegant way.
As mentioned above the reason for the success of Android OS is the rapidly evolving smart
phone technology and the interest of the people. Its success can be achieved in three short years.
The results of the Nielsen survey announced in 2011 show that Android has increased its
business market by 36 percent, followed by Apple ios at 26 percent. At the end of 2012, Android
controlled 50 percent of the global smart phone market, the rest 50 percent being divided by the all
other systems. Finally, 77 percent of the population utilizing the Android OS.
What is Android, and where did it come from?
HISTORY OF ANDROID
By collaboration of the four members - Andy Rubin, Rich Miner, Nick Sears, and Chris White,
Android Inc. was founded at Palo Alto, California, in October 2003.
Rubin described the Android project as "tremendous potential in developing smarter mobile
devices that are more aware of its owner's location and preferences".
Initially, the purpose of investors was to create this Android OS for the digital camera, in April
2004.
The company then decided that the market for cameras was not large enough for its goals, and
by five months later it had diverted its efforts and was pitching Android as a handset operating
system that would rival Symbian and Microsoft Windows Mobile.
Rubin had difficulty attracting investors early on, and Android was facing eviction from its office
space. Steve Perlman, a close friend of Rubin, brought him $10,000 in cash in an envelope, and
shortly thereafter wired an undisclosed amount as seed funding.
In July 2005, Google acquired Android Inc. for at least $50 million.
Its key employees, including Rubin, Miner and White, joined Google as part of the acquisition.
Speculation about Google's intention to enter the mobile communications market continued
to build through December 2006.
An early prototype had a close resemblance to a BlackBerry phone, with no touch screen and a
physical QWERTY keyboard, but the arrival of 2007's Apple iPhone meant that Android "had to
go back to the drawing board".[23][24] Google later changed its Android specification
documents to state that "Touch screens will be supported", although "the Product was
designed with the presence of discrete physical buttons as an assumption, therefore a touch
screen cannot completely replace physical buttons".
By 2008, both Nokia and BlackBerry announced touch-based smart phones to rival the iPhone
3G, and Android's focus eventually switched to just touch screens. The first commercially
available Smartphone running Android was the HTC Dream, also known as T-Mobile G1,
announced on September 23, 2008.
On November 5, 2007, the Open Handset Alliance, a consortium of technology companies
including Google, device manufacturers such as HTC, Motorola and Samsung, wireless carriers
such as Sprint and T-Mobile, and chipset makers such as Qualcomm and Texas Instruments,
unveiled itself, with a goal to develop "the first truly open and comprehensive platform for
mobile devices". Within a year, the Open Handset Alliance faced two other open source
competitors, the Symbian Foundation and the LiMo Foundation, the latter also developing a
Linux-based mobile operating system like Google.
1. KERNEL
Computer program – core of computer’s operating system.
Computer controls of everything inside of the system.
First program to be loaded on startup and it handles the rest of the startup process.
It handle the CPU, Memory.
Manages IO devices.
Communicates with other processes through system calls.
System calls occurs when other processes want to do something that is hardware related.
4. BINDER
Binder allows inter – app communication is a safe way.
Our app is not directly talking to the other app.
5. SYSTEM SERVER
Heart of Android and it’s responsible for everything Android does.
The Windows Manager is responsible for windowing and being in-charge all things in window.
It keeps tracks of window activities and it keeps track of what is visible.
It is also responsible for transitions between activities for an overall good UI experience.
The Package manager is responsible for installing new applications.
1. Linux Kernel
This layer is the foundation of the Android platform.
Contains all low level drivers for various hardware components support.
Android Runtime relies on Linux kernel for core system services like,
- Memory, process management, threading etc.
- Network stack
- Driver model
- Security and more.
3. Android Runtime
Each app runs in its own process and with its own instance of the Android Runtime (ART)
virtual machine.
This makes process management more crucial.
ART uses DEX files which is a type of bytecode, specially designed for Android, which helps
ART to manage memory more efficiency.
Contains set of core libraries that enables developers to write Android Apps using Java
Programming.
Prior to version 5.0 – Dalvik was used as the run time along with JIT compiler.
Just In Time (JIT)
o With the Dalvik JIT compiler, each time when the app is run, it dynamically translates a
part of the Dalvik bytecode into machine code.
o As the execution progresses, more bytecode is compiled and cached.
o Since JIT compiles only a part of the code, it has a smaller memory footprint and less
physical space on the devices.
Ahead of Time (AOT)
o ART is equipped with an Ahead-of-Time compiler.
o During the app’s installation phase, it statically translates the DEX bytecode into
machine code and stores in the device’s storage.
o This is a one-time event which happens when the app is installed on the device.
o With no need for JIT compilation, the code executes much faster.
4. Libraries
Many core Android System components and services such as ART and HAL are built from native
code that require native libraries written in C and C++.
The Android platform provides Java framework APIs to expose the functionality of some of
these native libraries to apps.
If you are developing an app that requires C or C++ code, you can use the Android NDK to access
some of these native platform libraries directly from your native code.
5. System Apps
Android comes with a set of core apps for email, SMS messaging, calendars, internet browsing,
contacts, and more.
Apps includes with the platform have no special status among the apps users chooses to install.
So a third-party app can become the user’s default web browser, SMS messenger, or even the
default keyboard (some exceptions apply, such as the system’s setting app).
Dalvik Execution
o Programs are commonly written in Java and compiled to bytecode.
o It is then translated to Dalvik bytecode.
o .dex (Dalvik Executable) and .odex (optimized Dalvik Executable)
o The compact Dalvik Executable format is designed for systems that are constrained in terms of
memory and processor speed.
Java compiler
Packaging (aapt)
(javac)
o A tool called dx is used to convert Java .class files into the .dex format.
o Multiple classes are included in a single .dex file.
o Duplicate strings and other constants used in multiple class files are included only once in the
.dex output to conserve space.
o An uncmpressed .dex file is typically a few percent smaller in size than a compressed Java
archive (JAR) derived from the same .class files.
Method Description
onResume called when activity will start interacting with the user.
Button
6. Type java coding in MainActivity.java.
7. Select your Android Virtual Device AVD.
8. To run the ‘app’ press play button in android studio or shift + F10.
PROGRAM
activity_main.xml
ATTRIBUTES VALUES
LINEARLAYOUT
Layout_width Match parent
Layout_height Match parent
id lay
BUTTON
Layout_width Wrap content
Layout_height Wrap content
Id b1
Text DISPLAY YOUR NAME
MainActivity.java
package com.example.toastmessage;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(MainActivity.this, "Mohamed Zamam Nazar",
Toast.LENGTH_LONG).show();
}
});
}
}
2.2 EDIT TEXT (PLAIN TEXT)
PROCEDURE
1. Start android studio application.
2. Select File in menu bar.
3. Choose your project as Empty Activity and click Next button.
4. Name your project (The application name begins with an uppercase letter), select language as
java, select your minimum API level as (Android 4.0 IceCreamSandwich) then click Finish
button.
5. For designing, type xml coding in activity_main.xml.
Components used for designing
BUTTON
6. Type java coding in MainActivity.java.
7. Select your Android Virtual Device AVD.
8. To run the ‘app’ press play button in android studio or shift + F10.
PROGRAM
activity_main.xml
ATTRIBUTES VALUES
LINEARLAYOUT
Layout_width Match parent
Layout_height Match parent
id lay
BUTTON
Layout_width Wrap content
Layout_height Wrap content
Id b1 and b2
Text ADD and SUB
EDIT TEXT (PLAIN TEXT)
Layout_width Wrap content
Layout_height Wrap content
Id A and B
Text (Leave it Blank)
MainActivity.java
package com.example.add;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1,b2;
EditText A,B;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A=findViewById(R.id.A);
B=findViewById(R.id.B);
b1=findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Integer i=Integer.parseInt(A.getText().toString());
Integer j=Integer.parseInt(B.getText().toString());
Integer K= i+j;
Toast.makeText(MainActivity.this, "Add : "+K, Toast.LENGTH_LONG).show();
}
});
b2=findViewById(R.id.b2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Integer i=Integer.parseInt(A.getText().toString());
Integer j=Integer.parseInt(B.getText().toString());
Integer K= i-j;
Toast.makeText(MainActivity.this, "Sub : "+K, Toast.LENGTH_LONG).show();
}
});
}
}
2.3 TEXTVIEW
PROCEDURE
1. Start android studio application.
2. Select File in menu bar.
3. Choose your project as Empty Activity and click Next button.
4. Name your project (The application name begins with an uppercase letter), select language as
java, select your minimum API level as (Android 4.0 IceCreamSandwich) then click Finish
button.
5. For designing, type xml coding in activity_main.xml.
Components used for designing
TEXTVIEW
6. Type java coding in MainActivity.java.
7. Select your Android Virtual Device AVD.
8. To run the ‘app’ press play button in android studio or shift + F10.
PROGRAM
activity_main.xml
ATTRIBUTES VALUES
LINEARLAYOUT
Layout_width Match parent
Layout_height Match parent
id lay
TEXTVIEW
Layout_width Wrap content
Layout_height Wrap content
Id tv1
Text Hello World!
Text Style Bold
Text Size 24dp
2.4 CHECKBOX
PROCEDURE
1. Start android studio application.
2. Select File in menu bar.
3. Choose your project as Empty Activity and click Next button.
4. Name your project (The application name begins with an uppercase letter), select language as
java, select your minimum API level as (Android 4.0 IceCreamSandwich) then click Finish
button.
5. For designing, type xml coding in activity_main.xml.
Components used for designing
CHECKBOX
6. Type java coding in MainActivity.java.
7. Select your Android Virtual Device AVD.
8. To run the ‘app’ press play button in android studio or shift + F10.
PROGRAM
MainActivity.java
package com.example.mycheckbox1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
CheckBox cb1,cb2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1=findViewById(R.id.cb1);
cb2=findViewById(R.id.cb2);
cb1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(cb1.isChecked())
Toast.makeText(getApplicationContext(), "MCA SELECTED", Toast.LENGTH_SHORT).show();
}
});
checkBox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (cb2.isChecked())
Toast.makeText(getApplicationContext(), "MSC SELECTED", Toast.LENGTH_SHORT).show();
}
}
});
}
}
2.4 RADIOBUTTON
PROCEDURE
1. Start android studio application.
2. Select File in menu bar.
3. Choose your project as Empty Activity and click Next button.
4. Name your project (The application name begins with an uppercase letter), select language as
java, select your minimum API level as (Android 4.0 IceCreamSandwich) then click Finish
button.
5. For designing, type xml coding in activity_main.xml.
Components used for designing
RADIOGROUP
RADIOBUTTON
6. Type java coding in MainActivity.java.
7. Select your Android Virtual Device AVD.
8. To run the ‘app’ press play button in android studio or shift + F10.
PROGRAM
MainActivity.java
package com.example.radiobutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
RadioButton rb1,rb2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1=findViewById(R.id.rb1);
rb2=findViewById(R.id.rb2);
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rb1.isChecked())
Toast.makeText(MainActivity.this, "MALE SELECTED", Toast.LENGTH_LONG).show();
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rb2.isChecked())
Toast.makeText(MainActivity.this, "FEMALE SELECTED", Toast.LENGTH_LONG).show();
}
});
}