Learning java/Android - my epic journey

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Introduction

I have always liked programming. I wrote my first programs at age 10 i Basic on my Commodore 128, and wanted to earn money as a programmer. However, life is what happens to you when you are busy making other plans, and a never learned more than the basics.
Now, 20 years (how time flies!) later, I see a lot of potential in Android apps, and I have decided to learn it. This is purely a spare time effort, so it will not be a fast process.

I have decided to post my experiences here. I will post my progress, my tips, my rants, and pleas for help. You are all encouraged to help and comment. It would perhaps be more obvious to post this on a developer board, or maybe as a blog. However, I like Anandtech and have frequented the site for a long time.

So here goes. First is a list of chapters:

01 - Introduction
02 - Where to start
03 - Hello World, Part 1
04 - Hello World, Part 2
 
Last edited:

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Where to start?

I had no idea where to start with this, so I google "Android development".

developer.android.com

D'oh. Oh well, that was easy. I can recommand this site. SDK, tutorials, reference, articles.

So, I browsed a little, and found a Hello World tutorial.

http://developer.android.com/resources/tutorials/hello-world.html

I decided to follow this, and downloaded the SDK. While downloading and installing, I decided what my first goal would be: Creating a daughter-app. You see, I have a baby girl, and I decided to make her the focus of a simple app. The app would contain 3 pictures of her, which the user (my wife) could swipe between. Clicking each picture would do 3 different things: one would launch the wife's blog, one would play a small video of my daughter, and one would play a clip of You're Beautiful which my daughter enjoys.

Coming up: Hello World.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Good idea. Having gone through the environment and platform setup (on Ubuntu) and done a little "Hello World" stuff myself, I'll be reading this with interest.
 

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Hello World, Part 1

The wife is watching horrible reality shows, the baby is sleeping. Here we go:

Eclipse is quite cluttered at first glance. The tutorial guides you how to create a project with screenshots and all. Nice. Click, click.

Hmm, namespace. That sounds important. What it is, exactly? Lets ask Wikipedia:

http://en.wikipedia.org/wiki/Namespace_(computer_science)
- there is a Java section.
If I understand correctly, I might as well create a private namespace for myself. Offkey Development (long story).

Am I going off a tangent here? Correct me if I am wrong.

Back to the tutorial. Damn. The tutorial is outdated - the new version of Eclipse is different. Oh, well, you live and learn. Wonder what happens if I click next? Ah, that seems more like it. Version 2.1 pleas... oh.

Both a Google Inc version and an Open Source version. Open source is something with free beer, IIRC. Sounds good. Next.
Hmm, package name, namespace. I need to read up on this. Onwards. Finish.

Er.. What now? Eclipse has created a package which expands into a tree. Its all Greek to me at the moment, and I don't speak Greek. The tutorial tells me to open HelloAndroid > src > com.example.helloandroid (which I changed to com.example.offkeydevelopment). Oooh! Java code.

I can already see that my laptop's 1920x1080 diplay is my friend in this Epic Journey.

A bunch of automatically generated code lines. I want to understand this. Here they all are:

Code:
package com.example.offkeydevelopment;

import android.app.Activity;
import android.os.Bundle;

public class 

HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public 

void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView

(R.layout.main);
    }
}

I will go through them one by one, and write what I think each of them does. If I make mistakes, please correct me.

Code:
package com.example.offkeydevelopment;
Package name again. What does it really mean?

Code:
import android.app.Activity;
import android.os.Bundle;
Here we import the needed classes. Get it. Is classes the right word?

Code:
public class HelloAndroid
Name of the class. What is the diffence between public and private here?

Code:
extends Activity {
Android splits applications into Activities, right?

Code:
@Override
Override? Is this some sort of message to the OS about interrupts?

Code:
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
Now I am struggling. What does this do? And what does public void mean? I have seen it before. Wikipedia?

Google? Hmm, OK, it has to do with how the application communicates with other applications, right? Or wrong?
Does someone have a link which explains this in an easy to understand way?

Code:
setContentView(R.layout.main);
Set content view. At least I get that :)

Whew. My head hurts, and I have not even started coding yet. Oh well, the horrible reality show is still on, and on screen right now is one of the ugliest women I have ever seen.
More code! Or really, my very first line:

Code:
import android.widget.TextView;
I need to import a package, and this line does it for me. OK. Eclipse has a nice autocomplete. It already like it and want to be friends with it.

Code:
TextView tv = new TextView(this);
TextView is a subclass that handles views. Here, I create a TextView called tv and gives (this) as context reference. I can use (this) because Activity inherits from Context, and my class is a subclass of Activity.
I think I mostly get it.

Code:
tv.setText("Hello, Android");
Here I define the content of tv.

Code:
setContentView(tv);
Here, I tell setContentView, which displays th UI, to use tv.

Let's run it. Run - Run. Hello self. It works. Hooray :) So, I should be able to simply change the content of my tv.setText statement. Yep.

That went rather well, all in all. Next time, I dive into xml. Today, I hardcoded the UI. Not nice, according to the tutorial, it can be defined in XML instead.
 

Broheim

Diamond Member
Feb 17, 2011
4,587
3
81
Hello World, Part 1

Code:
package com.example.offkeydevelopment;
Package name again. What does it really mean?
a package is basically just a collection of classes/interfaces/files/etc. that serves a common purpose in your program, I don't want to confuse you with a lot of technical speak right now so instead just know that packages will make your life easier once you delve into bigger projects

Code:
import android.app.Activity;
import android.os.Bundle;
Here we import the needed classes. Get it. Is classes the right word?

yes, it's best practice that classes (and constructors) start with a capital letter so you can easily identify if it's a class or not

Code:
public class HelloAndroid
Name of the class. What is the diffence between public and private here?
public and private has to do with the scope of something, meaning where can it be used or who is it available to
Code:
extends Activity {
Android splits applications into Activities, right?
that's my understanding of it too

Code:
@Override
Override? Is this some sort of message to the OS about interrupts?
no it's a message to the compiler, don't worry about it for now

Code:
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
Now I am struggling. What does this do? And what does public void mean? I have seen it before. Wikipedia?

public again has to do with the method scope and void means that the method doesn't return anything, for instance had it said public int onCreate() then it would have to return an integer

Google? Hmm, OK, it has to do with how the application communicates with other applications, right? Or wrong?
Does someone have a link which explains this in an easy to understand way?

my replies are in bold
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
Hello World, Part 1

Code:
extends Activity {
Android splits applications into Activities, right?

congrats on starting to learn android.

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. http://developer.android.com/guide/topics/fundamentals/activities.html

for example, lets say you make an application that simple let you enter text in a boxes, then when you click next it takes you to another page where you enter more text in boxes you click done and the application ends and goes back to the home screen.

those are two activities. each of those activities has an xml file in res/layout which defines what the gui looks like.

if you want the gui to look different when the user holds the phone in a landscape fashion then copy the res/layout to res/layout-land and make the appropriate change to the layout in layout-land directory. Keep the file names the same.

in your activity under super.onCreate(savedInstanceState); put setContentView(R.layout.theNameOfYourLayoutXMLfile);

when you leave the activity the following methods are called: http://developer.android.com/images/activity_lifecycle.png not all are guaranteed to get called depending on the situation in which you left the activity or that a user will come back to your activity and exit properly. You can count on onPause getting called so can put your persistence (info saving) in there.
 
Last edited:

oynaz

Platinum Member
May 14, 2003
2,449
3
81
Thanks for the help and encouragement. I can already see that your explanations will make a huge difference.
I have made some very basic ASP code (a database connection and some simple select and update SQL querys) a few years ago. Apart from that I have only coded in HTML/CSS and Commodore Basic.

I will have a look at the XML part of the Hello World tutorial tonight, provided the baby doesn't throw a tatrum.
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
Thanks for the help and encouragement. I can already see that your explanations will make a huge difference.
I have made some very basic ASP code (a database connection and some simple select and update SQL querys) a few years ago. Apart from that I have only coded in HTML/CSS and Commodore Basic.

I will have a look at the XML part of the Hello World tutorial tonight, provided the baby doesn't throw a tatrum.

in addition to the layout xml's you have a strings and a manifest.

the strings xml is self explanatory, you put all your string in there. you can call those string in your other xml files by using @string/myString and in your activities using R.string.myString (which will return the id (which is an int)). if you want the actual string in the activity you get it from your context.

Code:
Context context = getApplicationContext();
String myString = context.getString(R.string.myString)

in your manifest for each activity you list things like the application name, label of the activity (whats displayed in the title bar), and the intent (how its going to launch, etc..) http://developer.android.com/guide/topics/manifest/manifest-intro.html

For now it would be good to learn how to list an explicit intent vs implicit intent in the manifest. so to go about learning this add two buttons in your hello world application where one button opens a helloworld2 activity displaying some text and the second button opens a helloworld3 activity displaying some text.

have button one open helloworld2 via explicit intent and have the second button open helloworld3 via implicit intent.

so in order to do that you need to add the buttons to your layout, make two activities and two xml layout files for those activities. Update your manifest with the right intent-filters and add the onclick button code.

starter code for explicit intent

Code:
Button myButton= (Button)findViewById(R.id.myButton);
        myButton.setOnClickListener(new OnClickListener() {
        
         public void onClick(View v) {
         Intent explicitIntent = new Intent(HelloWorld.this, HelloWorld2.class);
         startActivity(explicitIntent);
         }
        });
 
Last edited:

oynaz

Platinum Member
May 14, 2003
2,449
3
81
04 - Hello World part 2

Net posted while I did this part. I covered some of what he wrote. I will look into explicit and implicit intent next time. Thanks for the suggestion.

I just remembered. My laptop has a &#8221;high performance mode&#8221; Click.

Whoa, that made a difference. Nice. Remember this, kids.

I thought of the relation between the Android code and the XML code as like that between HTML and CSS. I was right:

This model is inspired by the web development model, wherein you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data.

Nice to be right once in a while.

OK, the tutorial tells me that I can use the name of any class that extends View as an element. I get the gist of that, but what is an element, exactly? Part of a class?

I am also told 7hat I should place the XML layout files in the /res/layout folder, and landscape layout files in /res/layout-land &#8211; as Net wrote above.

I open main.xml in Eclipse. Now, this is neat. There is an example interface included here, with the option of rotating the screen, changing screen size etc.

I enter the XML code from the tutorial:

Code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
* android:id="@+id/textview"
* android:layout_width="fill_parent"
* android:layout_height="fill_parent"
* android:text="@string/hello"/>

I edit strings.xml per the tutorials instruc...

oops.

Code:
[2012-02-16 21:18:55 - HelloAndroid] Error in an XML file: aborting build.

My first bug! Woohoo!

Eclipse points out the places with red squares and tells me:
XML document structures must begin and end within the same entity. Ah:

Code:
/>
my old foe, we meet again. Bam:
Code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/hello"/>

Another error: No ressource found that matches the given name at 'id' with value @id/textview

Oh well. The XML did build. Close, and a small cigar, possibly a cigarillo. I will define that value later in the tutorial, I suspect.

OK, open strings.xml in res/values. Time to put something in @string/hello. Eclipse has made something for me.

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, HelloAndroid!</string>
    <string name="app_name">Hello Android</string>

</resources>

OK, I can edit the &#8221;hello&#8221; string to something more meaningful. I will let my daughter enter something:

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">78gbjumjjnjx</string>
    <string name="app_name">Hello Android</string>

</resources>
Her first word. Awwwww.

Now I need to edit my HelloAndroid class:

Code:
package com.example.offkeydevelopment;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main)
    }
}
Can you spot the error in my code? Eclipse could, and told me to insert a ; after my setContentView statement. It also tells my that android.widget.TextView is never used. Cool, I can delete that, I use the XML file instead. My first performance optimization :)

But, my @id/textview in main.xml is still resourceless. Hmm. Hmm. Hmm. OK, I give up, I look in the tutorial. My code:

Code:
android:id="@id/textview"

Tutorial code:
Code:
android:id="@+id/textview"

OK, syntax error-ish. Can anybody explain the difference between id and +id?

OK, run. It works. This emulator takes a while to start. Not critical, but might get annoying. Any way to speed it up?

My observations so far:

Eclipse seems a great tool. Keeping the structure of the project will be very important, Eclipse makes it much easier. The debug capabilities alsp seem very good. The syntax error detection and suggestion alone will save my hours. The last development tool I used was Stone's Webwriter, and while it was great at the time, it is outdated now.

This is going to be fun! When you get your head around it, the structure of the language seems logical.

Next time, I will make red text! Stay tuned.
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
Code:
@+id/textview

this creates the id named textview. id's are in the R.java file which is auto generated.

Code:
@id/textview

would reference textview from the R.id method. if it hasn't been created somewhere else then you'll get an error.

if you need to change or get something from your textview then in your activiy you'll call

Code:
TextView myView = (TextView) findViewById(R.id.textview);

// do something

if you just want to see it in your view then you don't need to get it in your activity java code