Your First Android Project! презентация

Содержание

Слайд 2

You can trust me

Mobile Consultant
7+ years of Android, 9+ years total
Conferences & meetups,

big & small
KUG & GDG SPb, Android Academy SPb & Msk!

@colriot

Слайд 3

Checklist

Android Studio, Java, Git are ready to use
Checked-in on Eventbrite
Joined slack (android-academy-msk) https://bit.ly/2NIR0x0


Android Studio, Kotlin, Git are ready to use

Слайд 4

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 5

Android Studio & First Project

Слайд 6

File >> New Project

Слайд 7

File >> New Project

Слайд 9

https://developer.android.com/about/dashboards/index.html

Слайд 11

What’s an Activity?

Activity - a component with a screen with which users can

interact in order to do something.

https://developer.android.com/guide/components/activities.html

Слайд 14

API 28

Alchaka
Art of Movement
Breath control
Center of Being
Detoxify Poison
Flashburn
Force Body
Force Enlightenment
Force ghost
Force healing[1]
Force speed
Force

stealth (Also known as Force Concealment)
Hibernation trance
Morichro
Tapas
Cloak of Shadow

Слайд 15

API 21

Alchaka
Art of Movement
Breath control
Center of Being
Detoxify Poison
Flashburn
Force Body
Force Enlightenment
Force ghost
Force healing[1]
Force speed
Force

stealth (Also known as Force Concealment)
Hibernation trance
Morichro
Tapas
Cloak of Shadow

Alchaka
Art of Movement
Breath control
Center of Being
Detoxify Poison
Flashburn
Force Body
Force Enlightenment
Force ghost
Force healing[1]
Force speed
Force stealth (Also known as Force Concealment)
Hibernation trance
Morichro
Tapas
Cloak of Shadow

Слайд 16

Alchaka
Art of Movement
Breath control
Center of Being
Detoxify Poison
Flashburn
Force Body
Force Enlightenment
Force ghost
Force healing[1]
Force speed
Force stealth

(Also known as Force Concealment)
Hibernation trance
Morichro
Tapas
Cloak of Shadow

Alchaka
Art of Movement
Breath control
Center of Being
Detoxify Poison
Flashburn
Force Body
Force Enlightenment
Force ghost
Force healing[1]
Force speed
Force stealth (Also known as Force Concealment)
Hibernation trance
Morichro
Tapas
Cloak of Shadow

Alchaka
Art of Movement
Breath control
Center of Being
Detoxify Poison
Flashburn
Force Body
Force Enlightenment
Force ghost
Force healing[1]
Force speed
Force stealth (Also known as Force Concealment)
Hibernation trance
Morichro
Tapas
Cloak of Shadow

min API 21

Support Library (AppCompat, androidx)

Слайд 18

AndroidManifest.xml
MainActivity.java
activity_main.xml

3 Interesting Things:

Слайд 22

How Does An Activity Look?

Слайд 23

Android Application Structure for now

Application

Activity
A

Activity
B

...

… Manifest ...

Activity
C

https://developer.android.com/guide/topics/manifest/manifest-intro.html

Слайд 24

How to run the app?

Push the tempo!

Слайд 25

How to run the app?

We need to choose a device.

Слайд 26

Option 1: A Real Device

Allow USB Debugging.
It’s in the Developer Options setting, which

might be hidden

Read more:
https://developer.android.com/tools/device.html
https://developer.android.com/studio/debug/dev-options.html

Слайд 27

Android developer in 30 seconds

Android developer

Слайд 28

Confirm popup and run

Слайд 29

Option 2: A Virtual Device
Open the AVD manager and create a virtual device.
The run

dialog will offer you to start it.

Слайд 30

I click “Run”, You say Ho!

Android Studio Builds the project

Слайд 31

*.java

*.xml

images, videos

AndroidManifest.xml

Android Asset Packaging Tool (aapt)

R.java

Java Compiler

Compiled Resources

*.class

*.dex

Dex
Compiler

APK Builder

foo.apk

Слайд 32

What does it take to build the project?

But Gradle helps us with it.

https://developer.android.com/studio/build/


Слайд 33

I click “Run”, You say Ho!

Android Studio Builds the project

http://developer.android.com/tools/help/adb.html

Слайд 34

How is it run?

Android looks through the application’s manifest

Finds an Activity with special

“marker”

Launches it

Слайд 35

It’s ALIVE!

Слайд 36

Build config

Project’s build.gradle
App’s build.gradle

Слайд 39

Any Questions?

Слайд 40

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 41

A Little Bit On Layouts

Слайд 42

Views

Everything in that XML file is a View.
A View:
Knows to draw itself
Used for

user interaction
Has (at least) width and height (match_parent / wrap_content/fixed)

Слайд 43

ViewGroup (Layout)

A special kind of view.
Knows to position its children views on the

screen.

Слайд 44

LinearLayout

Lays items in a row or a column.
Can also divide existing space by

weight.

https://developer.android.com/guide/topics/ui/layout/linear.html

Слайд 45

How does the layout look?

Слайд 48

Any Questions?

Слайд 49

BTC Miner

Слайд 52

activity_main.xml

android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>

Слайд 53

LinearLayout orientation

Слайд 54

activity_main.xml

android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>

Слайд 55

**making things prettier**

Слайд 56

Change Text Size (editor)

Слайд 57

Change Text Size (code)

...
android:textSize="70sp" />
...
android:textSize="70sp" />

Слайд 58

Save Size

We have set up the value 70sp twice!
Repetition in code?? Not for

me!

Слайд 59

Save Size in dimens.xml


70sp

Слайд 60

dimens.xml

Слайд 61

Use Size From dimen.xml

...
android:textSize="@dimen/btc_text_size"/>

Слайд 62

Saving String Resources

...
android:text="ADD BTC"/>

Слайд 63

Saving String Resources

...
android:text="@string/add_btc"/>

Слайд 64

Saving String on strings.xml


BTC Miner
ADD BTC
0

Слайд 65

strings.xml

Слайд 66

Resources

Layouts
Drawable (images, vectors, shapes)
Animations
Colors
Themes & Styles
Booleans / Integers / Dimens /…
Other XMLs
Raw (everything

else)

https://developer.android.com/guide/topics/resources/providing-resources

Слайд 67

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 68

BTC Miner

Does nothing...

Слайд 69

Activity

public class MainActivity extends AppCompatActivity {
}

Слайд 70

Activity

public class MainActivity extends AppCompatActivity {
}

Слайд 71

Activity

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}
}

Слайд 72

Activity

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}
}

Слайд 73

setContentView

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
}
}

Слайд 74

Let’s make it mine!

Access Views declared in XML from Java code
Change text in

TextView
Set onClickListener for the Button
Increment BTC counter & update text

Слайд 75

Reference to the view on code

public class MainActivity extends AppCompatActivity {
private TextView

btcText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btcText = findViewById(/*...*/);
}
}

Слайд 76

Assign view ID

...
android:id="@+id/btc_text"/>

Слайд 77

Reference to the view on code

public class MainActivity extends AppCompatActivity {
private TextView

btcText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btcText = findViewById(R.id.btc_text);
}
}

Слайд 78

*.java

*.xml

images, videos

AndroidManifest.xml

Android Asset Packaging Tool (aapt)

R.java

Java Compiler

Compiled Resources

*.class

*.dex

Dex
Compiler

APK Builder

foo.apk

Слайд 79

Mighty (Tho)R

Autogenerated
Each resource type becomes a static nested class
Each resource becomes an int

ID with the same name

Слайд 80

Change text in TextView

btcText = findViewById(R.id.btc_text);
btcText.setText("Hello World!");

Слайд 81

Change text in TextView

btcText = findViewById(R.id.btc_text);
btcText.setText("Hello World!");

Слайд 82

Change text in TextView

btcText = findViewById(R.id.btc_text);
btcText.setText(R.string.initial_btc);

Слайд 83

Find a button and setOnClickListener

Button addBtcButton = findViewById(R.id.add_btc_button);
addBtcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void

onClick(View v) {
// TODO : update BTC count
}
});

Слайд 84

Find a button and setOnClickListener

Button addBtcButton = findViewById(R.id.add_btc_button);
addBtcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void

onClick(View v) {
// TODO : update BTC count
}
});

Слайд 85

Let’s make a BTC counter!

We’ll need a BTC counter variable. Let’s add an

int and set it to 0.
public class MainActivity extends AppCompatActivity {
private int btcCounter = 0;
//...
}

Слайд 86

Setting onClickListener

addBtcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btcCounter++;
String btc

= String.valueOf(btcCounter);
btcText.setText(btc);
}
});

Слайд 87

Not xml?

android:id="@+id/add_btc_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="onButtonClick"/>
public void onButtonClick(View view) {
}

Слайд 88

OMG, It looks great!

Слайд 89

Questions?

Слайд 90

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 91

Rotate the device

Слайд 92

WHERE ARE...

MY BTC?????...

Слайд 93

Is it a bug or feature?

Слайд 94

Rotate the device

Слайд 95

Create a different layout for landscape

Слайд 96

activity_main.xml (#2)


Слайд 98

Now: Rotate the device

Слайд 99

setContentView

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
}
}

Слайд 101

First android device

HTC Dream
320x480px (2:3)
180 ppi
3.2” (81mm)

http://en.wikipedia.org/wiki/HTC_Dream

Слайд 102

Current Android Device

Pixel 2 XL
2880x1440px (18:9)
538 ppi
6.0” (152mm)

https://en.wikipedia.org/wiki/Pixel_2

Слайд 103

Big Android Device

Nexus 10 (Tablet)
2560x1600px
300ppi
10.1”

http://en.wikipedia.org/wiki/Nexus_10

Слайд 104

Small Android Device

LG G Watch
Most
Same techniques apply

http://en.wikipedia.org/wiki/Android_Wear

Слайд 105

The difference is Big

Слайд 106

Differences

Pixel Density
Screen size
Orientation
OS version
Locale
Device type
...

https://developer.android.com/guide/topics/manifest/activity-element#config https://developer.android.com/guide/topics/resources/providing-resources#AlternativeResources

Слайд 108

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 109

We need more Activities!

Слайд 110

Creating another Activity

Using Android Studio, use Alt + Insert -or- Command + N

to create something new. This shortcut is also context aware.
Let’s create a new activity and call it SecondActivity.

https://developer.android.com/training/basics/firstapp/starting-activity.html

Слайд 111

File >> New Project

Слайд 112

File >> New Project

Слайд 113

You can’t just open an activity on your own - Android does it

for you.
And this is done by using an Intent.

How to open SecondActivity

Слайд 114

Intents have...

used for Explicit Intents

used for Implicit Intents

used to tell things to the

recipient

used to tell things to the messenger

Component Name
Action
Data (and Type)
Category
Extras
Flags

Слайд 115

How to open SecondActivity

MainActivity.java:
public void openSecondActivity() {
Intent secondActivityIntent = new Intent(this,

SecondActivity.class);
startActivity(secondActivityIntent);
}

Слайд 116

Invoke opening SecondActivity

Let’s open it on every 5th BTC mined!
addBtcButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// ...
if (btcCounter % 5 == 0) {
openSecondActivity();
}
}
});

Слайд 117

Intents have...

used for Explicit Intents

used for Implicit Intents

used to tell things to the

recipient

used to tell things to the messenger

Component Name
Action
Data (and Type)
Category
Extras
Flags

Слайд 118

MainActivity.java:
private void openSecondActivity() {
Intent secondActivityIntent = new Intent(this, SecondActivity.class);
secondActivityIntent.putExtra("KEY_BTC", btcCounter);
startActivity(secondActivityIntent);
}

Pass

arguments to next activity

Слайд 119

Pass arguments to next activity

SecondActivity.java:
public static final String KEY_BTC = "KEY_BTC";
MainActivity.java:
private void openSecondActivity()

{
Intent secondActivityIntent = new Intent(this, SecondActivity.class);
secondActivityIntent.putExtra(KEY_BTC, btcCounter);
startActivity(secondActivityIntent);
}

Слайд 120

Get the data passed by the caller

See that the data passed
@Override
protected void onCreate(Bundle

savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
int btcCount = getIntent().getIntExtra(KEY_BTC, 0);
}

Слайд 121

activity_second.xml

The whole Activity is just a TextView with ID:

android:id="@+id/status_text"

android:layout_width="match_parent"
android:layout_height="match_parent" />

Слайд 122

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
int btcCount = getIntent().getIntExtra(KEY_BTC, 0);
}

Use the

data in UI

Слайд 123

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
int btcCount = getIntent().getIntExtra(KEY_BTC, 0);
TextView

statusText = findViewById(R.id.status_text);
statusText.setText(getString(R.string.btc_status, btcCounter));
}

Use the data in UI

Слайд 124

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
int btcCount = getIntent().getIntExtra(KEY_BTC, 0);
TextView

statusText = findViewById(R.id.status_text);
statusText.setText(getString(R.string.btc_status, btcCounter));
}

Use the data in UI

Слайд 125

String with format


BTC Miner
ADD BTC
0
Wow!

You already have %d BTC

Слайд 126

SecondActivity

Слайд 127

Best Practice: Start Activity Statically

SecondActivity.java:
private static final String KEY_BTC = "KEY_BTC";
public static

void start(Activity activity, int btcCounter) {
Intent intent = new Intent(activity, SecondActivity.class);
intent.putExtra(KEY_BTC, btcCounter);
activity.startActivity(intent);
}

Слайд 128

Best Practice: Start Activity Statically

SecondActivity.java:
private static final String KEY_BTC = "KEY_BTC";
public static

void start(Activity activity, int btcCounter) {
Intent intent = new Intent(activity, SecondActivity.class);
intent.putExtra(KEY_BTC, btcCounter);
activity.startActivity(intent);
}

Слайд 129

Best Practice: Start Activity Statically

MainActivity.java:
public void openSecondActivity() {
SecondActivity.start(this, btcCounter);
}

Слайд 130

Any Questions?

Слайд 131

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 132

Activity Lifecycle

Слайд 133

The Activity Lifecycle

https://developer.android.com/guide/components/activities/activity-lifecycle

Слайд 134

The Activity Lifecycle

When activity lifecycle state changes,
Android lets us know with a callback:

onCreate() -> onDestroy()
onStart() -> onStop().
onResume() -> onPause().

Слайд 135

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 137

Logging

Definition: A logfile is a file that records either events that occur in

an operating system or other software.
Code: Log.i(TAG, "Ahtung");

Слайд 138

Log level
Verbose Log.v()
Debug Log.d()
Info Log.i()
Warn Log.w()
Error Log.e()

Слайд 139

Log level
Verbose Log.v()
Debug Log.d()
Info Log.i()
Warn Log.w()
Error Log.e()
Fatal Log.wtf()

Слайд 141

private static final String TAG = "MainActivity";
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected

void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}

Log lifecycle

Слайд 142

private static final String TAG = "MainActivity";
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected

void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}

Log lifecycle

Слайд 143

Any Questions?

Слайд 144

Agenda

Android Studio & First Project
Activity & Layouts
Resources
Java & XML works together
Different configurations
Intents
Activity Lifecycle
Logs

Слайд 145

Recap

What’s an Activity? Describe Activity Lifecycle methods.
What’s the Manifest?
Which resource types do we

have?
What are View, ViewGroup and LinearLayout?
Which class helps us to open another Activity? How to pass data between Activities?
Which types of Intents do we have? What’s the difference?
How to print information to Android log file?

Слайд 146

git - getting today’s code

If you want to grab the code for this

lecture, you should clone this GitHub repository:
https://bit.ly/2x2mqHy

Слайд 147

And now….Exercise!
https://goo.gl/phRktb

Слайд 148

Fundamentals #2: Views

Имя файла: Your-First-Android-Project!.pptx
Количество просмотров: 53
Количество скачиваний: 0