Page 2 of 4
If you select the Create Custom Launcher Icon in the first dialog box you will be given the chance to specify a custom icon for your app. At this stage you have enough to worry about so don't select this option. There is time to work on custom icons when you have an app that is worth the effort so just click the Next button to see the next dialog box.
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 Blank Activity - this is the simplest of all of the projects that Android Studio can generate for you.
Finally you have to give your Activity a name - for the moment just accept the default of MainActivity. You can also use this dialog to select addtional features that might want such as a Fragment or an Action bar. For the moment leave this set to None as you really do need the simplest project to get started with. In fact you can carry on selecting None until you have discovered what all the other options are - and they are useful.
When you click the Finish button Android Studio starts to build your project. It can take a few minutes, especially the first time you try it, 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 the java folder and the various resource files belong in the res directory.
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.
What you see is an XML file that specified the layout in text form and a representation of the layout to the far right in another window. You can work with the XML directly to define where all the buttons and text goes and later you will learn at least how to edit it when things go wrong or to fine tune it but - Android Studio provides you with a very nice interactive editor and this is worth using.
If you look at the bottom left you will see two tabs - Design and Text:
You can swtich between editing the XML as text and editing it in the drag-and-drop designer simply by clicking on the tab.
If you now click on the tab the window will display the designer but be patient the first time you do this it might take a few moments.
The designer 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 either on the Nexus 4 image or in the Component Tree 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.