A First Android Project

In the previous leson, you created an Android project. When you create a new project, you'll see a screen like this one:

Android Studio IDE

The large white area is where you'll do all your coding. The MainActivity.java file is opened, in the image above. Click the small x to close it down:

Close down a file in Android Studio

Now, the activity_main.xml file is displayed. Close this down, too, to leave just a grey coding area:

Android Studio with no projects open

The smaller white area on the left is where all your project files are. The top left item is called app. Expand the app item to see three more items: manifests, java, and res. The java item is expanded in the image below, and the Java file highlighted:

Project Explorer showing Android folders

For a simple, blank project like this you'll have two files that you'll work on the most. The first file is the Java file. This is where the code lives. The Java code is separate from any interface you design. The interface you'll build, the one that users will interact with, is created with XML. The Java file points to the XML file.

With the java item expanded, under the first item on the list that says com.homeandlearn.ken.helloandroidworld in the image above, you should see a Java file called MainActivity. Double click this Java file to open it back up again. Double-clicking a file in the project explorer on the left will open up that file in the main editing window.

Now expand the res folder to see more items in this folder. The folders under res are drawable, layout, mipmap, and values. Expand the layout folder to see a file called activity_main.xml:

The Android layout folder

Any file you have for an interface should go in the layout folder. Now double click the XML file to open it up in the editor. If you have closed down an XML or Java file in the editor, just remember where they live: the XML file is in the res > layout folder, and the java file is in your java folder, under your project name.

Now examine the Java file:

The Java code

The Java file doesn't have many lines in it. But look at this line:

setContentView( R.layout.activity_main );

This is a Java method called setContentView. It sets the XML file you want as your main layout when the app starts. In between round brackets, you need the name and location of your layout file. The letter R in the round brackets is short for res. This is the resource folder where those drawable, layout, mipmap, and values folders are. The layout.activity_main part points to the activity_main XML file, which is in the layout folder of res. If you wanted a different XML file to load when your app starts, you'd point to a different file:

setContentView(R.layout.some_other_xml_file);

Let's take a look at the XML file.

Double click the activity_main.xml file under the layout folder. This will open it up again in the main coding area:

An XML layout file open in the Designer of Android Studio

In the coding area, an XML file can be viewed in Design view and Text view. The Design view is where you can drag and drop controls on to your app's interface. It's obviously used for designing your app, and tries to guess what it would be like when running on a phone or tablet. The blue version is called a blueprint. You'll see much more on Design view in later sections of this course.

Click the Text tab at the bottom to go to see the actual XML that is producing the interface from Design view:

The Text tab highlighted

The XML should look like this:

The XML for an Android layout

We'll get to what all this means in later sections. One change you can make, however, is to the android:text at the bottom. In the image above, ours says "Hello World!". Change it to something else. In the image below, we've changed it to "Hello Android World":

Changing the Text on a TextView

 

In the next lesson, you'll learn how to run your apps on a Android Virtual Device, and on a real device.