My first Android application with Android Studio

Share this idea :

My first Android app

Nothing complicated, Google prepared the work. Just follow the guide.

We launch Android Studio and create a new application by clicking New project :

Settings-Android-Studio-02

Then you can choose some options to simplify our work.

BL_HelloWorld_01

On this screen you indicate :

  • The name of the application.
  • The “domain of the company” : All Java programs are identified by a “technical” name that looks like a web address. You can enter what you want, set the domain of your website if you have one.
  • The project location on your computer.

Next!

BL_HelloWorld_02

This screen is used to simplify the choice of the Android version that will support your application. Don’t change anything for now. Next!

BL_HelloWorld_03

Android Studio offers several skeleton of application to simplify our work. For this first tutorial we chose “Blank Activity”. Next!

BL_HelloWorld_04

On this screen you choose:

  • The name of the main activity,
  • The name of the layout,
  • Title to display.

At first we don’t touch anything. We will see later what’s the point. Finish!

You should now see the list of files in your new project.

If this is your first project with Android Studio, you may see this message at the bottom of the screen:

BL_HelloWorld_05

Android Studio automatically downloads Gradle files. Gradle is essential to use Android Studio and it’s free.

After this download, you can compile with this icon. This step allows us to verify that there is no error in our program.

BL_HelloWorld_09b

If the compilation is ok, you will have this message at the bottom of Android Studio screen :

BL_HelloWorld_11

If you have an error, you might see this:

BL_HelloWorld_10

If this is the case, you have to read details of errors that appears just above, and correct it. You must be patient if you are a beginner 🙂

When you no longer have error, connect your Android device (phone or tablet) to the computer with a USB cable and launch your application by clicking on this icon.

BL_HelloWorld_09c

Android Studio asked to choose the device on which to install the application. In the example below, a Samsung Galaxy S3.

BL_HelloWorld_12

Ok! You should see the “Hello World” application launched on your device 🙂

And if Android Studio doesn’t find my Android device ?

If Android Studio doesn’t list your device, it’s probably because the “developer mode” is not enabled on the device. This is an option to activate developer features on your phone or tablet.

Check out options of your device.

Sometimes you also have to allow installation of application from external source (also an option on the device). Your first app come from your computer and not from Google Play store.

Some explanations before proceeding further

Android concepts

This first exercise uses the concepts of activity, view and layout.

An activity is what contains intelligence, ie calculations, file access, memory access, camera access… On our phones, one activity is running at a time. When switching from one screen to another, it is usually 2 differents activities.

Android manages the activity as a stack of plates. When using the back button of the device, Android stops the first activity of the battery (the one that was “in progress”), the second runs and become the first activity of the stack.

An activity runs but doesn’t display anything. It calls for a view.

A view manages the display. It displays the results of calculations or to get user inputs. It uses widgets, such as a button, a text, an image…

To learn how to put all these visual objects, the view is based on a layout.

A layout is simply a file which says how to show the visual objects relative to others.

In summary, we choose the layout with widgets (visual components), the view is responsible for displaying the whole, and the view is calling by the activity that does the calculations and provides the link with the Android system. Easy 🙂

The concept is good, but in real life, where are these files ? 🙂

The project files

In the Project Explorer, you can see that Android Studio has created many files and directories.

BL_HelloWorld_06

This organization is simple and very effective.

The main folders:

  • java: contains the source code written in java ordered by packet. In our example, the package com.beanslegacy.sample.helloworld contains one activity called HelloWorldActivity.

BL_HelloWorld_07

  • res: contains all the resources of our application ie images, colors, text in any languages, themes… and most GUIs.

BL_HelloWorld_08

Res contains some sub-directories:

  • drawable- *: they are many sub-directories because there is one per type of screen (or type of screen density). You put such images in your application adapted to each type of device. We will see this later.
  • layout: the default directory for graphical interfaces. We will see later that we can create others layout* sub-directories, such as when you want to do a different interface between portrait and landscape mode.
  • menu: it allows you to define the menus that are offered by the different activities.
  • values: this sub-directory contains the default dimens.xml files to centralize sizing information such as lines and spaces between other elements of the graphical interface. strings.xml contains the strings of your application (text menu, text buttons…) and styles.xml that defines the style of your application (Android offers styles to easyly start your application, we will see this later).

With the same principle, it’s advisable to create the following files in the directory values​​:

  • colors.xml to centralize all codes of color you use for your application.
  • fonts.xml to centralize information about fonts.
  • array.xml to centralize lists of values like a list of country…

The great strength of this organization is that it allows for the same project all possible versions of the application:

  • The tablet versions, telephone, watch TV…
  • All languages
  • All Android versions
  • All types of screen

We will go into detail later if necessary.

For now don’t worry: the first objective is to create a simple phone application in portrait mode.

By the way, have you tried to turn your device to landscape mode with your application? 😉

The source code of this simple project is available on the GitHub repository of Bean’s Legacy.

This post is also available in 2 - Lire cette page en français.

Leave a Reply

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

*