1 of 110

Hello world – First project

#1

09/09/2019

Vasya Drobushkov

1

WiFi: space/istheplace

2 of 110

2

Vasya Drobushkov

Android Developer @ Ciklum

@krossovochkin

3 of 110

3

4 of 110

4

5 of 110

5

6 of 110

android_broadcast

News for Android Devs

7 of 110

Mentors

7

8 of 110

Checklist

  • Android Studio, Git are ready to use
  • RSVP on Eventbrite
  • Joined Telegram (androidacademyminsk)

bit.ly/2kvGWhj

8

9 of 110

9

Questions ?

10 of 110

10

3 Questions

1 Winner

11 of 110

11

https://kahoot.it

12 of 110

Agenda

  • Hello World - First project
  • Android project structure
  • Introduction to Activities and Intents
  • Android Studio interface overview

12

13 of 110

Hello World – First Project

13

14 of 110

14

15 of 110

15

16 of 110

What is Activity?

16

17 of 110

17

18 of 110

18

19 of 110

19

20 of 110

20

21 of 110

21

- Run

22 of 110

22

23 of 110

23

24 of 110

24

25 of 110

25

26 of 110

26

27 of 110

27

!

28 of 110

Real Device

28

Click 7 times

29 of 110

29

- Run

30 of 110

30

31 of 110

31

Questions ?

32 of 110

Android Project Structure

32

33 of 110

33

34 of 110

34

35 of 110

35

AndroidManifest.xml

36 of 110

36

37 of 110

37

java

- MainActivity.kt

38 of 110

38

39 of 110

39

res

- drawable

- layout

- activity_main.xml

- mipmap

- values

40 of 110

40

41 of 110

41

AndroidManifest.xml

MainActivity.kt

activity_main.xml

42 of 110

42

Questions ?

43 of 110

43

44 of 110

44

1

2

3

45 of 110

45

46 of 110

46

47 of 110

47

48 of 110

48

49 of 110

49

50 of 110

50

ALT + ENTER

51 of 110

51

52 of 110

52

53 of 110

53

54 of 110

54

55 of 110

Resource Qualifiers

  • Language (-ru)
  • Screen orientation (-land)
  • API Version (-v23)
  • And more…

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

55

56 of 110

56

Questions ?

57 of 110

57

58 of 110

58

59 of 110

59

60 of 110

60

61 of 110

61

62 of 110

62

63 of 110

63

64 of 110

64

65 of 110

65

Questions ?

66 of 110

66

build.gradle

app/build.gradle

67 of 110

67

68 of 110

68

69 of 110

69

Questions ?

70 of 110

Activity lifecycle

70

71 of 110

71

72 of 110

72

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

73 of 110

73

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

74 of 110

74

Questions ?

75 of 110

Create another Activity

75

76 of 110

76

Creating another activity

77 of 110

77

78 of 110

78

79 of 110

Accessing View from code

79

80 of 110

80

81 of 110

81

82 of 110

Starting another Activity

82

83 of 110

83

84 of 110

Intents have...

Component Name

Action

Data (and Type)

Category

Extras

Flags

84

used for Explicit Intents

used for Implicit Intents

used to tell things to the recipient

used to tell things to the messenger

85 of 110

Starting another activity

85

Activity A

Intent (Activity B)

Extras

Intent (Activity B)

Extras

Intent (Activity B)

Extras

Activity B

Intent

Extras

86 of 110

Explicit intent

86

private fun openSecondActivity() {

val intent = Intent(this, SecondActivity::class.java)

startActivity(intent)

}

MainActivity.kt

87 of 110

87

Questions ?

88 of 110

Explicit intent

88

private fun openSecondActivity() {

val intent = Intent(this, SecondActivity::class.java)

intent.putExtra(KEY_EXTRA_BOOLEAN, true)

intent.putExtra(KEY_EXTRA_INT, 1)

intent.putExtra(KEY_EXTRA_STRING, "text")

startActivity(intent)

}

MainActivity.kt

89 of 110

Intent Get Extras

89

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_second)

val boolean = intent.getBooleanExtra(KEY_BOOLEAN_EXTRA, false)

}

SecondActivity.kt

90 of 110

Implicit Intent

fun openUrl(url: String) {� val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))� startActivity(intent)�}

90

91 of 110

91

Questions ?

92 of 110

Starting another activity for result

92

Activity A

Intent (Activity B)

Extras

Intent (Activity B)

Extras

Intent (Activity B)

Extras

Activity B

Intent

Extras

Working....

setResult(

RESULT_OK,

Data

)

finish()

finish()

finish()

onActivityResult(...) {

RESULT_OK + Data

RESULT_OK + Data

RESULT_OK + Data

93 of 110

startForResult

93

val intent = Intent(this, SecondActivity::class.java)

intent.putExtra(KEY_EXTRA_BOOLEAN, true)

intent.putExtra(KEY_EXTRA_INT, 1)

intent.putExtra(KEY_EXTRA_STRING, "text")

startActivityForResult(intent, REQUEST_CODE_SECOND_ACTIVITY)

MainActivity.kt

94 of 110

startForResult Return Result

94

setResult(RESULT_OK, intent)

finish()

SecondActivity.kt

95 of 110

startActivityForResult Handle Result

95

override fun onActivityResult(

requestCode: Int,

resultCode: Int,

data: Intent?

) {

if (requestCode == REQUEST_CODE_SECOND_ACTIVITY) {

if (resultCode == RESULT_OK) {

val boolean = data?.getBooleanExtra(KEY_EXTRA_BOOLEAN, false)

}

} else {

super.onActivityResult(requestCode, resultCode, data)

}

}

MainActivity.kt

96 of 110

96

Questions ?

97 of 110

Android Studio interface overview

97

98 of 110

98

99 of 110

99

100 of 110

Logging

  • Error: Log.e(...)
  • Warning: Log.w(...)
  • Info: Log.i(...)
  • Debug: Log.d(...)
  • Verbose: Log.v(...)

100

101 of 110

Log Activity Lifecycle

override fun onResume() {� super.onResume()� Log.i(TAG, "onResume")�}��override fun onPause() {� super.onPause()� Log.i(TAG, "onPause")

}��companion object {� private const val TAG = "MainActivity"�}

101

102 of 110

102

103 of 110

103

104 of 110

104

Build project

Run configuration

Target Device

Run app (Build and install)

Apply Changes

Sync Project with Gradle

Run with debugger attached

Attach Debugger to running app

105 of 110

105

106 of 110

106

107 of 110

107

Questions ?

108 of 110

108

109 of 110

109

Next Lecture #2 – 16.09 18:30

110 of 110

110

Don’t go home!

Networking!