Click here to Skip to main content
Click here to Skip to main content

Tagged as

Create Your 1st Android Application

, 30 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Help Beginners to create their 1st Android Application by a step-by-step guide

Introduction

This Article explains every components related to Android Project and how you can use or customize it. It provides step by step instructions for a beginner android developer to create an android project and run it on emulator or android device, as well as how to debug your code. This article is considered a 3rd article after the first 2 articles that describes more basic information about Android itself. [Article #3]

For Introduction About Android Kernel, kindly take a look on this article i found it one of the best http://www.codeproject.com/Articles/802449/Article-Introduction-to-Android

Now back to our article, it is composed of the following sections:

  1. Creating An Android Project using Eclipse IDE
  2. Basic explanation of the main components
  3. Testing on an Emulator
  4. Testing on a Physical Device
  5. How to Debug your code

1. Creating An Android Project

     1.1 Open Eclipse IDE

      Android Development Tool

     1.2 Go to File->New->Android Application Project

 

     

     1.3 New Android Application Screen

     

           (1) Set Your Application Name that appears on the android device

           (2) Set Your Project Name that appears on the Eclipse Projects Explorer

           (3) Set Your Package Name that appears on Eclipse Package Explorer and you can create multiple packages in same project

           (4) Set Your Minimum SDK Version that can run your application

           (5 - 6) Set Your Target SDK and Compile SDK Version

           (7) Choose Your Theme from one of the available themes, currently the newset one is called Holo

 

     1.4 Click Next

     1.5 Configure Launcher Icon. You can set any image from your library

     1.6 Create Blank Activty then click Next then Finish. Now We are ready to get into more details

2. Basic explanation of the various components

     2.1 src/Package Name

     This is where you can add your .java classes and all your logic inside it.

     As we see in the below code, we have a java class called MainActivity that extends Activity, and this class is located inside src/com.example.helloworld package.

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

     2.2 res

    You can use this libarary to access all your resources like images, sound files, menu , and layout

         2.2.1 Drawables ( Images )

         You have a various of drawables folder for small/medium/large/x-large/xx-large devices, you should add an image of these various sizes to make sure that your application design fits with all device screen sizes.

         2.2.2 Layout

        This folder contains the layout of all your screens, after creating ower project and chosed MainActivity.java, automatically it created an .xml layout file with it called activity_main.xml. For each class of type Activity,must be related to a xml layoutfile, and different classes can be related to the same layout file.

This an example of layout .xml file (activity_main.xml) whic represents the layout/look of MainActivity.

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

        2.2.3 Menu

        It's .xml file that represents the menu items that appears on user click on menu and it's represented as below 

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

        2.2.4 Values

         You can add different xml files like color.xml , dimens.xml, and strings.xml to add some default values to be used later in our application.

//styles.xml
/// AppBaseTheme is represening our selected default Theme "android:Theme.Light" and we extend it by another Theme "AppTheme" to customize the default theme.

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>
//dimens.xml
<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>


//strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">HelloWorld</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

     2.3 AndroidManifest.xml

     This file contains your configurations like currentVersion, Minimum SDK and Target SDK, as well as a reference to all your classes (MUST Include All your Activity Classes to this file) and set which class as your default and start your application with. Also, you can add any permission required like accessing the internet by adding the permission in this file.

<?xml version="1.0" encoding="utf-8"?>
<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="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloworld.MainActivity"
            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>

3. Testing Using an Emulator

     3.1 Right-Click on Project->Run As->Android Application, then wait until it opens the emulator and starts your application as described in the below images

4. Testing Using a physical device

Now, we will run our application on a physical device, and this is much preferable than an emulator because it's faster, better , and emulator doesn't allow to test all features like recording or camera.

     4.1 Bring your USB Cable and your mobile, connect to your PC

     4.2 Click on "Open Perspective"

     4.3 Choose 2nd Option which is "DBMS", and if you have any connected device it will appear.

NB : You must enable "USB debugging" from Settings/Developer options on your device

     4.4 Return to Java tab and run your application,and such windows will appear and select your device then click Ok and it will run

 

5. How to debug your code

     5.1 Add a break point where you want to debug your code

     5.2 Choose Debug insted of Run and choose your device

     5.3 Whenever it's time to execute the chosed line,it will transfer you to debug mode and you will be able to debug and watch your variables.

Summary

I hope this article helped any one who is interested to start developing android applications and found his easiest way to start his way.

Feel Free to ask any questions at any time.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ahmed Alaa El-Din
Software Developer Enozom Software Company http://enozom.com/enozom/
Egypt Egypt
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinprotectorRichard MacCutchan22-Aug-14 7:23 
GeneralThe best article PinmemberMember 106788256-Aug-14 9:08 
GeneralRe: The best article PinmemberAhmed Alaa El-Din6-Aug-14 12:17 
GeneralThanks for entering! PinstaffKevin Priddle31-Jul-14 10:21 
GeneralRe: Thanks for entering! PinmemberAhmed Alaa El-Din31-Jul-14 10:29 
GeneralMy vote of 5 PinprofessionalSunasara Imdadhusen30-Jul-14 19:53 
GeneralRe: My vote of 5 PinmemberAhmed Alaa El-Din30-Jul-14 23:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 30 Jul 2014
Article Copyright 2014 by Ahmed Alaa El-Din
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid