Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions android/1introduction/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ title: Introduction to Android Development

## 1. Intro

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

To follow this tutorial, you will need to [install Android
Studio](https://developer.android.com/studio/index.html) (please
note: this is a large file, so our advice is to download and install it before
arriving at the workshop!)

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

![Imgur](http://i.imgur.com/ssQF3jO.png)

Expand All @@ -24,15 +24,15 @@ Feel free to chose a different topic other than cookies, we really like the popu

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.

Finally, we want to create an `Empty Activity`
Finally, we want to create an `Empty Activity`

![Imgur](http://i.imgur.com/PzqiEXC.png)

> We are going to keep the default of MainActivity.java and activity_main.xml :smile:

click FINISH (this might take some time)

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

## 3. Layouts

Expand All @@ -44,7 +44,7 @@ We're going to be dealing with the XML directly in this tutorial, so find the 't

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

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

```xml
<ImageView
Expand All @@ -55,17 +55,17 @@ One of the most important features of our cookie clicker will be our cookie - we

> id’s need to start with @+id/ - the plus symbol means it assigns the variable name to the current ImageView

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 .
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 official guide](https://developer.android.com/guide/practices/screens_support.html) for more information.

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!
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 [our example image](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!

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!

![Imgur](http://i.imgur.com/dIMVebY.png)

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!

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"`.
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"`.

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`

Expand All @@ -75,13 +75,13 @@ Another neat feature of using a RelativeLayout is how we can position things **i

> this is how our layout code finally looked :smile:

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.
Ask your coach what other special things RelativeLayout can do? Do you know any other alternative parent layouts? Take a look at the [official guide](https://developer.android.com/guide/topics/ui/layout/relative.html) for more information.

## 4. Clicking

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

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 .
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 the [official training guide](https://developer.android.com/training/basics/activity-lifecycle/index.html).

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

Expand All @@ -99,11 +99,11 @@ After, we can set an onClickListener, which is again similar to the JavaScript e

![Imgur](http://i.imgur.com/IhKTxKv.png)

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

![Imgur](http://i.imgur.com/K5q1Ftr.png)

Run your app now, and see what happens when you tap on the cookie!
Run your app now, and see what happens when you tap on the cookie!

## 5. Game Logic

Expand Down Expand Up @@ -155,8 +155,8 @@ If you want to take your cookie game clicker onwards, maybe consider:

## 8. More Information

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 :)
Amal has written a more [advanced android tutorial](https://github.com/K4KYA/MakersHelloAndroid/tree/codebar) if you want to take Android even further :)

If you’d like to go even further, the Big Nerd Ranch have a really very good book to pick up Android (they have an iOS book too) - https://www.bignerdranch.com/we-write/
If you’d like to go even further, the Big Nerd Ranch have a [really very good book](https://www.bignerdranch.com/we-write) to pick up Android (they have an iOS book too).

Google also has their own tutorial series at https://developer.android.com/training/basics/firstapp/index.html, which touches on some of the details and complexities of Android app development - saving data, sharing things with other apps, getting location data and other things you may want a more complex app to be able to do.
Google also has their own [tutorial series](https://developer.android.com/training/basics/firstapp/index.html), which touches on some of the details and complexities of Android app development - saving data, sharing things with other apps, getting location data and other things you may want a more complex app to be able to do.