Saturday, 14 February 2015

cara membuat aplikasi android part II

The next page lets you select a template for your project. In this case the default Blank Activity is what you need. Every Android application consists of at least on Activity and this template generates a project with a single Activity ready for you to customize.

projectpage3

The next page lets you assign custom names for the various components of your project that the template generates. For a real project you would assign names that were meaningful but in this case you can accept the defaults:

projectpage4

Finally you can click the Finish button and wait as Android Studio creates all the files you need. Even a simple Android project has lots of files so again it all takes time.

First Look

When everything is ready you will see Android Studio for the first time.

Problems?

If you get any error messages - such as  "Missing styles. Is the correct theme chosen for this layout? " Then the chances are you have some left overs from a previous installation. Some times the only way to fix this is to delete all the .android, AndroidStudio and .gradle directories you can and then reinstall. If you have existing projects then don't delete AndroidStudioProjects.
Before you do any of this however it is worth trying the File,Invalidate Caches/Restart command. This usually works for "Missing styles" and similar errors.
As long as everything has worked you should be presented with a view of your new project starting off in the Designer selected.
AndroidStudio

Although there looks like a lot to master in Android Studio's UI most of it you will only visit occasionally. The key things to notice are that moving from left to right you have:
  • the project window
  • The designer split into its Palette and a Layout window
  • A component tree window and a properties window at the far right.
Most of the time you will be using the Project window and the Properties window. In the between the two you will see different editors depending on what sort of file you have selected.
in this case you have by default a layout file - activity_main.xml - selected and hence you have a layout editor in the middle of the screen.
More about layout a little later - first it is important that you know a little about the file structure of a project so that you can navigate to its different parts.

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.
Don't panic.
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.
So let's focus on the files that matter to us.
For our simple program there are only two important files one that determines the Activity's behavior
MainActivity.java
and one that determines most of the visual appearance
Main_Activity.xml
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.
You can see the location of these two important files in the Project window:
PROJECT2A

The java 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 res directory is where you store all of the resources - layouts, bitmaps etc. that you app needs.
So while things look complicated at the moment the only two project files that matter to you, and your project, are MainActivity.java and activity_main.xml.

Anatomy Of An Activity

An Android app is made up of one or more Activities.
You can think of an Activity as being something like a web page complete with HTML to determine what displays and JavaScript to determine what it does.
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 at 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.

So to be 100% clear:
  • the java file contains the code that makes your app behave in particular ways 
and
  • the .xml layout file contains a definition of the intial UI of your app.
Let's take a look at the two files that have been generated for our initial Hello World application beginning with the XML layout.

No comments:

Post a Comment