Android Tutorial for Beginners - Hello Android

1- Introduction

This document is guidelines on programming Android for beginners. It uses Android Studio tool.   Ensure that you have installed Android Studio, if you have not already installed you can view the instructions at:

2- Running Android Studio

In the first run, Android Studio ask you:
Selecting a theme that you like:
At the first run, Android needs to download some components. You need to wait until the download and install process completed. 

3- Creating your first project

Firstly you need to create and run successfully a first project.
Enter:
  • Application name: HelloAndroid
  • Company Domain: o7planning.org
  • Project location: F:\ANDROID_TUTORIAL\HelloAndroid
This is the screen of Android Studio:
Project has been created.

4- Running Application

Screen simulation has shown the phone, and write out the words "Hello World".
If running error (or simulated device does not open), or you see the message below, that means you have not installed Intel Emulator Accelerator, you need to install it.
You need to Install Intel HAXM:
If you see the Android Simulation screen too large, you may need some configuration to eliminate this discomfort:

5- Explore Nexus_5_23_x86

Now you need to become familiar with the simulator phone  which your applications is being run on.
​Slider below shows you where your applications has been deployed on phone. 
  • Slider:

6- Overview of Android Project Structure

Here is the image of your project on the Android Studio:
File or directory Description
AndroidManifest.xml This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.
java This contains the .java source files for your project. By default, it includes an MainActivity.java source file having an activity class that runs when your app is launched using the app icon.
res/drawable The previous Android version used this folder to store images, the current version replaced by mipmap folder for images. This directory is almost no longer used.
res/layout This is a directory for files that define your app's user interface.
res/menu The menu folder with XML of the items that will appear on the Action Bar
res/mipmap Used contains 'mipmap' images
res/values This is a directory for other various XML files that contain a collection of resources, such as strings and colours definitions.

AndroidManifest.xml

Whatever component you develop as a part of your application, you must declare all its components in a AndroidManifest.xml. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS. For example, a default manifest file will look like as following file
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.o7planning.helloandroid" >

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

res/mipmap

You need to understand  more about the images in the mipmap folder, see the image below, there is an image file in the mipmap folder.
One of the problems I occasionally come across while using Android apps is that I see images that are blurry, pixelated, or stretched awkwardly. The solution to this problem is to create a drawable folder for each of the densities that Android supports, and place the correctly scaled image inside each folder. There are 6 different density ranges (measured in dots per inch) that Android supports:
  1. ldpi: Low-density screens; approximately 120dpi
  2. mdpi: Medium-density (on traditional HVGA) screens; approximately 160dpi
  3. hdpi: High-density screens; approximately 240dpi
  4. xhdpi: Extra high-density screens; approximately 320dpi. Added in API Level 8
  5. nodpi: This can be used for bitmap resources that you do not want to be scaled to match the device density
  6. tvdpi: Screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a “primary” density group. It is mostly intended for televisions and most apps shouldn’t need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. This qualifier was introduced with API level 13.

7- Android Activity Lifecycle

Firstly, what is Activity?
Activity is a Java code that supports a screen or UI. In other words, building block of the user interface is the activity. Activity class is a pre-defined class in Android and every application which has UI must inherit it to create window.
For example, MainActivity class is an Activity that extends from available Activity in libraries. The figure below illustrates the lifecycle of an Activity.

A lifecycle of Activity describes of the processes of an Activity since it started launching, until the applications is shutdown. Including processes that Activity stopped  temporarily, resume, ..

To more easily understood, you can modify the code of MainActivity class, override the onStart(), onResume(),..  methods, Add on the message which demonstrates method has been run.  Rerun the application and see what happens
MainActivity.java
package org.o7planning.helloandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

  public static final String TAG ="MyMessage";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // Print Log
      Log.i(TAG,"onCreate");
  }

  @Override
  protected void onStop() {
      super.onStop();
      // Print Log
      Log.i(TAG,"onStop");
  }

  @Override
  protected void onDestroy() {
      super.onDestroy();
      // Print Log
      Log.i(TAG,"onDestroy");
  }

  @Override
  protected void onPause() {
      super.onPause();
      // Print Log
      Log.i(TAG,"onPause");
  }

  @Override
  protected void onResume() {
      super.onResume();
      // Print Log
      Log.i(TAG,"onResume");      }

  @Override
  protected void onStart() {
      super.onStart();
       // Print Log
       Log.i(TAG,"onStart");
  }

  @Override
  protected void onRestart() {
      super.onRestart();
      // Print Log
      Log.i(TAG,"onRestart");      }

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

      // Print Log
      Log.i(TAG, "onCreateOptionsMenu");
      return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Print Log
      Log.i(TAG, "onOptionsItemSelected");

      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }

      return super.onOptionsItemSelected(item);
  }
}
Rerun your application, and you see the messages recorded on logcat window.
You can setup the filter to logcat only display your message.
Enter:
  • Filter Name: My Filter
  • Log Tag (regex): MyMessage
Now, logcat window only displays  your messages.
Your phone screen:

8- How does Android Application run?

You've  run successfully first Android app, and now we will look back at how Android has been run since the application is called.
What you see on the Project window are not all components involved  creating your application. There are components that are generated automatically by the compiler program. And it does not display on Project window.  For example, based on the structure of the source files in your project,  the compiler creates a R.java source file that defines constants for the resources on the project.
To view the  R.java file on   Android Studio, open  MainActivity class, right click on R.layout.main_activity and select Go To/Implementation (s)
The constants defined in class R.java are created corresponding to the resources on the Project:
So somewhere in the Project, you can use the constants to refer resources in the project. Such as,  R.layout.activity_main  is a constant which refers activity_main.xml  file in the   res/layout folder.

The operating principle:

9- What makes the user interface?

Maybe now you are thinking that main_ativity.xml is the file that creates application interface. That's right  50%. Substantially,  main_activity.xml is a file that defines the entities involved in the application's interface, and how they are arranged on the screen. Activity will read this file and draw up the application interface. Thus, the interface of applications essentially created by Activity
Here's the code Activity read main_activity.xml file to create the interface.
MainActivity.java
package org.o7planning.helloandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

   public static final String TAG ="MyMessage";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       // Read activity_main.xml to draw user interface.
       setContentView(R.layout.activity_main);
   }
   
   // .....
}  
Activity may not need to read xml file to create the application interface:
MainActivity.java
package org.o7planning.helloandroid;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

   public static final String TAG ="MyMessage";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       // Layout
       RelativeLayout layout = new RelativeLayout(this);
       layout.setBackgroundColor(Color.GREEN);

       // Create Button
       Button button = new Button(this);
       button.setText("My Button");

       // Add button to Layout.
       layout.addView(button);

       // Set content
       setContentView(layout);
   }

   // ......
}
And rerun the application:
Current Android Studio version do not support you in designing Intuitive Interface on Java. However, it supports effectively in designing Intuitive Interface on  xml files, Android Studio will generate the XML code for you.  Actually, interface design on  XML will make your applications maintain more easily. 
Back to activity_main.xml, you can easily design application interface:

10- Next tutorial