Home and Learn: Android Course
In the previous leson, you created an Android project. When you create a new project, you'll see a screen like this one:
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:
Now, the activity_main.xml file is displayed. Close this down, too, to leave just a grey coding area:
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:
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.example.myapplication 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:
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 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.
In the coding area, an XML file can be viewed in Design view and Code view. (There's also a Split view where you view the Code and the Design) The Design view is where you can drag and drop controls on to your app's interface. The blue version ont right is called a blueprint. You'll see much more on Design view in later sections of this course.
Click the Code tab at the top right of the Designer. You'll then see the actual XML that is producing the interface:
The XML should look like this:
We'll get to what all this means in later sections. For now, we'll move on. In the next lesson, you'll learn how to run your apps on a Android Virtual Device, and on a real device.
< Getting Started| Running your Android Apps >
Back to the Android Contents Page
Email us: enquiry at homeandlearn.co.uk