Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,785,791 Members 53,651 Now Online
XDA Developers Android and Mobile Development Forum

[Guide] [Explained] Your first Android App

Tip us?
 
Rachid.Ala
Old
#1  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
Tutorial [Guide] [Explained] Your first Android App

Introduction


Hello guys and welcome to my first guide (be indulgent please haha)
In this guide I will show you how to build your first Android app, knowing java langage will be very helpful to follow up but it's not required.
In addition, we will not see a lot of Java in this introductory tutorial.
That said, if you want to go further in the development of Android applications, you will need to know Java.

In this guide we will create a very simple Android application ("Hello World!) The goal is to see basis of development.
What to expect from this guide
  • Setting up the development environment
  • A very simple example

I’ll be using Windows for this tutorial. However, there will be not much difference on another system (GNU/Linux or Mac OS X). You just need to adapt the instructions to your particular situation.
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 52 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
Rachid.Ala
Old
(Last edited by Rachid.Ala; 9th March 2014 at 01:05 PM.)
#2  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
Default The toolbox : JDK

The toolbox : JDK


As I said quickly in the introduction, Android applications are developed in Java. You may already have some of the tools that I will present here if you ever developed in Java. Well, so much better! It is much less work for you.

A word about Java

One characteristic of Java is portability, which means that computer programs written in the Java language must run similarly on any hardware/operating-system platform. This is achieved by compiling the Java language code to an intermediate representation called Java bytecode, instead of directly to platform-specific machine code. Java bytecode instructions are analogous to machine code, but they are intended to be interpreted by a virtual machine (VM) written specifically for the host hardware. End-users commonly use some special tools installed on their own machines.

These tools are included in the JDK. In addition, the JDK contains the JRE (so also the virtual machine), which is handy.
To summarize, there has two cases:
  • You are a simple user of Java : you must have JRE.
  • You are a developer : you need the JDK tools

Frequently asked question :
  • Since I tried to put myself in Java, I may already have JDK installed ... How do I know if this is the case ?
 

We will make a very simple little test (or rather 2 ).

Start by opening a command prompt in Windows (Start -> Programs -> Accessories -> Command Prompt) or a terminal if you're on a Mac or GNU / Linux.
  • To know if JRE is installed, type the following command:
    Code:
    java -version
  • To know if JDK is installed, type the following command:
    Code:
    javac

In both cases, if the command is not recognized, is that you do not have the tool in question (JRE or JDK).

If you have only the JRE, install the JDK.
If you have nothing ... Install the JDK: p (the JRE is included anyway).

In short, now that you know where you stand with your Java environment, you can do (or not btw) the installation.

While all of these explanations were not necessarily required, it is still better to know what is installed, right ?


Installing the JDK

The installation of the JDK will be faster than his explanations, I promise

downloading
To start, go to this address.
Click the "Download JDK" button.
On the next page, accept the license agreement and click on the executable to download : jdk-7u51-windows-x64.exe in my case

installation
To start the installation, double-click the executable previously downloaded and let it guide you (or run it from a terminal).

And voilà ! Here is the first brick laid.


Next steps are installing android sdk, eclipse and ADT plugin or you can just download and install adt bundle wich provide you with :
  • Eclipse + ADT plugin
  • Android SDK Tools
  • Android Platform-tools
  • The latest Android platform
  • The latest Android system image for the emulator
Thanks to @FreakOde and @Chain Hacker
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 22 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
Rachid.Ala
Old
#3  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
Default Specific tools for Android : Android SDK

Specific tools for Android : Android SDK



We have just installed the JDK. So now you have the tools to develop a Java program. That said, an Android application is a particular Java program. You have the basic tools but not the specific tools to Android.
The tool you are missing is the SDKAndroid. It contains what you need, not to develop a standard Java program, but to develop an Android application.

Download the Android SDK and decompression

To download the Android SDK, go here and choose the file depending on your system.
As you can see, this file is compressed (zip for Windows and Mac or tgz for GNU / Linux). Unzip it where you want on your hard drive.
Remember exactly where you put the decompressed content. His path will be needed for the future. I recommend a safe place that will not move.
Personally, I put the contents of the zip in : C:\Android\android-sdk-windows

Add the SDK to PATH

Frequently asked question :
  • Ok I do not mind ... but what's the point?

No worries, before the manipulation, let me explain what it is.

What is the PATH?

When Windows (and this is the same principle on Mac or GNU / Linux) executes a command, it will search for it in specific directories...if it is not found in these directories, it considers that it does not know it and returns an error.
These specific directories are defined in what is called an environment variable: a variable (which, by definition, contains a value) accessible by Windows, everywhere, all the time.

The environment variable in question is PATH. To see its value, type the following in the command prompt in Windows:
Code:
echo %PATH%
You get a series of directories separated by ";".
It is in each of these directories that Windows searches for commands that you ask.

For example, the java command that we used earlier, is located in the C:\Windows\System32. Well I'm sure that this directory is in your PATH.
So now, we'll need that Windows knows where the new commands of the Android SDK are.
To add it to your path, execute this command :
Code:
set PATH=%PATH%;"path to android sdk"
So in my case I should execute
Code:
set PATH=%PATH%;C:\Android\android-sdk-windows
One last step is required to install the Android SDK. Do not worry, this is the easiest.

Installing Android platforms

The last step is to install at least one version of Android platform. According to the version of Android, APIs differ a little. At the time of this writing, the latest version of Android is version 4.4.2 (KitKat).

In the SDK directory, an executable is present: SDK Manager.exe. It's a small interface to manage versions of Android.

Run it to start the installation.

Among the available packages, choose the one you want:


Personally, I took everything! To be sure not to return.

On Mac and GNU/Linux, run from a terminal the "android" command in the directory / tools /.

The installation will take a few minutes (to me it was quite long since I took everything ...).

Once installation is complete, we can move to: Installing Eclipse.
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 18 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
Rachid.Ala
Old
(Last edited by Rachid.Ala; 23rd February 2014 at 02:41 PM.)
#4  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
Default Development environment : Eclipse

Development environment : Eclipse
As for the JDK, it may be that you already have Eclipse installed. If this is the case, great. You can now move on to the next section.

Otherwise, we will install it together.

Frequently asked question :
  • But in fact, what is Eclipse?

Eclipse is what we call an IDE or "Integrated Development Environment". This is a software that allows you to write programs more easily than simple Notepad in Windows (although it is possible). Besides the color code, it will bring you useful tools to compile your programs, debugging, etc.. It can be used to develop with any type of language, but we use it to make Java.

In addition, Eclipse is designed to be augmented with plug-ins. Thus, there is a plugin to develop Android applications ...! We'll see this in the next section.

Do you like it? Well let's go for the download and installation!

Download

First of all, go here and download Eclipse.


Choose 32 Bit or 64 Bit depending on your configuration (if you do not know , take 32 Bit ) .

On the next page , click on the big green arrow and ... wait for it to finish!

Installing Eclipse

Installing Eclipse really comes down to ... unzip the downloaded file!

You can place it anywhere on your hard drive. Personally ( on Windows ), the story that things are organized , I placed the folder " eclipse" in C:\Program Files ( or C :\Program depending on your version of Windows).

To start Eclipse, run the executable found in the directory "eclipse" (really , you guessed ? haha ) .

At startup, Eclipse will ask you the location of your Workspace. This is the directory that contains all the data related to your Java programs ( and thus your Android applications). Your source code will be stored there. This is the time , if you have a hard drive dedicated to your data, choose a directory therein. Otherwise, you can leave the default directory.

Well. Now , to develop our Android apps more easily , we need a small plugin.
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 16 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
Rachid.Ala
Old
(Last edited by Rachid.Ala; 22nd February 2014 at 10:52 PM.)
#5  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
Default A small plugin for Eclipse ADT

A small plugin for Eclipse ADT


As I briefly mentioned in the previous section, Eclipse has been designed to be able to receive plugins. Depending on whether you are Java, PHP, Javascript developer, (...) you have at your disposal a whole bunch of plugins that make your life easier.

The plugin for the Android application development is called ADT (Android development tools).

This time we will not download the plugin on the website of his publisher but directly from Eclipse.

ADT installation:

So first start Eclipse. In the "Help" menu, select "Install New Software ...".

In the window that opens, click on "Add ..."



Enter the following values​​:

Name: Android Plugin (for example)
Location: https://dl-ssl.google.com/android/eclipse/



Click OK, and wait a few seconds. Then you get this window:

http://img15.hostingpics.net/pics/156355window.jpg

Check the "Developer Tools" (which also tick the underlying tools) and click "Next". In the next window, click again on "Next"
In the next window, accept the terms of the license agreements and click "Finish"

During installation, you may get a security warning.
It tells you that Eclipse does not know what you are installing. But we, we know. So you can safely confirm by clicking "Ok".
At the end of the installation, you are prompted to restart Eclipse.
We have one small thing to do before moving on to the next part: Configure the ADT plugin. We will tell the plugin where the Android SDK.
In the "Window" menu, select "Preferences".
In the left pane, choose "Android"



Click "Browse ..." and choose the location of the SDK installed earlier in this tutorial.
Click "Apply" then "Ok". And voila!
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 15 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
Rachid.Ala
Old
(Last edited by Rachid.Ala; 22nd February 2014 at 10:52 PM.)
#6  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
Setting a virtual device (for testing)


To test our application, we use a virtual phone! Very convenient because it's cheap (well it's free )
Our virtual phone (yes you can have more than one if you want) are called "AVD", which leans "Android Virtual Device". It fits with what you want to do.

To begin, click on the little button as shown in the screenshot


This opens the AVD Manager. You should have no AVD yet. To create one, click "New ...". The following window is very important because it will define your "phone":


Configuring AVD could be a complete tutorial ... We therefore remain there for the moment.

Click the "Create AVD" button to complete the creation of your virtual mobile. You then end up with a beautiful AVD in your list. You can then close the "AVD Manager" to finally move to developing your first Android app
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 12 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
Rachid.Ala
Old
#7  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
New Android project


Finally! We reach the goal! Now it's good, our environment is in place. If you have not had any problems so far, you are ready to create a new Android project.

Create a new project

To begin, select from the menu "File" -> "New" -> "Project". With the ADT plugin, you now have the option to create a "Android Application Project". Select this option and click "Next". Then you come to the window, which allows you to initialize your Android project:


Here are the important information to fill :
  • Application name: This time it is the name of your application. That will appear in the very eyes of the user. You can put special characters if you wish.
  • Project Name : This is the name that will define your Eclipse project. You can put whatever you want but avoid spaces and other special characters.
  • Package name: By convention, the name of a package is in the form of a "url upside down ." You can put your personal domain name or whatever you want. As long as you do not post your application on a large scale , it is not very important.
  • Minimum required SDK: This is the minimum number required API to run your application. You can not install the application on a mobile with an older API.
  • Build Target : Select the version of Android on which you want to work .

Click "Finish" to create your project.

Application code

Code créé par défaut

Here is the code created by default:

Code:
package com.test.helloxda_university;

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);
	}
}
This code contains various elements:
  • Package Name
  • some "import" android.*** : Required libraries to the script
  • The class MainActivity that inherits from Activity.

I will not detail what constitutes the onCreate() method because we will now change it.

Code of our "Hello XDA-University!"

The purpose of our application is to say "Hello XDA-university". For this, we need a field "text". On Android, a field of this type is a TextView (Android, all the elements are based on the View class).
We create our TextView with the following line:
Code:
TextView tv = new TextView(this);
This requires to import the corresponding library:
Code:
import android.widget.TextView;
we apply a text to our TextView:
Code:
tv.setText("Hello XDA-university !");
Finally, we apply the TextView to the current view:
Code:
setContentView(tv);
You will notice the following line:
Code:
super.onCreate(savedInstanceState);
It can tell Android how to start the application (we call the onCreate method of the parent class by passing the savedInstanceState, which is the previous state of the application).

Complete code (what a code!) of our application:

Code:
package com.test.helloxda_university;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        TextView tv = new TextView(this);
        tv.setText("Hello XDA-university !");
        
        setContentView(tv);
    }
}
With this, our text should be able to be displayed without issues.

Launch the application in the AVD

To launch the application, choose "Run" from the menu ... "Run"
The AVD will then take a few moments to start: you will see the letters "ANDROID" in gray on black background. This might take some time depending on your configuration.

Once the AVD is fully started, your application will launch automatically:


Ta daaaa!

Well I told you: nothing transcendent ... But we must start somewhere.

We arrive at the end of this tutorial dedicated to the establishment of an environment of Android development. This is not the only way.

There are many steps to finally not much except a little "Hello!" ... But you are now armed to get started with Android. I leave it to you to learn the art of developing Android applications: Sights, Activities, Intents, etc. ....

I hope you learned something with me and if you have any questions do not hesitate to ask me.
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 24 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
wfarid
Old
#8  
Junior Member
Thanks Meter 4
Posts: 15
Join Date: Aug 2012
this is a great writeup. Thank you!
The Following User Says Thank You to wfarid For This Useful Post: [ Click to Expand ]
 
Rachid.Ala
Old
#9  
Rachid.Ala's Avatar
Senior Member - OP
Thanks Meter 379
Posts: 341
Join Date: Aug 2012
Location: Rabat
Quote:
Originally Posted by wfarid View Post
this is a great writeup. Thank you!
You are welcome mate
Device : Galaxy S II
Android : 4.4.4
Runtime Environment : ART
ROM : SlimKat unofficial builds by Fusionjack (always latest)
Kernel : Dorimanx beta mali v3 V10.43 (always latest)
Modem : XXLS8
Talk is cheap, show me the code !
If I helped you out and you'd like to show your appreciation, click the thanks button
The Following 4 Users Say Thank You to Rachid.Ala For This Useful Post: [ Click to Expand ]
 
zamzameir
Old
#10  
zamzameir's Avatar
Senior Member
Thanks Meter 191
Posts: 280
Join Date: Sep 2013
Location: Kuala Lumpur

 
DONATE TO ME
subscribed for now. I might need this later. TQ

Hammerhead
Mako
Maguro

" Nexus ; lead the way "

The Following User Says Thank You to zamzameir For This Useful Post: [ Click to Expand ]
Tags
android, application, create, environment
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes