Friday, February 14, 2014

Android-Eclipse: Understanding My First App

---
Android - Understanding My First App

The next thing to do after the first Android Application (app) successfully ran is to understand the codes and structures behind itself.

This tutorial is a continuation of 
http://basic-steps.blogspot.com/2013/02/android-building-your-first-app.html which was based on  http://developer.android.com/training/basics/firstapp/creating-project.html.
1) Run Eclipse. Open MyFirstApp project.

2) In Eclipse, look at the project explorer panel. Examine MyFirstApp folder structure.


3) As for a start, we are going to look at MainActivity.java, AndroidManifest.xml and project.properties.
4) MainActivity.java
4.1) Double-click the item MyFirstApp/src/com.example.myfirstapp/MainActivity.java


4.2) By default, the editor doesn't show line numbers.
4.2.1) You can turn on this feature via menu 
Window/Preferences.
4.2.2) The Preferences window pops up. Click general/Editors/Text Editors. Tick Show line numbers. Click Apply. Click OK.


4.2.3.) As a result you get a better view of the codes.


4.3) There are three segments of codes; Package segmentImport segment and Class Declaration segment.


4.3) Package segment (line no.2) identifies your app as a unique app and is especially important when the app is published in the Internet.

4.4) The import segment (line no. 3 - 5) declares the standard Android classes that are required by your app.
4.4.1) 
android.os.Bundle - is used to send arbitrary data from one activity to another.
4.4.2) 
android.app.Activity - is used to create activity (one of the several components that can make up an Android Application). What distinguishes an Activity from all other components is that it is the only component that can (and must) have a user interface.
4.4.3) android.view.Menu -Interface for managing the items in a menu.
5) The file MainActivity.java contains the declaration of the class MainActivity at line no.7public class MainActivity extends Activity {...}

5.1) Class MainActivity extends the class Activity. This means that MainActivity will inherit all the properties and methods of the class Activity.

5.2) Class MainActivity may define instructions for the methods that it has inherited, which is called 
overrides. (Line no 9 and line no 15). For the methods onCreate and onCreateOptionsMenu, MainActivity has its own instructions to be executed.
 @Override
 protected void onCreate(Bundle savedInstanceState) {...
...

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {...


6) The super keyword is not specific to Android. It's a concept belonging to OOP, and represents the parent class of the class in which you use it. In Android, it's mostly usefull when you create your own Activity or component, and lets you call a default behaviorbefore implementing yours.

super.onCreate(savedInstanceState);

7) setContentView and getMenuInflater

7.1)  The setContentView method sets the view layout for the activity.
setContentView(R.layout.activity_main);

The resource is located at {project root}/res/layout/activity_main.xml

7.1.1) Notice that Line no.10 and 11 will cause the text to be placed at the center of the screen.


7.2) The getMenuInflater() method sets the menu item of the app.getMenuInflater().inflate(R.menu.activity_main, menu);

The resource is located at {project root}/res/menu/activity_main.xml

7.2.1) Whenever the user tap on Menu button, the menu item will be displayed on the screen.


7.3) Both of the files contain the keyword @string. These keywords are pointing to the string resources located at {project root}/res/values/string.xml


8) The app configuration details is stored in {project root}/AndroidManifest.xml


8.1) Line no. 7 defines the minimum SDK and target SDK version. This corresponds to the settings made during the New Android Application dialog window


8.2) Line no. 11 defines the general application settings. This includes the application name and icons to be used.

8.3) Line no. 16 registers the Activity within this application. This corresponds to the {project root}/src/com.example.myfirstapp/MainActivity.java

8.4) Line no. 20 defines the Intent for the Activity, i.e the abstract description of an operation to be performed towards the Activity. In this example, it calls for the Launcher to launch this app.

9) The app properties details is stored in {project root}/project.properties


9.1) As the comment texts have stated, do not modify this file, you need to use menu Project/Properties in order to change the target. Bear in mind that you need to install the relevant SDK version before changing the target, otherwise Eclipse may produce code error message.

---

No comments:

Post a Comment