Download Introduction to Android Technology

Document related concepts
no text concepts found
Transcript
Introduction to Android
Technology
Er. Girish Kumar Shakya
Android Developer
Agenda
What is an Android ?
History of Android
System Architecture
The Open Handset Alliance (OHA)
Anatomy of an Android application
Android Activity Life Cycle
Er. Girish Kumar Shakya
Android Developer
What is an Android?
• Android is an open software platform for
Mobile Development.
www.android.com
Operating Systems
Middleware
Applications
developer.android.com
Er. Girish Kumar Shakya
Android Developer
History of Android ?
Android 1.5 (Cupcake)
– support for soft (on‐screen) keyboards
– video recording
– application widgets
– more than 1,000 changes to API
Er. Girish Kumar Shakya
Android Developer
History of Android ?(cont..)
Android 1.6 (Donut)
– High and Low Density Display
– a Number of minor changes
Er. Girish Kumar Shakya
Android Developer
History of Android ?(cont..)
Android 2.0 (Eclair)
– multi-touch
– Virtual keys
– centralized account management
– synchronization APIs
– Docking
– HTML5 and more
Er. Girish Kumar Shakya
Android Developer
History of Android ?(cont..)
Android 2.1 (Eclair Maintenance Release 1)
– more HTML5 support
– other minor improvement
– Home screen enhancements
– live wallpaper and widgets
Er. Girish Kumar Shakya
Android Developer
History of Android ?(cont..)
Next Generation (FroYo)
Then - Gengerberd
Er. Girish Kumar Shakya
Android Developer
System Architecture
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
• Hardware Abstraction Layer
 Security
 Memory Management
 Process Management
 Network Stack
 Driver Model
Er. Girish Kumar Shakya
Android Developer
– C or C++
– Compiled for particular architecture
Surface Manager
– No screen buffer ‐‐‐‐ off screen bitmaps (combined with other bitmaps)
• 2D and 3D graphics
• Media Codecs
– AAC, AVC, H.263, MP3, MPEG‐4
• SQL Database (Firefox/iPhone)
Er. Girish Kumar Shakya
• Browser Engine (Crome, Safari, iPhone, Nokia S60) Android Developer
- Dalvik Virtual Machine
– Google’s implementation of Java
– Optimized for Mobile devices
• Runs .dex files
– More compact and efficient than .class files
• Memory and power concerns
• Core Java libraries are different from JavaSE and
JavaME
Er. Girish Kumar Shakya
Android Developer
Building block for creating Applications
Pre – Installed in Android
ActivityManager
- Control Activity Life Cycle
ContentProvider
– Encapsulates any data to share between applications (contacts)
ResourceManager
- Parts of program that are not code (sound bites, video, images)
LocationManager
- Android always know where it is........
NotificationManager
- Events, messages, appointments, any alerts.........
Er. Girish Kumar Shakya
Android Developer
 Apps
- Programs which take over the entire
screen
 Widgets (Gadgets)
– Operate in a small rectangle of Home
screen
Er. Girish Kumar Shakya
Android Developer
Open Handset Alliance
• The Open Handset Alliance (OHA) is a collection of more than
30 technology companies including hardware manufacturers,
mobile carriers, and software developers. Of particular note
are the prominent mobile technology companies Motorola,
HTC, T-Mobile, and Qualcomm. In their own words, the OHA
represents:
• “A commitment to openness, a shared vision for the future,
and concrete plans to make the vision a reality. To accelerate
innovation in mobile and offer consumers a richer, less
expensive, and better mobile experience.”
Er. Girish Kumar Shakya
Android Developer
Open Handset Alliance (cont..)
Some of the alliance’s prominent members are as follow:
 Sprint Nextel
 T-Mobile
 Motorola
 Samsung
 Sony Ericsson
 Toshiba
 Vodafone * Google * Intel * Texas Instruments
Er. Girish Kumar Shakya
Android Developer
Anatomy of an Android
application
Let‟s create a simple Android
TutorialBasic Application.
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
How to make an Android Application ?
Er. Girish Kumar Shakya
Android Developer
SCREEN
Er. Girish Kumar Shakya
Android Developer
Intent
Er. Girish Kumar Shakya
Android Developer
 Model
 View
 i.e.
 Model 1+ View 1=
Activity 1
Er. Girish Kumar Shakya
Android Developer
Model
Java (Logic)
Er. Girish Kumar Shakya
Android Developer
View
XML (Layout)
Er. Girish Kumar Shakya
Android Developer
Controller
AndroidManifest.x
ml
Er. Girish Kumar Shakya
Android Developer
Model
Java (Logic)
Er. Girish Kumar Shakya
Android Developer
View
XML (Layout)
Er. Girish Kumar Shakya
Android Developer
Controller
AndroidManifest.x
ml
Er. Girish Kumar Shakya
Android Developer
CONTROLLER
(AndroidManifest.
xml)
Intent
ACTIVITY
1
ACTIVITY
2
Er. Girish Kumar Shakya
Android Developer
ACTIVITY 1
Intent 2
ACTIVITY 2
Intent 3
CONTROLLER
(AndroidManif
est.xml)
ACTIVITY 3
Er. Girish Kumar Shakya
Android Developer
1 Model + 1 View = 1 Activity
ACTIVITY (n)
(1)Controller
(AndroidManifest.xml)
Er. Girish Kumar Shakya
Android Developer
TutorialBasic Application
 Open Eclipse IDE
 Close the Welcome Window if Open
 Go to File -> New -> Other -> Android ->
Android Project.
 Give Project Name : TutorialBasic
 Select Target Name as : Android2.2
 Click Next
 Give Package Name: my.hello.world
 Then click Finish.
Er. Girish Kumar Shakya
Android Developer
Application Name (Project Node)
Source Folder…. Here we put all
Java Code
Java Code also Known as Model or
Activity Source Code in Java Format
Auto Generated Code by Eclipse
Target SDK or Platform here
Android2.2
APK or also known as single Out put
File…
Resource Folder.. Collections of all
Your resources.
Layout Folder … Your VIEWs in XML
Format
Controller… Er. Girish Kumar Shakya
Android Developer
Res Folder
<xml>
 Resources: Text ,
pictures, sound etc.
Everything is broken
out of the code into a
resource, which is
reference in a class
Girish Kumar Shakya
called REr.
. Android
Developer
Src Folder
Collection of
your packages,
Activity Class
, Common Java
Classes…
Er. Girish Kumar Shakya
Android Developer
What is really an “Activity”?
Displays a user interface component and
responds to system/user initiated.
When an application has a user interface, it
contains one or more “Activities” One activity is
then considered as the main entry point by the
system
An existing “Activity” can be replaced with a new
one that fulfill the same contract
Each “Activity” can be invoked from others
applications
Er. Girish Kumar Shakya
Android Developer
What is really an “Activity”?
Adding a new “Activity” in an Android project
The new Java class must extend the
framework “Activity” class
Created “Activity” must be defined into the
application‟s Manifest
Er. Girish Kumar Shakya
Android Developer
Anatomy of App
(Activity lifecycle)
 Running in the
multitasking
environment
 Users switch
apps,calls come
in,system run low
on memory
 System invoke
callbacks in your
application
 The System will kill
your application
 Be sure to save
state
Er. Girish Kumar Shakya
Android Developer
Anatomy of App
(Activity lifecycle)
Activity
Created
onCreate()
onResume()
Activity
Running
Er. Girish Kumar Shakya
Android Developer
Anatomy of App
(Activity lifecycle)
onResume()
Activity
Running
Call Comes
in
onPause()
Er. Girish Kumar Shakya
Android Developer
Anatomy of App
(Activity lifecycle)
onResume()
Activity
Running
Activity
Destroy
Low
Memory
Call Comes
in
Return to
App
onPause()
Er. Girish Kumar Shakya
Android Developer
Intents / Intent Filters
Simple message objects that represent:
• An intention to do something
• A declaration of capacity and interest in
ofering assistance to those in need
Er. Girish Kumar Shakya
Android Developer
Intents / Intent Filters
(cont…)
An “intent” is made up a number off pieces
off information describing the action or the
service:
“action” attribute is typically a verb (VIEW,
EDIT, DIAL,…)
The data to operate on is expressed in the
form off an Universal Resource Identifier
(URI)
“category” attribute gives additional
Er. Girish
Kumar Shakya
information about the action to
execute
Android Developer
Receivers
When an application desires to receive and
respond to a global event..
• In order to be triggered when an event occurs,,
application does not have to be running
• By default,, Android includes some built-in “Intents
Receiver”
Er. Girish Kumar Shakya
Android Developer
Receivers
How to receive these specific “Intents”?
• The user class must extend the framework
“IntentReceiver” class
• To process incoming “Intents”,, “onReceiveIntent()”
method is implemented
• “receiver” element must be described into the
application's Manifest
Er. Girish Kumar Shakya
Android Developer
Service
(A long running background task)
Defining and invoking a “Service”
Adding a “Service” with Android is quite similar
tthan for an “Activity”..
Framework “Service” class must be extended
The new “Service” must be defined into the
application's Manifest
Er. Girish Kumar Shakya
Android Developer
Service(cont..)
(A long running background task)
When a “Service” is using IPC, an AIDL
description of its features is also needed..
• Android Interface Definition Language (AIDL) is used to generated
code to allow communication between two processes through IIPC
• This mechanism is interface-based,, similar to Cobra (Unix) or
COM (windows) but lighter weight
• With this specific mode, the “Service” is started through the
binder and not with “startService()”
Er. Girish Kumar Shakya
Android Developer
ContentProvider
(Handle data and expose them to
other applications)
The only way to share data between Android
applications..
• It implements a standard set of methods to
allow access to a data store
• Any form off data storage can be used like
SQLite database,, files,, or memory hash map
Er. Girish Kumar Shakya
Android Developer
ContentProvider(cont..)
(Handle data and expose them to
other applications)
A “Content Providers” exposes a unique URI
used to query,add,update and delete data::
• A standard prefix (“content:://”)
• The authority part (fully-qualified to ensure
uniqueness)
• The path to determine what kind off data is being
requested
• A specific record being requested,, if any
Er. Girish Kumar Shakya
Android Developer
Processes and Task
(A Task between Kernel and
Android)
Processes
A low-level kernel process in which an application's code is
running..
•
By default,, Android binds the content off an APK to a Linux process
•
“Process” tag can be used to tune this relation with a lower granularity
(activity, service,…)
Tasks
A notion that users know on other platform as “application”..
A collection of related “Activities”
Capable of spanning multiple processes
Interaction with “Activities” can be controlled through “Activity's
launchMode” attribute
Er. Girish Kumar Shakya
Android Developer
Processes and Task(cont.)
(A Task between Kernel and
Android)
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer










Using XML based layouts
Using Basic Widgets
Working with Containers
Using Selection Widgets
Using Fancy Widgets and Containers
Applying Menus
Embedding the WebKit browser
Showing Pop-up Messages
Dealing with Threads
Handling Activity Lifecycle events
Er. Girish Kumar Shakya
Android Developer








Text Controls
o TextView
o EditText (EditText control is subclass of TextView)
o AutoCompleteTextView
o MultiAutoCompleteTextView
Button Controls
o Button
o ImageButton
o ToggleButton
CheckBox
RadioButton
Spinner
ProgressBar
RatingBar
SeekBar
Er. Girish Kumar Shakya
Android Developer

Text Controls
Er. Girish Kumar Shakya
Android Developer

Text Controls
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:id="@+id/textView1"
android:textSize="25dp">
</TextView>
Er. Girish Kumar Shakya
Android Developer

Text Controls
Declare TextView
TextView tv;
Er. Girish Kumar Shakya
Android Developer

Text Controls
Initialize or Bind it in your Activity TextView
tv=(TextView)findViewById(R.id.textView1);
Er. Girish Kumar Shakya
Android Developer

Text Controls
Initialize or Bind it in your Activity TextView
tv=(TextView)findViewById(R.id.textView1);
tv.setText(“I Love my India”);
Er. Girish Kumar Shakya
Android Developer

Text Controls
Initialize or Bind it in your Activity TextView
tv=(TextView)findViewById(R.id.textView1);
tv.setText(“I Love my India”);
Er. Girish Kumar Shakya
Android Developer

Text Controls
Er. Girish Kumar Shakya
Android Developer

Text Controls
Er. Girish Kumar Shakya
Android Developer

<EditText android:text="EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
>
</EditText>
Text Controls
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:text="AutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</AutoCompleteTextView>
<Button android:id="@+id/button1"
android:text="Click"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Button>
<MultiAutoCompleteTextView
android:text="MultiAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/multiAutoCompleteTextView1"
>
Er. Girish Kumar Shakya
</MultiAutoCompleteTextView>
Android Developer

Text Controls
Declare your Views:
EditText simpleEdit;
AutoCompleteTextView autoEdit;
MultiAutoCompleteTextView multiEdit;
Button mybtn;
Er. Girish Kumar Shakya
Android Developer

Text Controls
Initialize your view
simpleEdit=(EditText)findViewById(R.id.editText1);
autoEdit=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
multiEdit=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextV
iew1);
mybtn=(Button)findViewById(R.id.button1);
Er. Girish Kumar Shakya
Android Developer

Text Controls
Add listener and perform some Event say
OnClickListener() and OnClick()
mybtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Put some code here
}
});
Er. Girish Kumar Shakya
Android Developer

Text Controls
Add listener and perform some Event say
OnClickListener() and OnClick()
mybtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
simpleEdit.setText("Hello");
autoEdit.setText("Hi");
multiEdit.setText("Bye");}
});
Er. Girish Kumar Shakya
Android Developer

Text Controls
Er. Girish Kumar Shakya
Android Developer

Button Controls
Er. Girish Kumar Shakya
Android Developer

Button Controls
<Button
android:id="@+id/button1"
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Button>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon">
</ImageButton>
<ToggleButton
android:text="Run"
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Run"
Er. Girish Kumar Shakya
android:textOff="Stop"
Android Developer
></ToggleButton>

Button Controls
Declare your Views
Button simpleBtn;
ImageButton imgBtn;
ToggleButton toggleBtn;
Er. Girish Kumar Shakya
Android Developer

Button Controls
Initialize or Bind your Views
simpleBtn=(Button)findViewById(R.id.button1);
imgBtn=(ImageButton)findViewById(R.id.imageButton1);
toggleBtn=(ToggleButton)findViewById(R.id.toggleButton1);
Er. Girish Kumar Shakya
Android Developer

Button Controls
Perform Listener and Event
simpleBtn=(Button)findViewById(R.id.button1);
imgBtn=(ImageButton)findViewById(R.id.imageButton1);
toggleBtn=(ToggleButton)findViewById(R.id.toggleButton1);
Er. Girish Kumar Shakya
Android Developer

Button Controls
Listener and Event simpleBtn
simpleBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//do something useful
}
});
Er. Girish Kumar Shakya
Android Developer

Button Controls
Listener and Event simpleBtn
simpleBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "I Clicked On Button",
Toast.LENGTH_SHORT).show();
}
});
Er. Girish Kumar Shakya
Android Developer

Button Controls
Listener and Event imageBtn
imgBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//put some code here
}
});
Er. Girish Kumar Shakya
Android Developer

Button Controls
Listener and Event imageBtn
imgBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "I Clicked On Image Button",
Toast.LENGTH_SHORT).show();
}
});
Er. Girish Kumar Shakya
Android Developer

Button Controls
Listener and Event toggleBtn
toggleBtn.setOnCheckedChangeListener(new
OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
//put some code here
}
});
Er. Girish Kumar Shakya
Android Developer

Button Controls
Listener and Event toggleBtn
toggleBtn.setOnCheckedChangeListener(new
OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
//put some code here
}
});
Er. Girish Kumar Shakya
Android Developer

Button Controls
Listener and Event toggleBtn
toggleBtn.setOnCheckedChangeListener(new
OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "Run Charlie Run !!",
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Stop Charlie !!",
Toast.LENGTH_SHORT).show();
}
}
});
Er. Girish Kumar Shakya
Android Developer

Button Controls
Er. Girish Kumar Shakya
Android Developer

Button Controls
Er. Girish Kumar Shakya
Android Developer

Button Controls
Er. Girish Kumar Shakya
Android Developer

CheckBox
Er. Girish Kumar Shakya
Android Developer

CheckBox
<CheckBox android:text="Fish"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<CheckBox android:text="Chicken"
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<CheckBox android:text="Dosa"
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
Er. Girish Kumar Shakya
Android Developer

CheckBox
Declare your checkboxes
CheckBox chk1;
CheckBox chk2;
CheckBox chk3;
Er. Girish Kumar Shakya
Android Developer

CheckBox
Initialize your checkboxes
chk1=(CheckBox)findViewById(R.id.checkBox1);
chk2=(CheckBox)findViewById(R.id.checkBox2);
chk3=(CheckBox)findViewById(R.id.checkBox3);
Er. Girish Kumar Shakya
Android Developer

CheckBox
Set your listener (OnCheckedListener)
chk1.setOnCheckedChangeListener(this);
chk2.setOnCheckedChangeListener(this);
chk3.setOnCheckedChangeListener(this);
Er. Girish Kumar Shakya
Android Developer
 CheckBox
Set your listener (OnCheckedListener)
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int id=buttonView.getId();
switch(id){
case R.id.checkBox1:
if(isChecked)
Toast.makeText(this, "Fish", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox2:
if(isChecked)
Toast.makeText(this, "Chicken", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox3:
if(isChecked)
Toast.makeText(this, "Dosa", Toast.LENGTH_SHORT).show();
break;
Er. Girish Kumar Shakya
}
}
Android Developer

CheckBox
Er. Girish Kumar Shakya
Android Developer

RadioButton
Er. Girish Kumar Shakya
Android Developer
<RadioGroup android:id="@+id/radioGroup1"

RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_height="wrap_content"
android:text="Fish"
android:id="@+id/radio0"
android:layout_width="wrap_content"
></RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:text="Chicken"
android:id="@+id/radio1"
android:layout_width="wrap_content">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:text="Dosa"
android:id="@+id/radio2"
android:layout_width="wrap_content"></R
adioButton>
</RadioGroup>
Er. Girish Kumar Shakya
Android Developer

RadioButton
Declare RadioGroup
RadioGroup rg;
Er. Girish Kumar Shakya
Android Developer

RadioButton
Initialize RadioGroup
rg=(RadioGroup)findViewById(R.id.radioGroup1);
Er. Girish Kumar Shakya
Android Developer

RadioButton
Perform Listener and event
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//put some code here
}
});
Er. Girish Kumar Shakya
Android Developer
Perform Listener and event
 RadioButton
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.radio0:
Toast.makeText(MainActivity.this, "Fish", Toast.LENGTH_SHORT).show();
break;
case R.id.radio1:
Toast.makeText(MainActivity.this, "Chicken",
Toast.LENGTH_SHORT).show();
break;
case R.id.radio2:
Toast.makeText(MainActivity.this, "Dosa", Toast.LENGTH_SHORT).show();
break;
}}
});
Er. Girish Kumar Shakya
Android Developer

RadioButton
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView,
MultiAutoCompleteTextView
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
<Spinner android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
<AutoCompleteTextView
android:layout_width="match_parent"
android:text="AutoCompleteTextView"
android:layout_height="wrap_content"
android:id="@+id/autoCompleteTextView1">
</AutoCompleteTextView>
<MultiAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/multiAutoCompleteTextView1"
android:text="MultiAutoCompleteTextView">
</MultiAutoCompleteTextView>
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Declare your View
Spinner spinner;
AutoCompleteTextView autoEdit;
MultiAutoCompleteTextView multiEdit;
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Declare your View
Spinner spinner;
AutoCompleteTextView autoEdit;
MultiAutoCompleteTextView multiEdit;
String[] list={"Sunday","Monday","Tuesday"
,"Wednesday","Thrusday","Friday","Saturday"};
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Initialize your View
spinner=(Spinner)findViewById(R.id.spinner1);
autoEdit=(AutoCompleteTextView)findViewById(R.id.autoCompleteText
View1);
multiEdit=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCo
mpleteTextView1);
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Create an Object of
ArrayAdapter<String>
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, list);
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Set Adapter to Spinner
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, list);
spinner.setAdapter(adapter);
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Set Adapter to AutoCompleteTextView
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, list);
spinner.setAdapter(adapter);
autoEdit.setAdapter(adapter);
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Set Adapter to AutoCompleteTextView
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, list);
spinner.setAdapter(adapter);
autoEdit.setAdapter(adapter);
multiEdit.setAdapter(adapter);
multiEdit.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
Er. Girish Kumar Shakya
Android Developer

Spinner , AutoCompleteTextView, MultiAutoCompleteTextView
Er. Girish Kumar Shakya
Android Developer

ProgressBar
Er. Girish Kumar Shakya
Android Developer

RatingBar
Er. Girish Kumar Shakya
Android Developer
RatingBar

<ImageView
android:layout_width="wrap_content“
android:src="@drawable/image"
android:id="@+id/imageView1"
android:layout_height="wrap_content"></I
mageView>
<TextView android:text="Movie : Rock Star"
android:id="@+id/textViewrock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Text
View>
<TextView android:text="rating:“
android:id="@+id/textViewRateValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<RatingBar
></TextView>
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></R
atingBar>
Er. Girish Kumar Shakya
Android Developer
RatingBar

<ImageView
android:layout_width="wrap_content“
android:src="@drawable/image"
android:id="@+id/imageView1"
android:layout_height="wrap_content"></I
mageView>
<TextView android:text="Movie : Rock Star"
android:id="@+id/textViewrock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Text
View>
<TextView android:text="rating:“
android:id="@+id/textViewRateValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<RatingBar
></TextView>
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></R
atingBar>
Er. Girish Kumar Shakya
Android Developer

RatingBar
Declare your View
TextView rateText;
RatingBar rateBar;
Er. Girish Kumar Shakya
Android Developer

RatingBar
Initialize or Bind your View
rateText=(TextView)findViewById(R.id.textViewRateValue);
rateBar=(RatingBar)findViewById(R.id.ratingBar1);
Er. Girish Kumar Shakya
Android Developer

RatingBar
Perform Listener
setOnRatingBarChangeListener()
rateBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
rateText.setText("rating is:"+rating);
}
});
Er. Girish Kumar Shakya
Android Developer

RatingBar
Er. Girish Kumar Shakya
Android Developer

SeekBar
Er. Girish Kumar Shakya
Android Developer

SeekBar
<SeekBar android:id="@+id/seekBar1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</SeekBar>
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
Er. Girish Kumar Shakya
Android Developer

SeekBar
Declare your views
TextView pertxt;
SeekBar seek;
Er. Girish Kumar Shakya
Android Developer

SeekBar
Initialize or bind your views
pertxt=(TextView)findViewById(R.id.textView1);
seek=(SeekBar)findViewById(R.id.seekBar1);
Er. Girish Kumar Shakya
Android Developer

Perform Listener on SeekBar
(setOnSeekBarChangeListener)
SeekBar
seek.setOnSeekBarChangeListener(new
OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
//put some here
}
});
Er. Girish Kumar Shakya
Android Developer

Perform Listener on SeekBar
(setOnSeekBarChangeListener)
SeekBar
seek.setOnSeekBarChangeListener(new
OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
pertxt.setText("your percentage is: "+progress);
}
});
Er. Girish Kumar Shakya
Android Developer

SeekBar
Er. Girish Kumar Shakya
Android Developer
LinearLayout
TableLayout


RelativeLayout

FrameLayout

Organizes its children either
horizontally or vertically
Organizes its children in
tabular form
Organizes its children
relative to one another or to
the parent
Allows you to dynamically
change the control(s) in the
layout
Er. Girish Kumar Shakya
Android Developer
LinearLayout
Er. Girish Kumar Shakya
Android Developer
TableLayout
Er. Girish Kumar Shakya
Android Developer
Relative Layout
Er. Girish Kumar Shakya
Android Developer
FrameLayout
Er. Girish Kumar Shakya
Android Developer

o
o
Pick and Choose
DatePicker
TimePicker
Er. Girish Kumar Shakya
Android Developer
<TextView android:layout_width="wrap_content"
android:text="Date and Time"
android:layout_height="wrap_content"
android:textSize="25dp"
android:id="@+id/textViewDateandTime">
</TextView>
<TimePicker android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TimePic
ker>
<DatePicker android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</DatePicker>
<Button
<Button
android:layout_width="match_parent"
android:layout_width="match_parent"
android:id="@+id/buttondate"
android:id="@+id/buttontime"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:text="Click to pick Date"></Button>
android:text="Click to pick Time"></Button>
Er. Girish Kumar Shakya
Android Developer
Declare your views
TextView tv;
DatePicker dp;
TimePicker tp;
Button clickDp;
Button clickTp;
Er. Girish Kumar Shakya
Android Developer
Initialize or bind your views
tv=(TextView)findViewById(R.id.textViewDateandTime);
dp=(DatePicker)findViewById(R.id.datePicker1);
tp=(TimePicker)findViewById(R.id.timePicker1);
clickDp=(Button)findViewById(R.id.buttondate);
clickTp=(Button)findViewById(R.id.buttontime);
Er. Girish Kumar Shakya
Android Developer
Perform Listeners and Events
clickDp.setOnClickListener(this);
clickTp.setOnClickListener(this);
Er. Girish Kumar Shakya
Android Developer
Perform Listeners and Events
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.buttondate:
String []month={"Jan","Feb","March","April","May"
,"June","July","Aug","Sep","Oct","Nov","Dec"};
String
strdate=dp.getDayOfMonth()+"/"+month[dp.getMonth()]+"/"+
dp.getYear();
tv.setText(strdate);
break;
case R.id.buttontime:
String
timestr=""+tp.getCurrentHour()+":"+tp.getCurrentMinute();
tv.setText(timestr);
break;
}
Er. Girish Kumar Shakya
}
Android Developer
Er. Girish Kumar Shakya
Android Developer


AnalogClock
DigitalClock
<DigitalClock
android:text="DigitalClock"
android:id="@+id/digitalCloc
k1"
android:layout_width="wrap_
content"
android:layout_height="wrap_
content"></DigitalClock>
<AnalogClock
android:id="@+id/analogCloc
k1"
android:layout_width="wrap_
content"
android:layout_height="wrap_
content"></AnalogClock>
Er. Girish Kumar Shakya
Android Developer

Creating a simple Menu
<?xml version="1.0"
encoding="utf-8"?>
<menu
xmlns:android="http://schemas.a
ndroid.com/apk/res/android">
<item
android:id="@+id/andhra"
android:alphabeticShortcut="a"
android:title="Andhra Pradesh"
></item>
<item android:id="@+id/mp"
android:alphabeticShortcut="m"
android:title="Madhya Pradesh"
></item>
</menu> Er. Girish Kumar Shakya
Android Developer
 Creating a simple Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater menuInflator=getMenuInflater();
menuInflator.inflate(R.menu.mymenu, menu);
return true;}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
//add some code
return true;
}
Er. Girish Kumar Shakya
Android Developer

Creating a simple Menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.mp:
Toast.makeText(MainActivity.this, "Madhya Pradesh",
Toast.LENGTH_SHORT).show();
break;
case R.id.andhra:
Toast.makeText(MainActivity.this, "Andhra Pradesh",
Toast.LENGTH_SHORT).show();
}
break;return true;
Er. Girish Kumar Shakya
Android Developer

Creating a simple Menu
Er. Girish Kumar Shakya
Android Developer
WebView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/an

droid"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
Er. Girish Kumar Shakya
Android Developer


Toast
Alert
Toast
A Toast is a transient message,
meaning that it displays and
disappears on
its own without user interaction.
Alert
A subclass of Dialog that can
display one, two or three
buttons.
Er. Girish Kumar Shakya
Android Developer

Toast
Toast
.makeText(this, "<clink,
clink>",
Toast.LENGTH_SHORT)
.show();
Er. Girish Kumar Shakya
Android Developer

AlertDialog
AlertDialog.Builder alert=new
AlertDialog.Builder(this);
alert.setTitle(“MessageDemo”);
alert.setMessage(“Hello Friends”);
alert.show();
Er. Girish Kumar Shakya
Android Developer

Example:
toastbtn.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
stub
Toast.makeText(PopUpDemo.this,
"Hello from toast",
Toast.LENGTH_SHORT).show();
}
});
alertbtn.setOnClickListener(new
OnClickListener() {
stub
@Override
public void onClick(View v) {
// TODO Auto-generated method
AlertDialog.Builder alert=new
AlertDialog.Builder(PopUpDemo.this);
alert.setTitle("MessageDemo");
alert.setMessage("welcome to
India");
alert.show();
}
});
Er. Girish Kumar Shakya
Android Developer

Example:
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button android:layout_width="match_parent"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Tutorial 1"></Button>
<Button android:layout_width="match_parent"
android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="Tutorial 2"></Button>
…………………..
…………………..
<Button android:layout_width="match_parent"
android:id="@+id/button14"
android:layout_height="wrap_content"
android:text="Tutorial 14"></Button>
</LinearLayout>
</ScrollView>
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer



Intents are used as a message-passing
mechanism
Explicitly (by specifying the class to load)
Implicitly (by specifying inside
AndroidManifest.xml)
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
<EditText
android:layout_width="match_parent"
android:id="@+id/editText1"
android:layout_height="wrap_content"></EditText>
<Button android:layout_width="match_parent"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Move to Second Activity"></Button>
Er. Girish Kumar Shakya
Android Developer
Declare your View
EditText edit;
Button btnClk;
Er. Girish Kumar Shakya
Android Developer
Initialize your View
edit=(EditText)findViewById(R.id.editText1);
btnClk=(Button)findViewById(R.id.button1);
Er. Girish Kumar Shakya
Android Developer
Prepare Listener and Event
btnClk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(FirstActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
Er. Girish Kumar Shakya
Android Developer
<TextView android:text="Whats your Name?"
android:id="@+id/textViewsecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextV
iew>
Er. Girish Kumar Shakya
Android Developer
@Override
protected void onCreate(Bundle
savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
Er. Girish Kumar Shakya
Android Developer
AndroidManifest.xml
<activity android:name=".SecondActivity"
android:label="@string/app_name">
</activity>
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer




putExtra(String
putExtra(String
putExtra(String
putExtra(String
name,
name,
name,
name,
boolean value);
int value);
double value);
String value);
Passing Message between Activity
Intent intent = new Intent();
Intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra(key,value);
startActivity(intent);
Er. Girish Kumar Shakya
Android Developer






Bundle bundle=getIntent().getExtras();
String str=bundle.getString(“Key”);
boolean b=bundle.getBoolean(“Key”);
int b=bundle.getInt(“Key”);
float b=bundle.getFloat(“Key”);
double b=bundle.getDouble(“Key”);
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:555-555-5555"));
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
Er. Girish Kumar Shakya
Android Developer
<EditText
android:layout_width="match_parent"
android:hint="http://"
android:layout_height="wrap_content"
android:id="@+id/editTextbrowser"></EditText>
<Button
android:layout_width="wrap_content"
android:id="@+id/buttonSearch"
android:layout_height="wrap_content"
android:background="@drawable/search"
android:layout_x="82dip"
android:layout_y="-3dip"></Button>
<Button
android:layout_width="wrap_content"
android:id="@+id/buttoncall"
android:layout_height="wrap_content"
android:background="@drawable/call"
android:layout_x="249dip"
android:layout_y="-1dip">
</Button>
<Button
android:layout_width="wrap_content"
android:id="@+id/buttoncallme"
android:layout_height="wrap_content"
android:background="@drawable/callme"
android:layout_x="167dip"
android:layout_y="-1dip"></Button>
<Button
android:layout_width="wrap_content"
android:id="@+id/buttonGo"
android:layout_height="wrap_content"
android:background="@drawable/go"
android:layout_x=
"17dip"
Er. Girish
Kumar Shakya
android:layout_y="2dip"></Button>
Android Developer
Declare Views
EditText edittextBrowser;
Button btngo;
Button btnsearch;
Button btncall;
Button btncallme;
Er. Girish Kumar Shakya
Android Developer
Initialized or Bind Views
edittextBrowser=(EditText)findViewById(R.id.editTextbrowser);
btngo=(Button)findViewById(R.id.buttonGo);
btnsearch=(Button)findViewById(R.id.buttonSearch);
btncall=(Button)findViewById(R.id.buttoncall);
btncallme=(Button)findViewById(R.id.buttoncallme);
Er. Girish Kumar Shakya
Android Developer
Perform Listener and Event
btngo.setOnClickListener(this);
btnsearch.setOnClickListener(this);
btncall.setOnClickListener(this);
btncallme.setOnClickListener(this);
Er. Girish Kumar Shakya
Android Developer
switch(v.getId()){
case R.id.buttonGo:
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://"+edittextBrowser.getTe
xt().toString()));
startActivity(intent);
break;
case R.id.buttonSearch:
intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
break;
case R.id.buttoncall:
intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
break;
case R.id.buttoncallme:
intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:9584458363"));
startActivity(intent);
break;
}
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer


ListView is used to display a scrollable list of
items.
ListView gets the data to display via an
adapter.
Er. Girish Kumar Shakya
Android Developer

ListView
ListActivity

setListAdapter(ArrayAdapter<T>);

setOnItemselectedListener();

Er. Girish Kumar Shakya
Android Developer
<?xml version="1.0" encoding="utf-
8"?>
<TextView
xmlns:android="http://schemas.andr
oid.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp"
android:textSize="25dp"
>
</TextView>
Er. Girish Kumar Shakya
Android Developer
public class ListSampleDemoActivity extends ListActivity {
ArrayList<String> days;
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
days=new ArrayList<String>( );
days.add("Sunday"); days.add("Monday"); days.add("Tuesday");
days.add("Wednesday"); days.add("Thursday"); days.add("Friday");
days.add("Saturday");
setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,days));
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
…….
…….
setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,days));
ListView listview=getListView();
…
…
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
……
…..
ListView listview=getListView();
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int
position,
long arg3) {
// TODO Auto-generated method stub
String itemname=adapter.getItemAtPosition(position).toString();
Toast.makeText(ListSampleDemoActivity.this, itemname,
Toast.LENGTH_SHORT).show();
}
});
}
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer

You can also define
your own layout for
the rows and
assign this layout
to your row
adapter. We will
add a graphic to
each list entry.
Er. Girish Kumar Shakya
Android Developer
<ImageView
android:id="@+id/icon"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_width="22px"
android:layout_marginTop="4px"
android:layout_marginRight="4px"
android:layout_marginLeft="4px">
</ImageView>
<TextView
android:text="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label"
android:textSize="30px">
</TextView>
Er. Girish Kumar Shakya
Android Developer
public class ListViewWithGraphicsActivity extends
}
Activity
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Er. Girish Kumar Shakya
Android Developer
{
ListActivity
public class ListViewWithGraphicsActivity extends
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Er. Girish Kumar Shakya
Android Developer
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,
R.id.label, names));
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,
R.id.label, names));
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,
R.id.label, names));
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,
R.id.label, names));
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,R.id.label, names));
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,R.id.label,
names));
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
……
…..
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.main,R.id.label, names));
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int
position,
long arg3) {
// TODO Auto-generated method stub
String item=adapter.getItemAtPosition(position).toString();
Toast.makeText(ListViewWithGraphicsActivity.this, item,
Toast.LENGTH_SHORT).show();
}
});
}
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
o
o
o
A Browser
Loading It Up
Navigation
Er. Girish Kumar Shakya
Android Developer



WebView is not significantly different than
any other widget in Android – pop it into a
layout, tell it what URL to navigate to via Java
code, and you're done.
loadUrl();
loadData();
Er. Girish Kumar Shakya
Android Developer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Er. Girish Kumar Shakya
Android Developer
public class WebBrowserDemoActivity extends Activity {
WebView browser;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
browser=(WebView)findViewById(R.id.webkit);
browser.loadUrl("http://www.google.com");
}
}
Er. Girish Kumar Shakya
Android Developer
<uses-permission
android:name="android.permission.INTERNET"></uses-
permission>
Er. Girish Kumar Shakya
Android Developer
browser.loadData(“
<html><body>Hello, world!</body></html>“ ,
"text/html", "UTF-8“
);
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer









reload()
goBack()
goForward()
canGoForward()
goBackOrForward()
canGoBackOrForward()
goBackOrForward()
clearCache()
clearHistory()
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar
Shakya(DAC,CDAC,Pune(Mh.)
Er. Girish Kumar Shakya
Android Developer




Particularly used to store your data.
That can be Shareable within your all
activities in an application.
Or might be Shareable across application.
Data sharing done by using key/values
pairs(akin to Map).
Er. Girish Kumar Shakya
Android Developer

To get access to the preferences, you have
two APIs to choose from:
1. getPreferences() from within your Activity,
to access activity specific preferences
2. getSharedPreferences() from within your
Activity (or other application Context), to
access application-level preferences
Er. Girish Kumar Shakya
Android Developer
Get the preference by using:
SharedPreferences settings=getPreferences(0);

Create an editor to edit your Preferences
SharedPreferences.Editor editor=settings.edit();



Set your preference by using put Methods
e.g. editor.putBoolean(“key”,false);
Commit it using editor.commit();
Er. Girish Kumar Shakya
Android Developer




Use additional method if necessary:
remove() to get rid of a single named
preference
clear() to get rid of all preferences
commit() to persist your changes made via
the editor
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
Create
a Project Named PreferenceDemo
xmlns:android="http://schemas.android.com/a
Name
the main Activity
pk/res/android"
PreferenceDemoActivity
android:layout_width="fill_parent"
Editandroid:layout_height="fill_parent">
the main.xml file:
<CheckBox android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This checkbox is: unchecked" />
<Button android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
Er. Girish Kumar Shakya
</LinearLayout>
Android Developer
//inside
onCreate()
write some to code to perform checked change
 Open
PreferenceDemoActivity
@Override
when //Inside onCreate() write some code to close the
 Edit it:
protected
@Override
voidononPause()
{ close is clicked
//user
clicked
checkedButton
//application
when Button
chb=(CheckBox)findViewById(R.id.
//set Auto-generated
onclicklistener
tocheck);
close
// TODO
protected
void onResume()
method
{ the application
stub
chb.setOnCheckedChangeListener(new
OnCheckedChangeListener()
{
public class PreferenceDemoActivity extends
Button
close=(Button)findViewById(R.id.
// TODO
super.onPause();
Auto-generated
method stubclose);
Activity {
public
void
onCheckedChanged(CompoundButton
buttonView,
SharedPreferences
super.onResume();
settings=getPreferences(0);
CheckBox
close.setOnClickListener(new
OnClickListener() {
boolean
isChecked)chb;
{
SharedPreferences.Editor
editor=settings.edit();
}public
// TODO
Auto-generated
method stub
void onClick(View
v) {
SharedPreferences
settings=getPreferences(0);
if(chb.isChecked()){
// TODO Auto-generated
method stub
chb.setText("This checkbox is: checked");
editor.putBoolean("chb_checked",
chb.setChecked(settings.getBoolean("chb_checked
finish();
}else{
}
chb.isChecked());
",
false));
chb.setText("This
checkbox is: unchecked");
});}
}editor.commit();
}
}});
Er. Girish Kumar Shakya
Android Developer


Finally run your program….
And see the difference…
Er. Girish Kumar Shakya
Android Developer


Xml folder
PreferenceActivity
Er. Girish Kumar Shakya
Android Developer
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="tableno"
android:title="Select A number for Table"
android:summary="Table Demo"
android:defaultValue="1"
android:entries="@array/tablenumberlist"
android:entryValues="@array/tablenumbervalues"></ListPreference>
<CheckBoxPreference
android:key="chekboxsum"
android:title="Check Box Demo"
android:summary="Find the sum of ten number"
></CheckBoxPreference>
<EditTextPreference
android:key="editTextPref"
android:title="Edit Text Preference"
android:dialogMessage="Enter a Text here"
android:dialogIcon="@drawable/icon"
android:positiveButtonText="OK"
android:negativeButtonText="Cancel"
android:summary="This is just a Text Demo"
></EditTextPreference>
<RingtonePreference
android:key="ringtonePref"
android:title="Set Ringtone Preference"
android:showSilent="true"
android:ringtoneType="all"
android:summary="Set Ringtone"></RingtonePreference>
</PreferenceScreen>
Er. Girish Kumar Shakya
Android Developer
public class TablePreference extends PreferenceActivity
implements OnPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.mytable);
}
Er. Girish Kumar Shakya
Android Developer
ListPreference listpreference=(ListPreference)findPreference("tableno");
CheckBoxPreference
chkpreference=(CheckBoxPreference)findPreference("chekboxsum");
EditTextPreference editpreference=(EditTextPreference)findPreference("editTextPref");
RingtonePreference
ringtpreference=(RingtonePreference)findPreference("ringtonePref");
listpreference.setOnPreferenceChangeListener(this);
chkpreference.setOnPreferenceChangeListener(this);
editpreference.setOnPreferenceChangeListener(this);
ringpreference.setOnPreferenceChangeListener(this);
}
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
Toast.makeText(this, newValue.toString(), Toast.LENGTH_SHORT).show();
return true;
}
}
Er. Girish Kumar Shakya
Android Developer
<activity android:name=".TablePreference"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer





Introducing SQLite
DDL Statements
Modification Statements
Query Statements
Using SQLite in Android.
Er. Girish Kumar Shakya
Android Developer


SQLite is a powerful Relational Database
engine created by Dr. Richard Hipp in
2000.
SQLite can be found in the Apple iPhone,
Symbian phones, Mozilla Firefox, Skype,
PHP, Adobe AIR, Mac OS X, Solaris, and
many other places.
Er. Girish Kumar Shakya
Android Developer





It is well Regarded, being:
Open Source
Standard-Complaint
Lightweight
Single-tier
Er. Girish Kumar Shakya
Android Developer




Three reason for its popularity:
It's free
It's small
It require no setup or Administration
Er. Girish Kumar Shakya
Android Developer


Data Definition Language(DDL)
Statement is used to define tables and
columns names....
create table contactlist (_id integer
primary key autoincrement,name
text,mob text not null,email
text,address text);
Er. Girish Kumar Shakya
Android Developer

create table contactlist (_id integer primary
key autoincrement,name text,mob text not
null,email text,address text);
_id
name
mob
email
address
Primary Key
Er. Girish Kumar Shakya
Android Developer


SQL provides number of statement that
let you perform CRUD operation …. like
create,retrive, update and delete of
operation in the database....
For example:
“update contactlist set name=„ashish‟
where id=„1‟”;
Er. Girish Kumar Shakya
Android Developer

“update contactlist set name=„ashish‟
where id=„1‟”;
_id name
1
ashish
mob
email
address
9039176xx
x
Girish.kum
[email protected]
n
228/B trade center
indore
Auto Generated value....
Er. Girish Kumar Shakya
Android Developer


SQL provides number of statement that
let you perform CRUD operation …. like
create,retrive, update and delete of
operation in the database....
For example:
“select * from table where _id=1”;
Er. Girish Kumar Shakya
Android Developer

“select * from table where _id=1”;
_id name
1
girish
mob
email
address
9039176xxx
Girish.kumar
@c2s.co.in
228/B trade center indore
1;girish;9039176xxx;[email protected];228/B trade
center indore
Er. Girish Kumar Shakya
Android Developer







Content Values
Using the SQLiteOpenHelper
ContactClass
Inserting Row in contactlist table
Retrieving from contactlist table
Updating contactlist table
Deleting from contactlist table
Er. Girish Kumar Shakya
Android Developer



This class is used to store values.
Each Content Values object represents a
single row, as a map of column names
to values.
Queries in Android are returned as
Cursor objects.
Er. Girish Kumar Shakya
Android Developer










The Cursor class includes several functions to
navigate query results including, but not limited
to, the following:
moveToFirst
moveToNext
moveToPrevious
getCount
getColumnIndexOrThrow
getColumnName
getColumnNames
moveToPosition
getPosition
Er. Girish Kumar Shakya
Android Developer

SQLiteOpenHelper is an abstract class
that help us to perform CRUD operation
for database.
Er. Girish Kumar Shakya
Android Developer
SQLiteOpenHelper
SQLiteOpenHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version);
SQLiteOpenHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version,
DatabaseErrorHandler errorHandler)
onCreate(SQLiteDatabase db)
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
getReadableDatabase()
getWritableDatabase()
onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
onOpen(SQLiteDatabase db)
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
public static final String
DB_NAME="contact.db";
public static final int DB_VERSION=1;
public static final String
DB_TABLE="contactlist";
////creating column
public static final String NAME="name";
public static final String PHONE_NO="mob";
public static final String EMAIL="email";
public static final String ADDRESS="address";
Er. Girish Kumar Shakya
Android Developer
"create table " +
"" +DB_TABLE+" (_id integer primary key
autoincrement," +
""+NAME+" text," + ""+PHONE_NO+" text not null,"
+
""+EMAIL+" text,"+ADDRESS+" text);";
Er. Girish Kumar Shakya
Android Developer
public ContactClass(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
Er. Girish Kumar Shakya
Android Developer
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int
arg2) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists "+DB_TABLE);
}
Er. Girish Kumar Shakya
Android Developer
ContactClass ccdatabase=new ContactClass(this);
SQLiteDatabase
sd=ccdatabase.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(key, value);
sd.insert(table, colname, contentvalues);
sd.close();
Er. Girish Kumar Shakya
Android Developer
ContactClass ccdatabase=new ContactClass(this);
SQLiteDatabase
sd=ccdatabase.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(ccdatabase.NAME, name.getText().toString());
cv.put(ccdatabase.PHONE_NO,
mobileno.getText().toString());
cv.put(ccdatabase.EMAIL, email.getText().toString());
cv.put(ccdatabase.ADDRESS, address.getText().toString());
//
sd.insert(ccdatabase.DB_TABLE, ccdatabase.NAME, cv);
sd.close();
Er. Girish Kumar Shakya
Android Developer
Cursor c=sd.query
(table,
Columns,
where,
selectionArgs,
groupBy,
having,
OrderBy);
Er. Girish Kumar Shakya
Android Developer







Table :- eg: ccdatabase.DB_TABLE
Columns :- null for all or “_id,name” for
specific
Where :- String where = _id + “=” +
requiredValue;
whereArgs :- String
whereArgs[]={“_id=1”,”name='girish'”};
GroupBy :-use here group by statement of sql
Having :- put here having query
OrderBy :-String orderBy = “name”;
Er. Girish Kumar Shakya
Android Developer
Cursor c=sd.query(ccdatabase.DB_TABLE, null,
null, null, null, null, null);
while(c.moveToNext()){
mylist.add(c.getString(0)+";"+c.getString(1)+";"+c
.getString(2)+";"+c.getString(3)+";"+c.getString
(4));
}
c.close();
Er. Girish Kumar Shakya
Android Developer
ContentValues cv=new ContentValues();
if(!(editname.getText().toString().equals("")))
cv.put(ccdatabase.NAME, editname.getText().toString());
if((editmobile.getText().toString().equals("")))
Toast.makeText(this, "mobile no cannot updated",
Toast.LENGTH_SHORT).show();
else
cv.put(ccdatabase.PHONE_NO, editmobile.getText().toString());
if(!(editemail.getText().toString().equals("")))
cv.put(ccdatabase.EMAIL, editemail.getText().toString());
if(!(editaddress.getText().toString().equals("")))
cv.put(ccdatabase.ADDRESS, editaddress.getText().toString());
String where="_id="+editid.getText().toString();
sd.update(ccdatabase.DB_TABLE, cv, where, null);
sd.close();
Er. Girish Kumar Shakya
Android Developer
SQLiteDatabase
sd=ccdatabase.getWritableDatabase();
String where="_id="+_id;
sd.delete(ccdatabase.DB_TABLE, where, null);
sd.close();
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer & Trainer
Er. Girish Kumar Shakya
Android Developer





Introduction
Playing Audio Content
Playing Video Content
Audio Recording
Image Capture
Er. Girish Kumar Shakya
Android Developer











android.media
Web
asset
Secure Digital Card (SD) Card
Codex:
3GPP
MP3
MIDI
PCM/WAVE
MPEG-4
http://developer.android.com/guide/appendix/media-formats.html
Er. Girish Kumar Shakya
Android Developer



android.media.MediaPlayer
playAudio()
KillMedia()
Er. Girish Kumar Shakya
Android Developer


android.media.MediaPlayer : MediaPlayer
class can be used to control playback of
audio/video files and streams.
Some Important methods :
reset(), setDataSource(), prepare(), start(),
seekTo(), pause(), getCurrentPosition(),
isLooping(), isPlaying(), release(), seekTo()
Er. Girish Kumar Shakya
Android Developer

MediaPlayer (constructing)
MediaPlayer mediaPlayer=
MediaPlayer.create(context, resid);
e.g.
MediaPlayer mediaPlayer=
MediaPlayer.create(this, R.raw.song1);
MediaPlayer mediaPlayer=
new MediaPlayer();
e.g.
MediaPlayer mediaPlayer=
new MediaPlayer();
Er. Girish Kumar Shakya
Android Developer

MediaPlayer (setting Data source)
String path=”/sdcard/mysong.mp3”;
mediaPlayer.setDataSource(path);

MediaPlayer (prepare for media)
mediaPlayer.prepare();

MediaPlayer (start media)
mediaPlayer.start();
Er. Girish Kumar Shakya
Android Developer

MediaPlayer (Seek to Position)
mediaPlayer.seekTo(0); // supply int value

MediaPlayer (Pause media)
mediaPlayer.pause();

MediaPlayer (find current Position)
int currentposition=mediaPlayer.getCurrentPosition();
Er. Girish Kumar Shakya
Android Developer

MediaPlayer (find media is playing or not)
mediaPlayer.isPlaying(); // supply int value

MediaPlayer (if everything done release
media)
mediaPlayer.release();
Er. Girish Kumar Shakya
Android Developer
public void playAudio() throws Exception{
KillMediaPlayer();
}
mediaPlayer=new MediaPlayer();
mediaPlayer.setDataSource(AUDIO_PATH);
mediaPlayer.prepare();
mediaPlayer.start();
Er. Girish Kumar Shakya
Android Developer
public void killMediaPlayer(){
try{
if(mediaPlayer!=null){
mediaPlayer.release();
}
}catch(Exception e){
}
}
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
<Button
android:id="@+id/buttonstart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Player" />
<Button
android:id="@+id/buttonrestart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Restart Player" />
<Button
android:id="@+id/buttonpause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pause Player" />
Er. Girish Kumar Shakya
Android Developer
final String AUDIO_PATH="/sdcard/Cuppycake_Song_.mp3";
MediaPlayer mediaPlayer;
int loopback=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//retrive all button from xml
Button playbtn=(Button)findViewById(R.id.buttonstart);
Button restartbtn=(Button)findViewById(R.id.buttonrestart);
Button pausebtn=(Button)findViewById(R.id.buttonpause);
…......................
….....................
}
Er. Girish Kumar Shakya
Android Developer
playbtn.setOnClickListener(new OnClickListener() {
});
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
playAudio();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Er. Girish Kumar Shakya
Android Developer
pausebtn.setOnClickListener(new OnClickListener() {
});
public void onClick(View arg0) {
// TODO Auto-generated method stub
loopback=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}
Er. Girish Kumar Shakya
Android Developer
restartbtn.setOnClickListener(new OnClickListener() {
});
public void onClick(View arg0) {
// TODO Auto-generated method stub
mediaPlayer.seekTo(loopback);
mediaPlayer.start();
}
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer


android.widget.VideoView
android.widget.MediaController
Er. Girish Kumar Shakya
Android Developer

android.widget.VideoView
Displays a video file. The VideoView class can load
images from various sources (such as resources or content
providers), takes care of computing its measurement from
the video so that it can be used in any layout manager, and
provides various display options such as scaling and tinting.
Important methods:
setMediaController(MediaController);
setVideoPath(VIDEO_PATH);
requestFocus();
start();
Er. Girish Kumar Shakya
Android Developer

android.widget.MediaController
A view containing
MediaPlayer.
Contains buttons like

Play/Pause

Rewind

Fast Forward

Progress slider
controls
for
a
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
<VideoView
android:id="@+id/videoView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
VideoView videoView=(VideoView)findViewById(R.id.videoView1);
MediaController mc=new MediaController(this);
mc.setBackgroundDrawable(getWallpaper());
videoView.setMediaController(mc);
videoView.setVideoPath("/sdcard/320474911302296_31015.mp4");
videoView.requestFocus();
videoView.start();
Er. Girish Kumar Shakya
Android Developer



android.provider.MediaStore.RECORD_SOUND
startRecording();
onActivityResult();
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
android.provider.MediaStore.RECORD_SOU
NDvoid startRecording(){
public

Intent intent=
new Intent("android.provider.MediaStore.RECORD_SOUND");
startActivityForResult(intent, 0);
}
Er. Girish Kumar Shakya
Android Developer

android.provider.MediaStore.RECORD_SOU
ND
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 0:
if(resultCode== RESULT_OK){
Uri recordedAudioPath=data.getData();
}
}
}
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
public void captureImage(View view){
ContentValues values=new ContentValues();
values.put(Media.TITLE, "My demo Image");
values.put(Media.DESCRIPTION,
"Image capture by Camera via an Intent");
myPicture=getContentResolver()
.insert(Media.EXTERNAL_CONTENT_URI, values);
Intent intent=
new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, myPicture);
startActivityForResult(intent, 0);
}
Er. Girish Kumar Shakya
Android Developer
@Override
protected void onActivityResult(int
requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode,
resultCode, data);
if(requestCode==0 && resultCode==
Activity.RESULT_OK){
}
}
Er. Girish Kumar Shakya
Android Developer
Er. Girish Kumar Shakya
Android Developer
Related documents