Android Development Tutorial

Android Development Tutorial

Android Apps Development Tutorial is a detailed training on developing android applications from ideas to apps store. You are going to start your android development journey through this android development tutorial by work out the smart examples which absolutely very simple yet effective examples you can even download.



Android Development Tutorial — Building blocks of Android


Android platform structed in a way that makes phones more crash-resistant.
Activities

Activity is the building block of the user interface. To be straight into the point, a activity is like the page in a Web app. Android supports a lot of activities. When ever a user cliks submit/login button you can bring up new activities and pressing the back button to back up, just as like as we do in a web browser.

Services

Activities are short-lived and it can be stopped or shutdown at any time.
Services, on the other hand, are designed to keep runnig, independent of any activity. One simple example for service might be checking updates to a twitter/email account.

Intents

Intents are system messages.
  • Used to notify various events like incoming data, hardware state changes…
    create our own intent to launch other activities.
  • We are going to use Eclipse IDE through out this Android Tutorial for Beginners series. It is simple to use yet very effective ide out there for android platform.

Android Development Tutorial Eclipse Installation for Android

If you don’t have eclipse installed in your system then continue reading this post, otherwise you can skip the below steps and

Eclipse Download

There are various versions out there which one should I download ?
You can download any of the following versions.[/box]

android eclipse download

Ok, now you have eclipse ide. Unzip the downloaded package file and run eclipse.exe



  • Now you need to select a workspace, where our project and related files are going to be stored.

  • android development tutorial

  • Click the browse button to select your desire directory. And if you don’t want it to be prompt in every eclipse start up then check.
  • use this as the default and do not ask again.
  • Finally press ok.

Install

  • In eclipse goto Help-Install New Software…

  • android tutorial beginners android install

  • Install window will appear. In that press Add button and give the following credentials in the newly pop up window “Add Repository”.
  • Name Android
  • Location https //dl-ssl.google.com/android/eclipse/
  • Then press ok.
  • Now you will see the Developer Tools in the Install window.

  • android tutorial beginners install developers tools

  • Press Select All and then Next.
  • Now the Configure SDK will be appeared.

  • android tutorial beginners sdk

  • Browse to the location where you want to install new SDK.
  • Press Next to Install the SDK.
  • In the Choose Packages to Install window press select all and click Install.

  • android tutorial beginners package

  • In the Android SDK Manager window, you can see a list of available versions of Android Platform and Extra tools.
  • Its highly recommended to select the basic version that support most of the Android devices out there – Android 2.1
  • And also select the current advanced version. -Android 4.0.3
  • Its always better to check Sample for SDK which provides great example for the components available and a better way to start learning android for the beginners.
  • Google APIs is an extension to the Android SDK development environment that give access to develop applications for devices that includes Google’s set of custom applications, libraries, and services.

android tutorial beginners sdk manager

android virtual device

The Google APIs add-on includes

  • The Maps external library
  • The USB Open Accessory Library (only with API Levels 10 and 12+)
  • A fully-compliant Android system image (with the Maps library and other custom system components built in
  • A sample Android application called MapsDemo
  • Full Maps library documentation (also available on this site)
  • Now you can see newly created icons especially for Android development.
android sdk launch

android emulator


android homescreen

First Android Program

Now we are going to create the most popular hello world program in android.
  • Start Eclipse IDE in which the Android platform is installed.
  • Lets start our First Android Program by creating a new Android Project.
  • You can create an android project by one of the following ways.
  • click the Create Android Project Icon.
  • File-New-Android Project.
  • Right the Package Explorer Area and go New-Android Project.

first android program new android project

In the New Android Project Window, we need tell eclipse a few things about our project

  • Project Name : Name of the project
  • And how to create your project
  • Location where you want to save

first android program new android project

Now lets click Next to move to the window where we can select the target SDK.

  • In Select Build Target Window we can choose the target SDK.

  • first android program new android project

  • If you want to develop an application that need to support most of the android devices out there in the market.
  • Then you should select Android 2.1
  • If your application is going to make use of Maps then select appropriate Google APIs version.
  • In this First Android Program, I am going to use Android4.0.3, just to see how the new version looks like.

Click Next to bring up the next window where we are going to configure our android project.

first android program new android project

Application Name
It is different from Project Name. Application name is the name how our app should be called.
Package Name

Android likes to keep things structured and nicely ordered. We need to give the name of the package where our project files, icon images are going to be stored.And the name you are giving here should be unique.


Make sure Create Activity is checked

and the value here is the name of the activity thats going to be created by android for us.Don’t change anything for now, we can take a deep look at this later.

Minimum SDK

This field is used to specify a minimum platform version for your application

While preparing to set up the application, the device verifies the min sdk value and compares it to the system’s current version. In case the minSdk Version value is actually more than the device version, the device ends installing the application. Likewise, the device installs the application on condition that its android:maxSdkVersion works with the system version.

I am going to leave this field as it is for now.

  • Click Finish.
  • Now you can see the HelloWorld project is created.
Since It is our first project, before going to do anything. I just want to make sure every thing is setup perfectly. We can check this by right click our newly created project, and perform Run As-Android Application.
first android program run

  • It will boot up the Android Emulator.
  • If the Emulator is running for the first time then it will take some time to load.
If you can see the following image the you have setup every thing nice and perfect.
Take your time and play around with Android.
first android program run
first android program run
first android program run

Now it is time to take a look at our project folder.
  • In src folder you can find our package, inside the package you can see HelloWorldActivity which we selected at time of project creation. If we didn’t choose that option, now we wouldn’t have this activity.
  • Next see the gen folder. The files in this directory is generated by the Android platform. So don’t change anything in that.
  • Then come to the res folder here we have our resource files laied out.
  • The Icon of our application is in this drwable folder only.
  • In layout folder by default you will have only one xml file called main.xml
  • The layout xml file is like a screen of the application.
first android program new android project

As you start developing your application you will use more layout xml files. The layout xml file is like a web page where every page is unique and has its own set of components like buttons, text box, images …

  • So for example in a user login system,
  • login form is a xml file – login.xml
  • It has name and password fields.
  • Based on the user input it will open one of the two different layout.
  • user.xml – has user information
  • error.xml – has error message

Inside values directory we have strings.xml, This is where we are going to define all of our String variables.
id and value. id should be a unique one.

AndroidManifest.xml is a file which has informations about how this application should run in a device, which layout it should launch as MAIN.
What are the activities the application has, and which image it should use as icon and we can take a look closer at this in coming days.

Now you may wonder, we didn’t give/change anything.
But how is it displaying “Hello World, HelloWorldActivity!”

You have the answer in the following image.
first android program new android project

Open main.xml from layout folder. And change the view option from Graphical Layout to coding window.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
  • The first line is the xml version.
  • LinearLayout – is the default layout. But there are number of layouts available. We have to choose a layout in a way that can be appropirate to our application. If you are creating a photo gallary viewer application then you need to use grid view like.
  • TextView – is like a label, we need to specify width, and height of the label.
  • And carefully look at the next line
  • android:text="@string/hello"
  • android:text is the text we want to display in the lable.
  • @string/hello – this tells android that hello is a string typed variable. And what ever the value of hello is going to be the text of the label.
But when and where we specified the hello variables value ?
  • Go inside the values folder we have strings.xml, and open it.
  • Choose strings.xml at bottom, it will show the coding window, here you can find the following code.
  • first android program new android project

    <string name="hello">Hello World, HelloWorldActivity!</string>
  • Lets understand this code by spliting it.
string – used to tell it is a string variable.

name – name of the variable. Should be given inside the double quotes.

The value of the variable “hello” needs to be given inside the tags.

<string name="hello">Hello World, HelloWorldActivity!</string>

And finally don’t forget to put closing tag

Now I am going to create a new string variable called “welcome”.
For this I need to write the following code in strings.xml

<string name="welcome">Welcome to First Android Program</string>

Then in in main.xml, change as android:text=”@string/welcome”

Now save the changes and run the project as Android Project.

Someone may ask like, our xml file name in values folder is strings.xml, but here we mentioned @string/welcome, how will it find out the welcome variable?

Here Android plays a smart trick to keep everything nice and clean. Whenever we create anything like string variable, color variable, a new layout, image or icon … It will store its information inside a common Resource file called R.java

Finally we came to know that the name of the xml file doesn’t do anything we can even give myStrings.xml


Now the time comes to take a peek into R.java

open R.java inside gen- your package

first android program new android project

Lets add some more properties
  • android:textSize Size of the text in points ( pt ) – “10pt”
  • android:textStyle Style for the text. (bold, italic, bolditalic)
  • android:typeface Typeface for the text.(normal, sans, serif, monospace)
  • android:autoText If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.
  • android:capitalize If set, specifies that this TextView has a textual input method and should automatically capitalize what the user types.
  • android:gravity Specifies how to align the text by the view’s x- and/or y-axis when the text is smaller than the view.
  • android:marqueeRepeatLimit The number of times to repeat the marquee animation.
  • android:textColor Text color.
  • android:textColorHighlight Color of the text selection highlight.
  • android:textColorLink Text color for links.
Three ways of creating a String
  • So far we have create string using coding window
  • <string name="Name">Name</string>
  • If you wish graphical way of doing things then you can use the xml gui editor.
  • first android program new android project
    first android program new android project

  • This is my favorite way of creating strings.

In main.xml , just enter the android:text value inside TextView, now the android will give warning to us that the string has not created. Now select the string and goto Refactor from top menu and perform Android=>Extract Android String.

first android program new android project

Now the Extract Android String window will appear, in that you need to give the String value that you want in the String text field, and the variable id/name in the Replace by R.String text field.

first android program new android project

Finally select the Resource file where you want to include the String, in this case strings.xml
Then press ok to create the string variable. Now the warning has gone in the main.xml and instead of “hai” you will see “@string/hai”.

Now its time to place our string at the center of the screen, for that we need to tweek main.xml


Insert the following code inside Linear Layout

android:gravity="center"

And in the TextView we need to change the

android:layout_width="wrap_content"
android:layout_height="wrap_content"
  • wrap_content – means it will take the size of the content.
  • layout_width=”wrap_content” – then the width becomes the length of the text.

    layout_height=”wrap_content” – then the height becomes the height of the text.

Now the text becomes centered to the layout.
Lets change the color of the Text and Background.

Sofar we have know how to create string, but how can we insert color property?
Insert the following code inside the TextView.

android:textColor="#ff0000"

#ff0000 is the hexa decimal value for the color red.

  • To change the background property of the layout, insert the following code inside LinearLayout.
android:background="#00ff00"

first android program new android project

  • If you want to use text value instead of hexa-decimal values then you need to perform some steps like how you have created the String variables.
    • Add the following code inside the resource tag of strings.xml
    <color name="my_red">#ff0000</color>
    <color name="green">#00ff00</color>
    • Then in main.xml you can use the color name instead of hexa-decimal.
    first android program new android project

    android:textcolor="@color/my_red"
    • If you don’t want to mess up strings.xml with color values then create a new xml file in values folder and give any descriptive name to the xml file.
    Here I am going to create colors.xml file
    Follow the below images to create a new xml file.
    first android program new android project

    • Right click values folder then goto New=>File
    • Choose the parent folder that is values folder then,
    • Give the name of xml file like colors.xml then click Finish.
    first android program new android project


    It will create colors.xml file copy the following code to colors.xml from strings.xml file.
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <color name="green">#00ff00
    <color name="red">#ff0000
    </resources>
    

    first android program new android project

    Then cut and paste the color code from strings.xml to colors.xml.

    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
    

    Save Changes in both xml files.
    first android program new android project

    Lets take a look at HelloWorldActivity.java in src=>package.
    • HelloWorldActivity extends Activity
    • onCreate() is the method called when the activity is first created
    • setContentView(R.layout.main) –
      Here we have set up the main layout as our default layout. That is when the application launch at the time main layout will be appear.

    first android program new android project

    R.layout.main
    • R – resource R.java
    • layout – Layout Class
    • main – variable in the layout class.

    So in our java code, if we want to specify our android elements we need to refer R.
    Finally we are going to give attention to AndroidManifest.xml

    first android program new android project

    android:minSdkVersion=”15″

    Inside tag we have android:icon=”@drawable/ic_launcher” this code setup the icon of the application.

    android:label=”@string/app_name”

    this is the application name.
    If you want to change your application name then change it to any desire name you want.



    activity

    is used to add activity information to the Manifest file.




    Whenever we create an activity we need to register the activity by giving name of the activity.
    <activity
    android:name=".HelloWorldActivity"
    android:label="@string/app_name"/>
    

    If you want to change the app name when this activity is loaded then specify the text as

    android:label="@string/desire_name"

    Intentfilter is used to specify the activity which should be used as the launcher of the application.

    Now we are going to change the Icon of our Application.

    first android program new android project

    • To change the icon of your application,
    • place your icon image in the project folder called res=>drawable
    • Now in Eclipse refresh your project folder.
    • Finally you need to change the icon name in your Manifest file.
    android:icon="@drawable/logo"

    first android program new android project
    first android program new android project
    first android program new android project

    You have set up everything perfectly, now its time to run our program, so save all changes and run as Android Project.You will see the following screen shot.

    first android program new android project
    first android program new android project

    So in this First Android Program Tutorial we have covered some basic stuff like adding text, color, positioning, additional text propertise, and change application icon.

    And also we have peek on the Activity, and Manifest file.

    Complete Code

    main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#555555"
    >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hai"
    android:textColor="@color/red"
    android:textSize="10pt"
    android:textStyle="bold"
    android:typeface="serif" />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/welcome"
    android:textSize="10pt"
    android:typeface="serif"
    android:textStyle="bold"
    android:textColor="@color/green"/>
    </LinearLayout>
    
    strings.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello">Hello World, HelloWorldActivity!
    <string name="app_name">HelloWorld
    <string name="welcome">Welcome to VimalTuts
    <string name="hai">Hai
    </resources>
    
    colors.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <color name="green">#00ff00
    <color name="red">#ff0000
    </resources>
    
    AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vimaltuts.android.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
    android:icon="@drawable/logo"
    android:label="@string/app_name" >
    <activity android:name=".HelloWorldActivity"
    android:label="@string/app_name" >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>
    </manifest>
    
    HelloWorldActivity
    package com.vimaltuts.android.helloworld;
    import android.app.Activity;
    import android.os.Bundle;
    public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
    }
    
    Lets continue our Android development. This android development tutorial is just like get into the car but you need to drive a long way to reach your destination. Keep reading all the tutorials in the android development tutorial section.

    Leave a Reply

    Scroll To Top

    Foolow Me on Google Plus

    VimalTuts on Twitter
    63 people follow VimalTuts
    engmomanalanosmaabhijeetminkyuuuiambgvdeeprojeMillerHerupinder