Skip to content

Commit abff797

Browse files
committed
Fix markdown formatting because github
1 parent f551891 commit abff797

1 file changed

Lines changed: 15 additions & 49 deletions

File tree

android/introduction/tutorial.md

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: page
33
title: Introduction to Android Development
44
---
55

6-
##1. Intro
6+
## 1. Intro
77

88
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.
99

@@ -14,134 +14,102 @@ arriving at the workshop!)
1414

1515
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?
1616

17-
1817
![Imgur](http://i.imgur.com/ssQF3jO.png)
19-
##2. Create New Project
2018

19+
## 2. Create New Project
2120

2221
![Imgur](http://i.imgur.com/er2g9BZ.png)
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?
2422

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?
2524
2625
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.
2726

28-
2927
Finally, we want to create an `Empty Activity`
3028

31-
3229
![Imgur](http://i.imgur.com/PzqiEXC.png)
33-
> We are going to keep the default of MainActivity.java and activity_main.xml :smile:
3430

31+
> We are going to keep the default of MainActivity.java and activity_main.xml :smile:
3532
3633
click FINISH (this might take some time)
3734

38-
3935
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?
4036

41-
42-
##3. Layouts
43-
37+
## 3. Layouts
4438

4539
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.
4640

47-
4841
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.
4942

50-
5143
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.
5244

53-
5445
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`.
5546

56-
5747
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.
5848

59-
6049
```xml
6150
<ImageView
6251
android:id="@+id/imgCookie"
6352
android:layout_width="256dp"
6453
android:layout_height="256dp"/>
6554
```
6655

67-
6856
> id’s need to start with @+id/ - the plus symbol means it assigns the variable name to the current ImageView
6957
70-
7158
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 .
7259

73-
7460
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!
7561

76-
7762
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!
7863

79-
8064
![Imgur](http://i.imgur.com/dIMVebY.png)
8165

82-
8366
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!
8467

85-
8668
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"`.
8769

88-
8970
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`
9071

91-
9272
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.
9373

94-
9574
![Imgur](http://i.imgur.com/Dq3RsRU.png)
96-
> this is how our layout code finally looked :smile:
9775

76+
> this is how our layout code finally looked :smile:
9877
9978
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
10179

80+
## 4. Clicking
10281

10382
Next, we want to make it so our cookie actually does something when it's been clicked on!
10483

105-
10684
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 .
10785

108-
10986
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
11087

111-
11288
```java
11389
@Override
11490
protected void onCreate(Bundle savedInstanceState) {
11591
super.onCreate(savedInstanceState);
11692
setContentView(R.layout.activity_main);
11793

118-
11994
ImageView imgCookie = (ImageView)findViewById(R.id.imgCookie);
120-
121-
12295
}
12396
```
12497

125-
12698
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!
12799

128-
129100
![Imgur](http://i.imgur.com/IhKTxKv.png)
130101

131-
132102
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
133103

134-
135104
![Imgur](http://i.imgur.com/K5q1Ftr.png)
136105

137-
138106
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.
141107

108+
## 5. Game Logic
142109

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.
144111

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:
145113

146114
```java
147115
private int currentScore = 0;
@@ -164,30 +132,28 @@ private int currentScore = 0;
164132
}
165133
```
166134

167-
168135
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.
169136

170-
171137
Firstly, we need to make a variable called lblTotal and set it to our TextView just like how we set up our ImageView.
172138

173-
174139
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!
175140

176-
177141
![Imgur](http://i.imgur.com/qCoy89R.png)
142+
178143
> final code
179144
180-
#6. More Advanced Game Logic
145+
## 6. More Advanced Game Logic
181146

182147
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.
183148

184-
##7. Extra Credit
149+
## 7. Extra Credit
185150

186151
If you want to take your cookie game clicker onwards, maybe consider:
152+
187153
* Things people can spend their cookies on to get cookies for them without clicking, such as using timers
188154
* Use something like firebase to have high scores, or allow people to sign in and save their cookies
189155

190-
##8. More Information
156+
## 8. More Information
191157

192158
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 :)
193159

0 commit comments

Comments
 (0)