Download android:layout_height="wrap_content"

Document related concepts
no text concepts found
Transcript
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Android
Introduction
Open source
Developed by Google
First device released in 2008
Devices
Compatibility
Pre-requisites
Hardware requirements
Development Platforms
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
1
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Android Framework
Android Application
Android API’s
Android Libraries
Linux Kernel
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
2
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Android App Preparation
.java file
.java file
.class file
.class file
.dex file
.dex file
.xml file
.dex file
Res files
.apk file
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
3
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Installation
Notepad + Command Prompt
Netbeans / Eclipse + Android SDK + ADT
Android Studio
JDK
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
4
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Project Structure
App
manifests
Drawables
Strings
Java
Layout
Dimens
Res
Values
Colors
Mipmap
Styles
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
5
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Manifest file
Configuration file
Defines Activities, Services, Receivers, Permissions, Features, Providers etc
One Manifest file per app
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
6
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Creating App
Define App name
Define target Device
Choose Activity
Choose Minimum API to support
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
7
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Physical Device
Enable Developer Options
Enable USB debugging mode
Install drivers if required
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
8
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Android Virtual Device
Default Virtual Devices
Screen size
Clone Virtual Devices
RAM
Memory Storage
New Virtual Device
Hardware features
Android Version
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
9
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Android Layout
Defines User Interface
Defined through XML file
Each screen will have one or more XML files
Separates the presentation layer from business layer
Defined in app => res => layout folder
Layout can be edited in Code or Design mode
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
10
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Types of Layout
LinearLayout
RelativeLayout
TableLayout
GridLayout
FrameLayout
DrawerLayout
Layouts can be nested
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
11
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
LinearLayout
Present the UI element in Vertical or Horizontal orientation
Defined usng <LinearLayout> tag
Orientation attribute is used to specify the orientation of layout
Username
Username
Password
Password
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
12
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Nested LinearLayout
Name:
Username
Password:
Password
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
13
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
RelativeLayout
Elements are arranged relative to other elements
Name:
Username
Password:
Password
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
14
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
TableLayout
Elements are arranged in rows
Emp No
Name
Salary
1
SKONDA
10000
2
ANDROID
20000
Related tags:
<TableLayout>
<TableRow>
Disadvantages
Rowspan and colspan is not possible
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
15
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
GridLayout
Elements are arranged in rows and columns
Emp No
Name
1
SKONDA
2
ANDROID
Salary
10000
Colspan and rowspan is possible
Related tags:
<GridLayout>
Related Attributes:
rowCount
columnCount
layout_column
layout_row
layout_rowSpan
layout_columnSpan
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
16
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Java based Activity
An activity can be created without XML file
Android provides several classes to create elements
setContentView is used to inflate the layout
addView() is used to add elements
Elements can be styled using setter methods
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
17
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Activity Life Cycle
onCreate()
onStart()
onResume()
Activity visible
onRestart()
onPause()
onStop()
onDestroy()
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
18
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Activity Life Cycle
Method
Description
onCreate()
views are created in this state, UI not visible
onStart()
UI visible but user cannot interact
onResume()
user can interact with views
onPause()
called when app is visible but lost focus, like getting a phone call
onStop()
called when app is not visible on foreground
onRestart()
called when a stopped activity is started again
onDestroy()
called when app is closed or navigated to another activity
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
19
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Activity Life Cycle
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "oncreate executed",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(MainActivity.this, "activity started",
Toast.LENGTH_SHORT).show();
}
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
20
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
UI Elements
Also called as Widgets / Views
Widget
Description
TextView
like a label to display text
EditText
input text field to enter data
Button
Button element to trigger an event
ListView
a group of views displayed as a list
Spinner
dropdown menu listing values
CheckBox
allow selection of an element
RadioButton
provides multiple options to choose one
Switch
on / off switch
ToggleButton
button with yes or no option
ImageView
to embed an image
ImageButton
button with image
ScrollView
scrollbars when screen has more elements that it can fit
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
21
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
TextView Element
Used to display text
Important attributes:
id
text
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
22
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
EditText Element
Used as a input element
Important attributes:
id
hint
type
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200dp" />
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
23
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Button Element
Used for click event listener
Important attributes:
id
text
onclick
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
24
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Button Event Handling
onClick=methodname
View.OnClickListener Interface
override onClick(View v) method
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
25
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Multiple Button Event Handling
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content“
android:id=“skondaBtn1”
android:onClick=“skondaMet1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content“
android:id=“skondaBtn2”
android:onClick=“skondaMet1"/>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
26
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Multiple Button Event Handling
View.OnClickListener interface
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.skondaBtn1:
Toast.makeText(MainActivity.this, "this is button1 by SKONDA",
Toast.LENGTH_SHORT).show();
break;
case R.id.skondaBtn2:
Toast.makeText(MainActivity.this, "this is button2 by SKONDA",
Toast.LENGTH_SHORT).show();
break;
case R.id.skondaBtn3:
Toast.makeText(MainActivity.this, "this is button3 by SKONDA",
Toast.LENGTH_SHORT).show();
break;
}
} By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
27
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ImageButton Element
Image works as a button element
Important attributes:
id
src
onClick
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/skondaImage“
android:onClick="onClickButton"/>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
28
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ImageView Element
To display Image on the screen
Important attributes:
id
src
content
scaleType
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/skondaImage“
/>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
29
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
CheckBox element
To display list of options
User can select more than one option
Important attributes:
id
text
onClick
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/skondaCkb"
android:text="CPP"
android:onClick="listenCB"
/>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
30
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
CheckBox Event Handling
CompoundButton.OnCheckedChangeListener interface
Register the element
Override onCheckedChange(CompoundButton, boolean)
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
31
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
RadioButton Element
Used to select one option out of several
RadioButton and RadioGroup elements are used
Event Handling
RadioGroup.OnCheckedChangeListener interface
Register the element
Override onCheckedChange(RadioGroup, int)
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
32
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Switch Element
Scrollbar type on/off button
Switch element is used to define the element
Event Handling
CompoundButton.OnCheckedChangeListener interface
Register the element
Override onCheckedChange(CompountButton, Boolean)
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
33
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ToggleButton Element
On/off button
Event Handling
CompoundButton.OnCheckedChangeListener interface
Register the element
Override onCheckedChange(CompountButton, Boolean)
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
34
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ListView Element
Used to list elements
Entries (xml)
ArrayAdapter (programmatically)
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
35
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ListView Entries
Strings.xml
<resources>
<array name="names">
<item> C </item>
<item> C++ </item>
<item> Java</item>
</array>
</resources>
<ListView
android:layout_width="wrap_content“
android:layout_height="wrap_content"
android:entries="@array/names“
>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
36
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ListView ArrayAdapter
In XML
Define ListView
In program:
Define List items as an array of elements
Get the reference of ListView and associate array with ListView using
ArrayAdapter
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
37
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ListView Entries
ListView lv = (ListView) findViewById(R.id.lv);
String[] courses = {"C", "C++", "Java", "Web Designing"};
ArrayAdapter<String> ad =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, courses);
lv.setAdapter(ad);
Layout
Array
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
38
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ListView with Custom Layout
ArrayAdapter<String>(this, R.layout.listview_skonda,courses);
<TextView xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/colorAccent"
android:height="40dp"
android:paddingTop="10dp"/>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
39
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
ListView Item Event Handling
AdapterView.OnItemClickListener interface
Register for listener using setOnItemClickListener() method
Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString()
}
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
40
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Spinner Element
Used to create a dropdown list of items
Implementation similar to ListView
AdapterView.OnItemSelectedListener interface
Register for listener using setOnItemSelectedListener() method
Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString()
}
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
41
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Other Event Handlers
OnFocusChangedListener
TextWatcher
OnLongClickListener
OnTouchListener
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
42
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Resources
Drawables
Mipmap
Layout
Values
• Strings
• Styles
• colors
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
43
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Using Resources
XML
@string/app_name
@color/primary_dark
Example:
android:textColor="@color/colorBlue"
Code
blue = getResources().getColor(R.color.colorBlue);
tv.setTextColor(blue);
tv.setText("This is blue color");
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
44
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Dimensions
Dimension
dp
sp
pt
px
mm
inch
Description
Density independent pixels. This dimension is scaled
based on the density of the device. This is the
commonly used dimension as the elements scale
according to the screen density.
scale independent pixels. This is also like dp which
scales according to the density and also scales
according to user's preferred font size.
1/72 of inch, measured with respect to physical
screen
pixels, represents actual pixels on screen. This is not
recommended measurement unit as number of pixels
change as per screen
millimeters, with respect to physical size of screen.
Not recommended dimension
with respect to physical size of screen. Not
recommended
Device
Independent
Yes
Yes
No
No
No
No
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
45
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Get Configuration
getResources().getConfiguration() method is used
to get the system configuration details.
Attribute
Description
densityDPI
return density in Dots Per Inch
screenWidthDp
returns screen width in Density in Pixels
screenHeightDp
returns screen height in Density in Pixels
smallestScreenWidthDp
returns smallest width of screen in DP
touchscreen
used to check if touchscreen available
orientation
returns 1 for portrait, 2 for landscape
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
46
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
System Resources
Android provides various system resources like
strings, color, images etc to use in the application.
These resources can be accessed using android.R class.
To browse through available system resources, open project in
=>
Packages mode
=>
App
=>
Android Support Library
=>
R class
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
47
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Shape Element
border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="3px" android:color="@color/colorAccent" />
<size android:width="300px" android:height="300px" />
<corners android:radius="150px" />
</shape>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="@drawable/border"
android:textSize="24sp" />
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
48
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Layer List
border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
49
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Working with Multiple Screens
• Orientation
• Screen Size
• Density
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
50
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Orientation
Portrait and Landscape
-port and –land qualifiers can be used for resources
Orientation can be set programmatically using
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
or
screenOrientation attribute can be used in the manifest file to set the
orientation
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
51
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Density
ldpi
mdpi
hdpi
xhdpi
xxhdpi
Density
dpi
dp
px
ldpi
<120 dpi
1
0.75
mdpi
<160 dpi
1
1
hdpi
<240 dpi
1
1.5
xhdpi
<320 dpi
1
2
xxhdpi
<480 dpi
1
3
xxxhdpi
<640 dpi
1
4
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
52
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Screensize
small
normal
large
xlarge
Specification
Qualifier
value
Example
smallest
width
sw
-sw600dp
width
w
-w600dp
height
h
-h600dp
Description
screen size with minimum width
600dp irrespective of orientation.
Even in landscape mode, the
smallest width of the screen is
considered.
screen size with minimum width
600 dp, this will change with
orientation
screen size with minimum height
600 dp, this will change with
orientation
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
53
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Different Languages
-en
-fr etc
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
54
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Publishing App
Generate Signed APK
Developer Console Account
Google Play Store / Google Play Website
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
55
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Google Play Developer Console
Number of
downloads
Ratings
Review
Optimization tips
Any crash report
Ad Campaign
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
56
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Intent
Two types:
Explicit
Implicit
Used to Launch Activities
Perform a specific action
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
57
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Intent
Two types:
Explicit
Implicit
Used to Launch Activities
Perform a specific action
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
58
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Explicit Intent
Used to launch an acitivity
Intent intent = new Intent(this, target.class);
StartActivity(intentname);
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
59
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Pass and receive data
Pass data:
Intent intent1 = new Intent(MainActivity.this, Main2Activity.class);
intent1.putExtra("username","SKONDA IT Services");
startActivity(intent1);
Receive data:
Intent intent2 = getIntent();
String username = intent2.getStringExtra("username");
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
60
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Receive data back
startActivityForResult is used to receive data back from called activity
Intent <intentname> = new Intent(this, target.class);
startActivityForResult(intentname, request_code);
Intent intent = new Intent();
intent.putExtra("company", "skonda it services");
setResult(RESULT_OK, intent);
public void onActivityResult(int requestcode, int resultcode,
Intent data)
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
61
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Implicit Intent
Used to start an activity based on action
Syntax:
Intent intent = new Intent(action);
startActivity(intent);
Example:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(EXTRA_TEXT, message);
startActivity(intent);
https://developer.android.com/guide/components/intents-common.html
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
62
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
User defined Implicit Intents
User can define an implicit intent
</activity>
<activity android:name=".subactivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
63
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Create Chooser
Default chooser can be overridden by defining a chooser
Intent intent = new Intent(Intent.ACTION_SEND)
Intent intentChoosen = Intent.createChooser(intent, message);
startActivity(intentChoosen);
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
64
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Background Processing
Reduce load on main thread
Avoid ANR messages
AsyncTask
Threads
Loaders
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
65
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
AsyncTask
Class AsyncTask is used to run task in background
Provides call back methods
Advantages:
Easy to implement
Access to UI
Disadvantages:
Multiple tasks cannot be executed with one class
Activity Configuration changes will loose Asynctask
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
66
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
AsyncTask
private class SkondaLoad extends AsyncTask<Params, Progress, Result>
Method
onPreExecute()
doInBackground(Params. . .
params)
onPostExecute(Result result)
publishProgress(Progress...
values)
onProgressUpdate(Progress...
values)
Description
Runs on UI thread before doInBackground()
process task in background
runs on UI thread after doInBackground() completes
invoked from doInBackground() to publish updates if
required
runs on UI thread after publishProgress() completes
skondaLoad.execute();
executeForExecutor(Executor, params);
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
67
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Fragments
Fragments are re-usable components on the UI
Fragments have their own life cycle and run within an acitivity
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
68
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Fragments
Creation of Fragments
Fragment
Class
Fragment
XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
69
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Fragments
Including Fragments in an Activity
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="in.skonda.skondafrag.DetailFragment"
android:id="@+id/dfrag“
/>
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
70
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Fragments
Dynamically adding fragments
Framelayout & Fragment Manager
getFragmentManager().beginTransaction().add(R.id.fl, new NFragment()).commit();
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
71
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Saving Instance state
It is required to store the instance state when activity is
destroyed and recreated
saveInstanceState(Bundle) method
onCreate(Bundle) / onRestoreInstance(Bundle)
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
72
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Action bar
Also Called as App bar
Used to display
• logo
• Title
• Screen Name
• Search option
• Action Items
• Overflow Menu
• Settings
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
73
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Action bar Action Items
Menu folder => items.xml
onCreateOptionsMenu(Menu)
getMenuInflater.inflate(R.Menu.Items)
Creating sub menu
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
74
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Removing default Action bar
getSupportActionBar()
hide()
show()
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
75
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Event Handling with Action Items
onOptionsItemSelected(MenuItem)
hide()
show()
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
76
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Content Menu
Used to display list of action items
Floating Content Menu
Action Mode Content Menu
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
77
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Floating Context Menu
Register the element for Context menu
registerForContextMenu(element);
Inflate the menu items, override
onCreateContextMenu()
Event listener, override
onContextItemSelected()
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
78
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
Action Mode Context Menu
Depends on Elements or Listview
For elements:
use onLongClick() and startActionMode(Actionmode.Callback)
For ListView:
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
By SKONDA IT Services PVT LTD, Mob: 7673988855 URL: www.skonda.in
79