38.28M
Categories: softwaresoftware electronicselectronics

Your First Android Project!

1.

Your First Android Project!
Sergey Ryabov
11.09.2018
1

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
Git are
are ready
ready to
to use
use
Android
Studio, Kotlin,
Checked-in on Eventbrite
Joined slack (android-academy-msk)
https://bit.ly/2NIR0x0

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

8.

9.

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

10.

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

12.

13.

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

16.

Support Library
(AppCompat,
androidx)
min 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

17.

18.

3 Interesting Things:
1. AndroidManifest.xml
2. MainActivity.java
3. activity_main.xml

19.

20.

21.

22.

How Does An Activity Look?

23.

Android Application Structure
Application
Activity
A
Activity
B
for now
… 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
WOW!
7 times
You are now a 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!
1. Android Studio Builds the project

31.

images, videos
AndroidManifest.xml
*.class
R.java
Compiled
Resources
Dex
Compiler
*.dex
APK Builder
*.xml
Java Compiler
Android Asset Packaging Tool (aapt)
*.java
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!
1. Android Studio Builds the project
2. Loads it to a device (real or virtual)
3. Runs it on the device,
adb
attaches a debugger if needed
http://developer.android.com/tools/help/adb.html
adb install YourApp.apk
shell am start -n com.examp.name...

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

37.

38.

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?

46.

47.

2
1
3

48.

Any Questions?

49.

BTC Miner

50.

51.

52.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>

53.

LinearLayout orientation
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>

54.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>

55.

**making things prettier**

56.

Change Text Size (editor)

57.

Change Text Size (code)
<TextView
...
android:textSize="70sp" />
<Button
...
android:textSize="70sp" />

58.

Save Size
We have set up the value 70sp twice!
Repetition in code?? Not for me!
ALT+Enter

59.

Save Size in dimens.xml
<resources>
<dimen name="btc_text_size">70sp</dimen>
</resources>

60.

dimens.xml

61.

Use Size From dimen.xml
<TextView
...
android:textSize="@dimen/btc_text_size"/>

62.

Saving String Resources
<Button
...
android:text="ADD BTC"/>

63.

Saving String Resources
<Button
...
android:text="@string/add_btc"/>

64.

Saving String on strings.xml
<resources>
<string name="app_name">BTC Miner</string>
<string name="add_btc">ADD BTC</string>
<string name="initial_btc">0</string>
</resources>

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/

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!
1. Access Views declared in XML from Java code
2. Change text in TextView
3. Set onClickListener for the Button
4. 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
<TextView
...
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.

images, videos
AndroidManifest.xml
*.class
R.java
Compiled
Resources
Dex
Compiler
*.dex
APK Builder
*.xml
Java Compiler
Android Asset Packaging Tool (aapt)
*.java
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?
<Button
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)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>

97.

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);
}
}

100.

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

107.

Questions ?

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.

How to open SecondActivity
You can’t just open an activity on your own Android does it for you.
And this is done by using an Intent.

114.

Intents have...
Component Name
used for Explicit Intents
Action
Data (and Type)
used for Implicit Intents
Category
Extras
Flags
used to tell things to the recipient
used to tell things to the messenger

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...
Component Name
used for Explicit Intents
Action
Data (and Type)
used for Implicit Intents
Category
Extras
Flags
used to tell things to the recipient
used to tell things to the messenger

118.

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

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:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/status_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />

122.

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

123.

Use the data in UI
@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));
}

124.

Use the data in UI
@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));
}

125.

String with format
<resources>
<string name="app_name">BTC Miner</string>
<string name="add_btc">ADD BTC</string>
<string name="initial_btc">0</string>
<string name="btc_status">Wow! You already have %d BTC</string>
</resources>

126.

SecondActivity
Wow! You already have 15 BTC

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

136.

Logging

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
Debug
Info
Warn
Error
Log.v()
Log.d()
Log.i()
Log.w()
Log.e()

139.

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

140.

Logging

141.

Log lifecycle
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");
}

142.

Log lifecycle
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");
}

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
19.09 19:00
Avito

149.

Don’t go home!
Networking!
English     Русский Rules