58
Android development basics Introduction,Structure,Dev elopment Boncho Valkov .Net Developer

Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Embed Size (px)

Citation preview

Page 1: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Android development basicsIntroduction,Structure,Development

Boncho Valkov.Net Developer

Page 2: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Table of Contents

1. About Android and why to use it2. What knowledge do you need to start

Basics of programming,OOP,Databases

3. What software (IDEs) do you need Android Studio/Eclipse Emulator – Genymotion or other

4. Structure of Android app5. Main components – Intent,Activity,Service,Content

Provider,Broadcast Receiver6. Activities lifecycle 2

Page 3: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Table of Contents

7. Debugging and logging8. Intents, passing data between activities9. Views. Layout Management10.Listviews and Adapters11.Notifications12.HTTP Networking. AsyncTasks and Callbacks13.Local Storage – SQLLite and SharedPrefs. Content Provider.

3

Page 4: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

4

About Android and why to use it

Page 5: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

5

Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google.

Started back in 2008, today is the most popular mobile OS with 1.5 million daily activations, 1 billion devices in total (as of April 2013).

About Android

Page 6: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

6

About Android - Architecture

Page 7: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

7

The most popular mobile OS Large community Extremely well documented APIs Reuse existing knowledge in Java development Uses MVC

Why android

Page 8: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

8

Basics of programming in some object oriented language like C#, Java or other

OOP principals Databases – SQL/MYSQL or other XML

What do you have to know to start

Page 9: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

9

1. For programming Android studio – for a couple of months there is

official version Eclipse plus SDK tools

2. Emulator Genymotion Integrated emulator in android studio Test on actual device

What software (IDEs) do you need

Page 10: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

10

Select API Level Using Packages like JAVA Java folder Res folder Android Manifest file Gradle

Structure of Android project

Page 11: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

11

Main components

Activity Intent Service Content Provider -------------------- Broadcast Receivers Fragments Views

Page 12: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

12

Represents a single screen with an user interface plus code that runs behind it

If we have multiple activities – one must be selected as the app’s main activity.

The main activity is similar to the main() function in Java and C#

Activity

Page 13: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

13

Objects carrying messages from one component to another (within the application or outside it).

Two types of intents: • Explicit intents – tell exactly what do

you want to start to get some result• Implicit intents – tell that you what

some result, without specifying how

Intent

Page 14: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

14

Runs in the background to perform long- running operations. Doesn’t need to interact with the user.

Service

Page 15: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

15

Supplies data from one application to others on request.

- Can use different ways to store data (database, files, over a network).

Content Providers

Page 16: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

16

Respond to broadcast messages from other applications or from the system itself (called events or intents).

Applications can also initiate broadcasts to let other applications know that some data has been downloaded or processed.

Broadcast Receivers

Page 17: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

17

Kind of sub-activity Can be placed in an Activity to achieve more modular design.

-You can add or remove fragments in an activity while the activity is running.

Fragments

Page 18: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

18

Layouts Widgets

Menu Drawer Tabs

Can be created via: -XML files Programmatically

Views

Page 19: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Activity Lifecycle

Page 20: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

20

onCreate onStart onResume – application is running onPause onStop onDestroy

Activity Lifecycle

Page 21: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Demo

Page 22: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Debugging and logging

Page 23: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

23

F8 – step forward F9 – go next breakpoint Log.v() -VERBOSE Log.d() -DEBUG Log.i() -INFO Log.w() -WARN Log.e() -ERROR

Debugging and logging

Page 24: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Demo

Page 25: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Intents, passing data between activities

Page 26: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

26

Intents

What is an intent messages you can pass between your app components

can also send them to components in other apps, and execute a task like playing music, sending email and taking pictures.

Page 27: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

27

With an Intent you can Start activities Start services Deliver broadcasts Pass data

Intents

Page 28: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

28

Types of intents Implicit

have not specified a component they must include enough information for the system to determine

which of the available components is best to run for that intent.

Intents

Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse("http://www.softuni.bg ")); startActivity(intent);

Page 29: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

29

Intents

Implicit

Page 30: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

30

Types of intents Explicit

have specified a component, which provides the exact class to be run

usually is used for starting internal activities

Intents

Intent intent = new Intent (this,SecondActivity.class); startActivity(intent);

Page 31: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

31

Put the data in the intent

Retrieve the data from the intent from the component that has been started, usually in onCreate method

Serializable objects can be passed

Passing data between activities

Intent intent = getIntent();String passedData = intent.getExtra(“data”);

Intent intent = new Intent (this,SecondActivity.class);intent.putExtra("data","i have been passed"); startActivity(intent);

Page 32: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

32

We use the intent filters to deny or allow intents to reach our app’s components.

Restrict access

Intent Filters

<activity android:exported = false>

<action android:name="android.intent.action.SEND"/> <data android:mimeType="text/plain"/> <category android:name="android.intent.category.DEFAULT"/>

Page 33: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Demo

Page 34: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Views. Layout Management

Page 35: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

35

Views. Layout Management Mandatory view props

android:layout_width android:layout_height

Match parent Fill parent Wrap Content

Page 36: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

36

Single direction, vertically or horizontally Specified by the android:orientation attribute All children are stacked one after another Can use gravity

Linear Layout

Page 37: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

37

Displays child views in relative positions The position of each view can be specified as relative to sibling

elements Can align two elements by right border

Relative Layout

Page 38: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Demo

Page 39: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

List Views and Adapters

Page 40: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

40

Parts of the ListView: Rows Adapter -bridge between a

view and the underlying data for that view. Provides access to the data items.

Fast Scrolling Section Index

List Views and Adapters

Page 41: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

41

List Views and Adapters

Types of list views and adapters

Page 42: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

42

Simple List View<ListView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/listView"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true" />

Create ArrayAdapter

Set Adapter to the listView

Set OnClickListener

Page 43: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

43

Most of the time we need something specific

Create custom row Create Custom Adapter that extents

BaseAdapter Implement required methods

Custom List View Adapter

convertView = LayoutInflater.from(context)

.inflate(R.layout.university_row,parent,false);

Page 44: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Demo

Page 45: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Notifications

Page 46: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

46

Three required components Icon Content title Content text

Optional components Action attached to notification, usually an Activity, it’s done by

Pending Intent, by calling set Content Intent

Notifications Components

Page 47: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

47

Notification sample

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.notification_icon)

.setContentTitle("My notification")

.setContentText("Hello World!");

If want to add action to Notification Create Intent Create PendingIntent, by PendingIntent.getActivity Use setContentIntent to attach the pending Intent

Page 48: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

DemoAlong with the live demo

Page 49: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

HTTP Networking. AsyncTasks and Callbacks

Page 50: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

50

Http

Hypertext Transfer Protocol (HTTP) HTTP is the foundation of data

communication for the World Wide Web.

Needs permmitions

HttpClient defaultHttpClient = new DefaultHttpClient(new BasicHttpParams());

HttpPost httpPost = new HttpPost(path);

HttpResponse response = defaultHttpClient.execute(httpPost);

Page 51: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

51

Class that that is being executed on a different thread Prevent using the Main thread (UI) for time taking operations Extend class with AsyncTask<String,Void,String>

AsyncTask

Page 52: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Demo

Page 53: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Local Storage – SQLLite and SharedPrefs. Content Provider.

Page 54: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

54

Manage access to a structured set of data Encapsulate the data, and provide mechanisms for defining data

security The standard interface that connects data in one process with

code running in another process.

Content Providers

Page 55: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

55

Software library that implements a self-contained, zero-configuration, transactional SQL database engine.

The most widely deployed SQL database engine in the world.

Useful tools and queries: - DB Browser for SQLite Create, Select,

Delete, Update

SQLite

Page 56: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

56

Shared Preferences Store private primitive data in key-value pairs.

Local storage

SharedPreferences sp = getSharedPreferences(PREFS, Context.MODE_PRIVATE);

Editor editor = sp.edit();

editor.putString("key", "value");

editor.commit();

if (sp.contains(Name))

name.setText(sp.getString(Name, ""));

Page 57: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Demo

Page 58: Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer

Questions?