Page 2 of 4
The next dialog box allows you to select the icon used for your app - just select Next there is time to work on custom icons when you have an app that is worth the effort:
The next dialog box asks you to select the type of Activity you want to create. Every Android app consists of at least one Activity - you can think of it as the basic unit of app construction.
We need to talk much more about Activities later but for the moment simply select a Simple Blank Activity. If you don't see Simple Blank Activity then you haven't downloaded and installed the custom template - see the Important Note earlier.
If you can't or don't want to install the custom template then you could use the standard Blank Activity but this creates a more complex project structure and you will have to edit the files to match the examples given or cope with the differences.
Finally you have to give your Activity a name - for the moment just accept the default of MainActivity.
When you click the Finish button Android Studio starts to build your project. It can take a few minutes so don't assume it has crashed.
Basic Project Structure
When the project has finished building all of the files created can be viewed by opening the Projects tab. The main thing to notice is that there are a great many folders and files.
It seem almost unbelievable that the simplest Android app you can create involves so many files.
Most of the files that have been created are autogenerated and most of the time you don't need to know anything about them let alone open or edit them. In fact opening and editing autogenerated files really isn't a good idea.
For our simple program there are only two important files one that determines the Activity's behavior
and one that determines most of the visual appearance
You can set which activity is the one that the system starts but by default it is the single activity that you created and named when you set up the project. In this case we named the activity MainActivity and its layout file Main_Activity - but you could change these defaults.
The MainActivity.java file can be found in the src/main/java directory and the Main_Activity.xml file can be found in src/main/res/layout directory.
The src directory is from your point of view where most of the construction of your app occurs so make sure you know where it is. The src/main directory is where you keep the files concerning the main Activity and the Java code goes in Java and the various resource files belong in res.
Anatomy Of An Activity
An Android app is made up of one or more Activities.
In the case of an activity the layout is determined by the XML file in resource (res) directory and the behavior is determined by the Java code in the Java directory.
The XML can be thought of as a markup language much like HTML or XAML.
It defines an initial layout for the screen when the app first runs. It is possible to generate new layout components are run time from the Java file. In fact if you really want to you can dispense with the XML file and generate everything from Java but as you will discover the XML markup approach is much the best way to do the job - because of the availability of the Designer.
Let's take a look at the two files that have been generated for our initial Hello World application beginning with the XML layout.
First the XML layout file. Double click on activity_main.xml file in the Project tab and the file will open - but not as an XML file as you might expect. What you see when you open a layout file is the drag-and-drop layout designer.
This looks a little too much to take in when you first see it but you will quickly get used to it.
On the left is a Palette of all of the components - buttons, text, checkboxes and so on - that you can place on the design surface.
In the middle is the design surface and this defaults to the screen size and appearance of the Nexus 4. You can select other devices to work with.
On the right you have the Component Tree which shows you the structure of your layout - its the same as the nesting structure of the XML file. Below the Component Tree you have the Properties window that can be used to set the properties of any component in the layout.
If you have used any drag-and-drop designer then this one will be familiar to you. If you have struggled with the detailed layout using a markup language be it HTML, XAML or XML then you will like how easy the designer makes building and testing a UI.
In the case of our sample program the only component uses is a single TextView already containing the text "Hello World".
You can modify the greeting text if you want to. Select the TextView component and use the Properties window to find its Text property. Change this to read "Hello Android World".
Don't worry about the form of the original entry starting with an @ and looking complicated just type the string in as given.
You can use the designer to create any UI you care to and you really don't have to get involved in the XML that corresponds to the layout.
The designer will automatically generate the XML needed to create the layout for you and modify it as you change the layout. If you really want to see the XML then all you have to do is select the Text tab at the bottom of the designer window. You should find it fairly easy to understand but leave it to the designer to modify it.
We will return to the designer and the XML it generates later.