Android development primer: Creating your first Android Application

This entry is part 4 of 50 in the series Android Development Primer

So now that you have a little idea what Android is all about, we will create our very first Android Project.

Launch Eclipse IDE. Navigate to File->New->Android Application Project. The “New Android Application” dialog comes to the front.

Depending on the version of Eclipse and Android SDK you are using, some options may or may not be present and some might be distributed over a number of dialogs.

  • Project Name is the name of the project you want your application to be in.
  • Application Name is the name of the application. This is the name by which your application will be installed in users’ devices.
  • Package Name is the package in which your application resides. It often looks something like “com.example.yourappname” and automatically appears once you put in the Application Name. You can choose to edit it or leave it be. Usually developers replace “example” with their name.
  • Minimum Required SDK  or Min SDK Version shows the lowest Android version that your application supports. For instance if you set this to API 9 (Gingerbread), devices with API 1-8 will not be able to install your application. It is generally advisable to use as low an API as possible. But then there arises a problem that you will not be able to include features or elements that have been introduced in the later versions. You need to analyze this trade-off and choose this accordingly.
  • Target SDK or Build Target is the version of Android that your applications targets.
  • Compile With should generally be the same as Target SDK.
  • Theme shows the basic theme that you want your application to have. Choose whichever suits your needs.
  • Create Activity is to be checked and the name of an activity to be provided. Usually, this is the activity that appears when your applications is launched and is named Main or MainActivity.
  • If you have the latest versions of Android SDK you will be taken to a number of dialogs from here where you can select the icon of your application, navigation type etc. Since it is our first application, we will let them be at their default values.
  • When you are done Click Finish.

Now to the left of your coding area, you can see a “Package Explorer” tab that lists all packages your workspace contains. You package sould be visible in this area.

If you can see a small cross in the icon of you project, don’t worry. Android needs a little time to set itself up, after which the cross vanishes. In fact all packages will have this cross every time you launch Eclipse.

UNDERSTANDING THE SKELETON APPLICATION.

You have just created an Android Application Project. In order to assist you Android creates a “Hello World” skeleton application on it’s own. Navigate to <yourprojectname> -> src -> <com.yourname.yourappname> -> Main.java. In newer SDKs Main.java is named MainActivity.java. Once this opens up, take a good look at the source code. If you have prior java experience, you can see some familiar things. You can see the import packages, class name and the parent class name from which this class is inherited. You can also see some functions/methods in the source code. Below is the explanation of all the elements of the source code.

  • import android.app.activity : This is the Android Activity package. This package contains all the functions that one can possibly use in an Activity.
  • public class MainActivity extends Activity : Each activity in your application will be in the form of a Class. This class inherits from the Class Activity. This class/activity hence should override the required functions of the original class.
  • onCreate() : This is the function that is called when an activity is first created. This takes an argument Bundle savedInstanceState. For the time being, it will be sufficient for you to know that the state of an activity is saved in a Bundle. We will get into further details later.
  • super.onCreate() : This calls the onCreate() method of the super class i.e. the Activity class with the same arguments.
  • setContentView() : This assigns a layout to the Activity. The argument to this function is R.layout.activity_main. We’ll come to it in a minute.

So, when the activity is first created a couple of things are done. The super class method is called and a layout is assigned to the activity. There are many other functions such as onPause(), onStart(), onResume() etc. in order to monitor the life-cycle of an activity. We’ll discuss this in detail at a later time.
In order to understand what a layout is navigate to yourprojectname -> res -> layout. You can now see the “activity_main.xml” file. In some systems it might be named “main.xml”. This is the layout of your activity. Open it and have a look at the source code.

  • You can see that there are two tabs at the bottom – Graphical Layout and activity_main.xml
  • The Graphical Layout shows what your layout would look like in the device/emulator.
  • activity_main.xml shows the xml code of the layout.

Depending on the version of SDK some of the below components may or may not be present in your xml code.

  • LinearLayout : It tells you that all the components in your layout will be laid down in a linear fashion i.e. one below the other.
  • RelativeLayout : It tell you that all the components in your layout will be laid down in a relative fashion i.e. you can choose the height and width of it’s location from the axes.
  • TextView : This is a rather widely used component in applications. It is used to create static text in an application.

You can also see some properties inside the tags in the xml code. Here’s a brief description of them

  • layout_width/height determines the width and height respectively of the particular element. match_parent/fill_parent means that it covers the entire width/height of it’s parent layout i.e. the layout it is inside of. wrap_content means that it only occupies the space that it requires.
  • margin_top/bottom/left/right determines the margin between two components or the borders of the screen
  • orientation determines the orientation in which elements will be added within this layout. If the orientation is horizontal elements will be added side-by-side. It is mainly a feature of Linear Layout.
  • text is present in the textview. It determines the text that the textview is to show on the screen.

So, now that you are familiar with the skeleton application that Android provided you with, it is time to see how it will look like in a device. For this we will use an AVD that we have previously created. If you haven’t created an AVD create one now.
Click the “Run” button which will pop up a dialog box in which you should select Android Application. This dialog appears only the first time an activity of an application is run. If the emulator was already running when you ran the application it installs on your emulator and if not, the most suitable emulator from your created AVDs is selected and launched. Once the application is installed it runs automatically. You can see the “Hello World, Main!” message on the screen.

Although this is a basic way to get started with an application, I’m gonna go ahead and tell you a couple of things that might interest you. Navigate to <yourprojectname> -> gen -> R.java.
Caution : Take care not to edit the contents of R.java file as it is an Auto-Generated file by the Android system.
Let’s look at how things work

  • With every resources that you add to your project (resources reside in <yourprojectname> -> res) Android is going to add an entry to the R.java file.
  • drawable/layout/string are all resources that you can see in <yourprojectname> -> res. You can now understand why the argument in the setContentView() is R.layout.activity_main.
  • In order to refer to each resource, Android assigns a numerical value that is represented in the hexadecimal form.

Another interesting thing is the res folder. It contains all the resources that your application might need.

  • drawable hdpi/ldpi/mdpi/xhdpi/xxhdpi : These contain the pictures or icons that you have in your applications. In order to put an icon in you application, navigate to <eclipseworkspacelocation> -> <yourprojectname> -> res -> drawablehdpi, paste the icon in it and from your code refer to it as R.drawable.<youriconname>
  • You can choose to have the same icon in different resolutions for devices that have hdpi/ldpi/mdpi/xdhpi/xxhdpi screens.
  • You can also see values folder in the “res” folder. It contains dimens.xml, strings.xml, styles.xml. Click on each one and have a look at the source code.

Now, that you have an idea how to create an application, I will discuss about the elements of the layout and multiple screen support in my later posts.

Series Navigation<< Android development primer: Getting to know Android a little betterAndroid development primer: Layout elements in Android >>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

41,728 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>