You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: android/introduction/tutorial.md
+15-49Lines changed: 15 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ layout: page
3
3
title: Introduction to Android Development
4
4
---
5
5
6
-
##1. Intro
6
+
##1. Intro
7
7
8
8
The aim of this worksheet is to create a cookie clicker game (http://orteil.dashnet.org/cookieclicker/), with a cookie image that can be tapped to increase the score.
9
9
@@ -14,134 +14,102 @@ arriving at the workshop!)
14
14
15
15
Feel free to chose a different topic other than cookies, we really like the popular pokémon Goomy Clicker - http://joezeng.github.io/goomyclicker what things do you like?
16
16
17
-
18
17

19
-
##2. Create New Project
20
18
19
+
## 2. Create New Project
21
20
22
21

23
-
> Set the name of your app, this is what people will see when they install your app. The company name, and the resulting package name, is how Google keeps track of apps - it needs to be unique, so maybe use something with your name?
24
22
23
+
> Set the name of your app, this is what people will see when they install your app. The company name, and the resulting package name, is how Google keeps track of apps - it needs to be unique, so maybe use something with your name?
25
24
26
25
Next, we set the SDK level to `14` - It'll give you a helpful guide for how many people this supports. Speak to your coach about what minimum sdk level they usually have to use.
27
26
28
-
29
27
Finally, we want to create an `Empty Activity`
30
28
31
-
32
29

33
-
> We are going to keep the default of MainActivity.java and activity_main.xml :smile:
34
30
31
+
> We are going to keep the default of MainActivity.java and activity_main.xml :smile:
35
32
36
33
click FINISH (this might take some time)
37
34
38
-
39
35
Take some time to look at the different buttons with your coach - where are the different files located in the project? How do you run your app in an emulator or on a device?
40
36
41
-
42
-
##3. Layouts
43
-
37
+
## 3. Layouts
44
38
45
39
Next, we want to open our layout file. On the left find the res folder and open layout. You should be able to double click on the activity_main.xml and open it.
46
40
47
-
48
41
Layouts open in 'design' mode. This gives you some tools to make layouts with a graphical interface, but it's more common to write layouts by hand using the text editor. If you're familiar with Dreamweaver, it's a similar “What you see is what you get” (WYSIWYG) vs code situation.
49
42
50
-
51
43
We're going to be dealing with the XML directly in this tutorial, so find the 'text' tab in the bottom left corner of the design window.
52
44
53
-
54
45
Now you should see some code that looks familiar-ish! Android layouts using XML, which is very very similar to HTML. You should see already we have a `<RelativeLayout>` and a `<TextView>` tag, with some attributes already there. The thing that looks weird is that all the attributes currently are prefixed with the word `android`.
55
46
56
-
57
47
One of the most important features of our cookie clicker will be our cookie - we can create an `<ImageView>`, you will need to provide a `height` and a `width`. We also need to provide it an id so we can connect to it later.
58
48
59
-
60
49
```xml
61
50
<ImageView
62
51
android:id="@+id/imgCookie"
63
52
android:layout_width="256dp"
64
53
android:layout_height="256dp"/>
65
54
```
66
55
67
-
68
56
> id’s need to start with @+id/ - the plus symbol means it assigns the variable name to the current ImageView
69
57
70
-
71
58
In Android we don't use pixels, but instead use `dp` which is a device point. 1 point might be 1 pixel on a really low resolution device, but might be 4 pixels, or even 8 pixels on a newer device. Take a look at this handy guide Android wrote for more information https://developer.android.com/guide/practices/screens_support.html .
72
59
73
-
74
60
Next, we want to save the following cookie image into our project. You can either google for a cookie image that's free to use, or use ours that we found earlier = http://imgur.com/a/9BXV4 . You need to save it inside your project folder using Windows Explorer or Mac OSX Finder and navigate to the following -> app -> src -> main -> res . You need to create a folder called `drawable-xhdpi` and place your cookie image in here!
75
61
76
-
77
62
Because android has different density devices, we usually need to provide different resolution images for all those different devices. If we only provide it in one folder, Android will scale the image for other devices, but this might cause make the image look bad!
78
63
79
-
80
64

81
65
82
-
83
66
If we want to then use that image in our Android app we can use an attribute `android:src="@drawable/cookie"` - autocomplete will be your friend here!
84
67
85
-
86
68
Because we have a RelativeLayout parent this means we can do some special things to position our cookie on our screen. We can provide an attribute like `android:layout_centerInParent="true"`.
87
69
88
-
89
70
Next, we want to look at having a TextView for to keep track of how many cookies we've clicked. Again you need to provide it a height, width, and id. But we can also provide it some special label things, such as `text`, `textColor`, or `textSize`
90
71
91
-
92
72
Another neat feature of using a RelativeLayout is how we can position things **in relation** to other things. So we can say that this TextView should `appearAbove` the image's id. In order for this to work, the TextView needs to know where the ImageView is, so the code for the TextView needs to go below the ImageView.
93
73
94
-
95
74

96
-
> this is how our layout code finally looked :smile:
97
75
76
+
> this is how our layout code finally looked :smile:
98
77
99
78
Ask your coach what other special things RelativeLayout can do? Do you know any other alternative parent layouts? Take a look at https://developer.android.com/guide/topics/ui/layout/relative.html for more information.
100
-
##4. Clicking
101
79
80
+
## 4. Clicking
102
81
103
82
Next, we want to make it so our cookie actually does something when it's been clicked on!
104
83
105
-
106
84
We want to open our MainActivity.java file and take a look at what's there already. What do you think the `onCreate` method might do? Speak to your coach about what other methods get called in the activity lifecycle. For more information look at https://developer.android.com/training/basics/activity-lifecycle/index.html .
107
85
108
-
109
86
Inside the `onCreate` method, below where it sets the layout to the xml file we just finished designing. We want to create a variable called imgCookie and findViewById to hook them up. This is similar to how we use jQuery in JavaScript to hook up with a HTML element
After, we can set an onClickListener, which is again similar to the JavaScript element.click() method. The trick with Android Studio is to let it write as much code as possible, it has an extremely powerful auto completer! When you start typing onClickListener, you should see a suggestion with curly brackets on it. If you press tab at this point, it'll auto complete the entire code that you need!
127
99
128
-
129
100

130
101
131
-
132
102
Inside our method we're going to put a Toast. These are those little messages at the bottom of the phone that show for a short period of time. They're really good
133
103
134
-
135
104

136
105
137
-
138
106
Run your app now, and see what happens when you tap on the cookie!
139
-
##5. Game Logic
140
-
Next, we want to make it so that when the image is clicked we increment a score. We need to create a private variable int called currentScore and set initially set it to 0.
141
107
108
+
## 5. Game Logic
142
109
143
-
When we click on the image, we need to increment the score by 1. For now, we can use our toast to see how much our current score is:
110
+
Next, we want to make it so that when the image is clicked we increment a score. We need to create a private variable int called currentScore and set initially set it to 0.
144
111
112
+
When we click on the image, we need to increment the score by 1. For now, we can use our toast to see how much our current score is:
145
113
146
114
```java
147
115
privateint currentScore =0;
@@ -164,30 +132,28 @@ private int currentScore = 0;
164
132
}
165
133
```
166
134
167
-
168
135
Toast messages are super useful for whilst we’re building the app, but people playing probably don’t want that. Let’s go about hooking it up to our TextView.
169
136
170
-
171
137
Firstly, we need to make a variable called lblTotal and set it to our TextView just like how we set up our ImageView.
172
138
173
-
174
139
Next, inside our imgCookie onClick, we can do lblTotal.setText to set our current counter. However we can’t do this directly; our currentScore is an int (number), and the lblTotal can only display a String (text). So we need to do String.valueOf to convert it!
175
140
176
-
177
141

142
+
178
143
> final code
179
144
180
-
#6. More Advanced Game Logic
145
+
## 6. More Advanced Game Logic
181
146
182
147
Now is the time to get creative! What other things would make your game yours. Maybe you could get the cookie picture to change when you get to specific numbers using an if statement, e.g. after 10, 20, 30 … etc. Or how about getting more advanced and using a log scale e.g. after 10, 100, 100 cookies.
183
148
184
-
##7. Extra Credit
149
+
##7. Extra Credit
185
150
186
151
If you want to take your cookie game clicker onwards, maybe consider:
152
+
187
153
* Things people can spend their cookies on to get cookies for them without clicking, such as using timers
188
154
* Use something like firebase to have high scores, or allow people to sign in and save their cookies
189
155
190
-
##8. More Information
156
+
##8. More Information
191
157
192
158
Amal has written a more advanced android tutorial located at https://github.com/K4KYA/MakersHelloAndroid/tree/codebar if you want to take Android even further :)
0 commit comments