Don’t Panic! Basic Coding — Android #4

It’s time to start learning about how to actually “code” but don’t panic, we’ll start small. Note that the start of this post is almost exactly the same as the iOS version, but then it diverges into Java loveliness / madness.

What exactly is code?

Code is the “programming language” you use in your apps to specify certain things. Things like — what happens when a button is pressed, what happens when an image is swiped to the left, what colour text to use, which font to use, how long it takes to do something … and much much more.

Why can’t we just use English?

Interesting question — as yet no-one has (to my knowledge) created a system where you can just use natural language to build apps. Each app platform / system / operating system has a programming language that’s been created to allow you to build apps using a specific set of commands. For Android the standard language used is Java. For iOS it’s Objective-C or Swift.

So, I need to learn a new language?

Sort of. A programming language isn’t like a natural language, like Japanese or Irish; it’s really a set of commands, structures and concepts that can be used to do particular tasks.

Like a natural language however, you don’t need to learn it all to begin to use use it. A child can say “I don’t want my dinner” years before she can say “Once matter passes a black hole’s event horizon, it won’t escape, not even light”.

Likewise, you can make basic iOS apps without becoming completely proficient in Java. Over time, with more practice and learning, you can become more “fluent” in the language and create more complex apps.

Really, not even light will escape?

Nevermind that, focus! But yes …

Ok, back to the coding

We’ll go through some of the basics, then put them into action in Android Studio to give them a bit more context.

Coding is generally made up of these basic blocks:

  • Classes
  • Variables
  • Functions

There are way more things apart from those, but they’ll do for now.

Classes / Objects

A lot of programming languages, including Java are “Object-Orientated” — this means it’s all about Objects; or as they’re otherwise called, Classes.

A Class is basically a way of describing a “thing” or an “object” — a thing that is in the app; a thing you can use or do something with or to. For example, each “Screen” in an Android app is a “Activity” Class that can do various things — like displaying content like text, images or buttons.

Furthermore, each of those content items are themselves Classes; there is a Button Class for buttons, a TextView Class for displaying text etc and many many many (many many) more classes to avail of and use.

“Behind” the screen, there are also Classes to use and manipulate data / content / other objects — such as the “String” class, which is used to store and manipulate natural language e.g. a name or a title.

Most of “learning to code” is learning about these Classes are and how they can be used.

There are two main sets of Classes to learn — the Java ones and the Android SDK ones. “SDK” means Software Development Kit — it’s a set of Classes and Functions (written in Java) that Google (Android) have pre-made to help you make apps.

We’ll go through loads of these Classes through the various tutorials.

What’s the benefit of Objects / Classes?

Classes are a really handy way of doing things in code; they allow the definition of concepts in an understandable manner. What makes this even handier is that you can define your own Classes. If the app is about cars, you can create a Car Class. If it’s about airplane times, you could potentially want to create an Airplane Class and maybe an Airport Class too, along with a Departure Class and an Arrival Class.

Classes also allow for re-use of code. If you have 10 names to display, you just need 10 instances of the one Class, not 10 different ones. If you have 7,853 airplanes, then you just need one Airplane Class and you can reuse it 7853 times.

Variables

Variables are how you use Classes. A variable is an “instance” of a Class, that you can use to store a value and re-use in your code. So:

String myName = "Odysseus";

This is actual Java code to create (or “declare”) a variable, of type (Class) String and to “initialise” it with a value of “Odysseus”. (If you were cool enough to have a name like that. The more you read of this site, the more you’ll realise how much I like Greek Mythology!).

String

is how you specify what Class the variable is

myName

is the name of the variable, and it could be anything, like:

yourName

or

mickeyMouse

would also be fine. Note that Class starts with an upper case letter String and the variable name with a lower case — yourName. This helps us see easily what’s a variable and what’s a Class.

The

=

is putting the value “Odysseus” in the variable myName. It doesn’t mean “myName equals Odysseus”, it means “put the value Odysseus in the variable myName”. Make sense? Of course, once that is done, the two actually do equal! But it’s important to understand that the “ = “ symbol doesn’t mean “equal to”, it means “put in”.

This bit:

;

is a semicolon and must go at the end of the line. It basically says “this line of code ends here”. In iOS / Swift, they’re not needed, but in Java / Android, they are.

So,

String myName = "Odysseus";

is how you declare a variable called myName and store the value Odysseus in it.

You can then use that variable elsewhere — for example, if you include this in your app:

System.out.println("My name: " + myName)

and run it, you’ll see “My name: Odysseus” in the Android Console in Android Studio.

Let’s try it quickly. Create a new app (or use an existing one) and open the MainActivity.java file. You’ll see this piece of code:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

This is a method (more on those later) — a group of code that is run when the activity is created. We can add code to this method and it will also be run when the activity is created.

Add these two lines of code:

String myName = "Odysseus";
System.out.println("My name: " + myName);

after the

setContentView(R.layout.activity_main);

but before the

}

i.e.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String myName = "Odysseus";
System.out.println("My name: " + myName);
}

or in all it’s screenshot glory:

Run the app (hit the little green triangle beside “app” if you haven’t done this before), pick your emulator (create if needed) and click on the Android Monitor at the bottom of the screen.

The Android Monitor prints out messages as the app runs — you can add your own via the

System.out.println(“whatever you want to print out”);

command. You’ll see a whole lot of system messages, then eventually when the app is running, you should see “My name: Odysseus” in the output:

Sweet — we created a String variable, stored “Odysseus” in it, and printed it out to the Android Monitor.

Methods

Methods (also called functions) are groups of code that “do” something. The System.out.println method above is a built-in method that you can use to print out things like the value of a variable to the Android Monitor - this is handy when building apps so you can keep track of what’s going on.

Java has many many built in functions — again, a lot of learning to code is learning what these are and how to use them.

You can (and will) also create your own functions. Here’s a simple function to add four numbers and “return” the result:

public int addFourNumbers(a : int, b: int, c: int, d: int){
return a + b + c + d;
}

The

public

says that this method can be used elsewhere in the Class. The

int

is the “return type” of the method — this means that the method will return a result and it will be of type int - which is used to store numbers.

addFourNumbers

is the name of the method. We could call it anything, but it’s usually good to call it something meaningful! If it’s a method to send an email, call it something like:

sendEmail

If you want to create a method to change the color of a button to pink, maybe call it:

changeButtonToPink

and so on! Ok, back to:

public int addFourNumbers(a : int, b: int, c: int, d: int){
return a + b + c + d;
}

The

(a : int, b: int, c: int, d: int)

is a bit more complicated. a, b, c and d are “parameters”. Parameters are values we can “pass in” to methods for use inside them. Here we we say that there are four parameters in this function, both of Class “int” — which is used to store numbers.

Again a, b, c and d are the names of the parameters, we could have called them anything. We also don’t need parameters if we don’t want. A method without parameters has empty brackets like this:

public int methodWithNoParameters(){
// some amazing code
}

The

{

is known as an “opening bracket” and signifies that the method’s code is about to begin. This code is what “runs” when the function is “called” i.e. “run”.

return a + b + c + d;

adds the four numbers together and “returns” the result. Returns means that this is the result of the function.

The

;

is needed at the end of the statement.

}

is the “closing bracket”. Every opening bracket must have a corresponding closing one. Or all hell breaks loose!

How would you use this amazing function?

int fourNumbersAdded = addFourNumbers(101, 15, 364, 2345);

This creates a variable called “fourNumbersAdded” and stores in it the value the result of our new method, which adds together the passed in parameters — 01, 15, 364 and 2365.

Cool? Confusing? It’s ok, all will become clearer with more examples and time!

Note of course that this method would never actually be needed, as we could just have written:

int fourNumbersAdded = 101 + 15 + 364 + 2365;

But it worked as a simple example I hope! To see it in action in Android Studio copy in the method:

public int addFourNumbers(a : int, b: int, c: int, d: int){
return a + b + c + d;
}

into the MainActivity class, before the final closing bracket i.e.

package com.example.whatever.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public int addFourNumbers(int a, int b, int c, int d){
return a + b + c + d;
}
}

This creates the method, but does not actually use it. To use it we “call” it somewhere else. In the onCreate method, type in:

int result = addFourNumbers(57,34,213,2131);
System.out.println("Result of our method: " + result);

so all the file looks like:

package com.example.whatever.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

int result = addFourNumbers(57,34,213,2131);
System.out.println("Result of our method: " + result);
}

public int addFourNumbers(int a, int b, int c, int d){
return a + b + c + d;
}
}

Now when you run the app, you should see this in the Android Monitor:

One more thing to note before we close out this guide — a method doesn’t need a return type (like int in the example above), it can just “do something” and not return a result. For example:

public void changeMainViewToBlue(){
getWindow().getDecorView().setBackgroundColor(Color.BLUE);
}

changes the color of the Activity background to blue. Again, call it by puttin this in onCreate:

changeMainViewToBlue();

Note that this uses the Color Class — which your MainActivity file doesn’t know about. So you need to “import” it — by adding this line to the top of the file:

import android.graphics.Color;

or with all the rest of the code:

package com.example.whatever.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

int result = addFourNumbers(57,34,213,2131);
System.out.println("Result of our method: " + result);

changeMainViewToBlue();
}

public void changeMainViewToBlue(){
getWindow().getDecorView().setBackgroundColor(Color.BLUE);
}

public int addFourNumbers(int a, int b, int c, int d){
return a + b + c + d;
}
}

When you run the app, the changeMainViewToBlue method is called, and voila:

Boom! We just created a method and used it to change the color of our app to a truly awful shade of blue!

Summary

This post skimmed the tip of the iceberg of coding in Java / Android — discussing Classes, variables and methods — but we all need to start somewhere. We’ll introduce more coding concepts gradually as we go along, so keep an eye out for the upcoming tutorials and guides!

This is part of appsandbiscuits — my new site to help teach app development to people with zero coding experience. If you liked it, please hit the little heart below, it’ll help other people come across the site! Thanks! Andy.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andy O'Sullivan

Andy O'Sullivan

Creator of Boxapopa, the iOS game for young kids with zero ads, just fun! https://apps.apple.com/ie/app/boxapopa/id1550536188