Creating Your First Android Application - Android: Programming and App Development for Beginners (2015)

Android: Programming and App Development for Beginners (2015)

Chapter 3. Creating Your First Android Application

When you start to create your first application for Android, you should use Eclipse IDE. Open Eclipse, select File>New>Project and then click on the wizard for Android New Application from the list of wizards that are shown. Give your application a name, for the purposes of this, we will stick with HelloWorld. Follow all the other instructions on the screen, making sure that all the other entries are set as Default right through to the last step. That will give you a project screen showing your successfully created project.

Before you even think about running your app, you need to be aware of some of the files and directories in your Android project so let’s have a look at how an application is made up.

src

Contains .java source files that are needed for your project. A MainActivity.java file is included by default, and that contains an activity class that will run as soon as your app is launched on a device from the app icon

gen

Contains a computer generated .R file that is for referencing the resources contained in your project. This is one file that you must never modify

bin

Contain package files with the extension .apk. These are built during the build process. This folder also has everything else that is requires to run your Android application

res/drawable-hdpi

A directory for drawable objects that have been specifically designed for high density displays

res/layout

A directory for the files that will be used to define the user interface of your app

res/values

A directory that contains various other XML files which, in turn, hold a number of resources like color definitions and strings

AndroidManifest.xml

The manifest file responsible for the description of the characteristics of your application and for defining the individual components

In this next section, we are going to look at some of the more important application files, along with examples.

The Main Activity File

MainActivity.java is the name of the main activity code. This is in the form of an application file that will be converted into a Dalvik VM executable file and is what runs the application. The following is the default code that will have been generated for the “Hello, World!” application in the wizard you used earlier:

· package com.example.helloworld;

· import android.os.Bundle;

· import android.app.Activity;

· import android.view.Menu;

· import android.view.MenuItem;

· import android.support.v4.app.NavUtils;

· public class MainActivity extends Activity {

· @Override

· public void onCreate(Bundle savedInstanceState) {

· super.onCreate(savedInstanceState);

· setContentView(R.layout.activity_main);

· }

· @Override

· public boolean onCreateOptionsMenu(Menu menu) {

· getMenuInflater().inflate(R.menu.activity_main, menu);

· return true;

· }

· }

In this code, notice that R.layout.activity_main is referring to the activity_main.xml file that is found inside the res/layout folder. The method onCreate() is one of a large number that are configured whenever an activity loads.

The Manifest File

No matter what components you are developing within your application, you have got to declare every single component in a manifest.xml file. This file lives at the root of the project directory for the application and is an interface between the application and the operating system. If your component is not declared in that file, the operating system will not consider it. An example of a default manifest file is:

· <manifest xmlns:android="http://schemas.android.com/apk/res/android"

· package="com.example.helloworld"

· android:versionCode="1"

· android:versionName="1.0" >

· <uses-sdk

· android:minSdkVersion="8"

· android:targetSdkVersion="22" />

· <application

· android:icon="@drawable/ic_launcher"

· android:label="@string/app_name"

· android:theme="@style/AppTheme" >

· <activity

· android:name=".MainActivity"

· android:label="@string/title_activity_main" >

· <intent-filter>

· <action android:name="android.intent.action.MAIN" />

· <category android:name="android.intent.category.LAUNCHER"/>

· </intent-filter>

· </activity>

· </application>

· </manifest>

The <application>…</application> tags contain the components that relate to your application. The attribute named android:icon points to the icon for the application that is found under res/drawable-hdpi. The application in the example is using an image that I called ic_launcher.png, which is in the drawable folders.

The <activity> tag specifies an activity and the attribute android:name is used to specify the class name of the Activity subclass. The class name is fully qualified. The android:label attributes are used to specify a certain string that is to be used to label the activity. Using the <activity> tags, you can specify as many activities as you want.

Android.intent.action.MAIN is the name of the action used in the intent filter. The name indicates that the activity is the starting point for your application. Android.intent.category.LAUNCHER is the name of the intent filter category and it is used to indicate that a launcher icon on a device can be used to open the application

@string is referring to the strings.xml file that I will talk more about in a bit. @string/app is referring to the string called app_name that is defined within the strings.xml file. In this case, it is “Hello, World!” but many other strings can be populated in the application as well.

Below is a list of the tag that can be used in the manifest file as a way of specifying certain components in the Android application:

· <activity>elements for activities

· <service> elements for services

· <receiver> elements for broadcast receivers

· <provider> elements for content providers

The Strings File

The file named strings.xml can be found inside the res/values folder. It is here that all the text used in your application is stored. An example of this would the names that you put on buttons or labels, default text and any other similar string type. An example of what a default strings file would look like is:

· <resources>

· <string name="app_name">HelloWorld</string>

· <string name="hello_world">Hello world!</string>

· <string name="menu_settings">Settings</string>

· <string name="title_activity_main">MainActivity</string>

· </resources>

The R File

It is the gen/com.example.helloworld/R.java file that holds the activity Java files and the resource files, like strings.xml, together. The file is generate automatically and must not be modified. An example of what an R.java file would look like is:

· /* AUTO-GENERATED FILE. DO NOT MODIFY.

· *

· This class was automatically generated by the

· aapt tool from the resource data it found. It

· should not be modified by hand.

· */

· package com.example.helloworld;

· public final class R {

· public static final class attr {

· }

· public static final class dimen {

· public static final int padding_large=0x7f040002;

· public static final int padding_medium=0x7f040001;

· public static final int padding_small=0x7f040000;

· }

· public static final class drawable {

· public static final int ic_action_search=0x7f020000;

· public static final int ic_launcher=0x7f020001;

· }

· public static final class id {

· public static final int menu_settings=0x7f080000;

· }

· public static final class layout {

· public static final int activity_main=0x7f030000;

· }

· public static final class menu {

· public static final int activity_main=0x7f070000;

· }

· public static final class string {

· public static final int app_name=0x7f050000;

· public static final int hello_world=0x7f050001;

· public static final int menu_settings=0x7f050002;

· public static final int title_activity_main=0x7f050003;

· }

· public static final class style {

· public static final int AppTheme=0x7f060000;

· }

· }

The Layout File

In the res/layout directory you will find he layout file called activity_main.xml. This is the file that your application references when building the interface; be prepared to make numerous modifications to this file as you change the layout of the application. For the “Hello, World!” application, the file will contain the following default layout content:

· <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

· xmlns:tools="http://schemas.android.com/tools"

· android:layout_width="match_parent"

· android:layout_height="match_parent" >

· <TextView

· android:layout_width="wrap_content"

· android:layout_height="wrap_content"

· android:layout_centerHorizontal="true"

· android:layout_centerVertical="true"

· android:padding="@dimen/padding_medium"

· android:text="@string/hello_world"

· tools:context=".MainActivity" />

· </RelativeLayout>

TextView is an Android control that is used in building the GUI. It will contain a number of different attributes, such as android:layout_height, android:layout_width, etc. These are all used to control different aspects of the application layout, such as height and width. @string is referring to the strings.xml file, which can be found in the res/values folder. In the case of this example, @string/hello_world is referring to the hello string that is defined in the strings.xml file

Running the Application

Ok, now it’s time to try and run that “Hello, World!” application you created. Open up Eclipse, open a project activity file and click on the Run icon in the toolbar. When you set up your environment part of it would have been to create an AVD so Eclipse will install your app to the AVD and start it. If all is well, you will see an emulator window that prints your text, “Hello, World!”

Congratulations, you have created your very first Android application. The rest of this chapter is designed to help you become an even better developer. To be fair, what you have used so far is very little in terms of what you can use to build a decent android application. Quite aside from the coding, you need to look at other resources, such as the static content used by your code, i.e. bitmaps, layout definitions, colors, animation instructions, user interface strings and much more besides. All of these resources will be maintained inside separate sub directories, all fond under the project directory called res/.

Next, we are going to talk about organizing the resources you have used in your application, some alternatives that you can look at and how to access them in the applications.

How to Organize the Resources in Eclipse

Each separate resource should be in a specific sub directory of the res/ directory. An example of the hierarchy for a simple project would look something like this:

· MyProject/

· src/

· MyActivity.java

· res/

· drawable/

· icon.png

· layout/

· activity_main.xml

· info.xml

· values/

· strings.xml

Inside the following example directory, we have subdirectories for image resources, string resources and tow layout resources.

· MyProject/

· src/

o main/

o java/

o MyActivity.java

· res/

· drawable/

· icon.png

· layout/

· activity_main.xml

· info.xml

· values/

· strings.xml

The next table shows you how the resource directories are supported inside the res/ project directory:

Directory

Resource Type

anim/

These are XML files that define animations of properties. They are saved to a folder called res/anim/ and can be accessed via the R.anim class

color/

These are XML files that define state lists of colors. They are saved in a folder called res/color and are accessed via the R.color class

drawable/

These are image files like XML, .jpg, .png or .gif that are compiled into shapes, state lists, bitmaps, animation drawables etc. They are saved to a folder called res/drawable and can be accessed via the R/drawable class

layout/

These are XML files that are used to define the layout of a user interface. They are saved to a folder called res/layout and can be accessed via the R.layout class

menu/

These are XML files that are used to define menus in the applications, such as a Sub menu, Context menu or an Options menu. They are saved to a folder called res/menu and can be accessed via the R. menu class

raw/

These are arbitrary files that are saved in their raw form and to open the you must call Resources.openRawResources() using the resource ID ofR.raw.filename

values/

These are XML files that have simple values in them, such as integers, string and colors. Some of the conventions for naming the resources that can be created in this directory are:

· arrays.xml – for resource arrays, found in the class called R.array

· integers.xml – for resource integers, found in the class called R.integer

· bools.xml – for resource booleans, found in the class called R.bool

· colors.xml – for resource colors, found in the class called R.color

· dimens.xml – for dimension values and found in the class called R.dimen

· strings.xml – for string values and found in the class called R.string

· styles.xml – for styles and fond in the class called R.style

xml/

These are arbitrary XML files that can be called with Resources.getXML()to be read at runtime