How to make android apps?

How to make android apps

In this page, you will know how to create the simple hello android application. We are creating the simple example of android using the Eclipse IDE. For creating the simple example:

  1. Create the new android project
  2. Write the message (optional)
  3. Run the android application

Hello Android Example

You need to follow the 3 steps mentioned above for creating the Hello android application.

1) Create the New Android project

For creating the new android studio project:

1) Select Start a new Android Studio project

hello android example

2) Provide the following information: Application name, Company domain, Project location and Package name of application and click next.

hello android example 2

3) Select the API level of application and click next.

hello android example 3

4) Select the Activity type (Empty Activity).

hello android example 4

5) Provide the Activity Name and click finish.

hello android example 5

After finishing the Activity configuration, Android Studio auto generates the activity class and other required configuration files.

Now an android project has been created. You can explore the android project and see the simple program, it looks like this:

hello android example 5

2) Write the message

File: activity_main.xml

Android studio auto generates code for activity_main.xml file. You may edit this file according to your requirement.

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  4.     xmlns:tools=”http://schemas.android.com/tools”  
  5.     android:layout_width=”match_parent”  
  6.     android:layout_height=”match_parent”  
  7.     tools:context=”first.javatpoint.com.welcome.MainActivity”>  
  8.   
  9.     <TextView  
  10.         android:layout_width=”wrap_content”  
  11.         android:layout_height=”wrap_content”  
  12.         android:text=”Hello Android!”  
  13.         app:layout_constraintBottom_toBottomOf=”parent”  
  14.         app:layout_constraintLeft_toLeftOf=”parent”  
  15.         app:layout_constraintRight_toRightOf=”parent”  
  16.         app:layout_constraintTop_toTopOf=”parent” />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  
  19. }  

File: MainActivity.java

  1. package first.javatpoint.com.welcome;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5.   
  6. public class MainActivity extends AppCompatActivity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.     }  
  12. }  

To understand the first android application, visit the next page (internal details of hello android example).


3) Run the android application

To run the android application, click the run icon on the toolbar or simply press Shift + F10.

hello android example 7

The android emulator might take 2 or 3 minutes to boot. So please have patience. After booting the emulator, the android studio installs the application and launches the activity. You will see something like this:

hello android example 8

Internal Details of Hello Android Example

Here, we are going to learn the internal details or working of hello android example.

Android application contains different components such as java source code, string resources, images, manifest file, apk file etc. Let’s understand the project structure of android application.

Project Structure of Hello Android example

Java Source Code

Let’s see the java source file created by the Eclipse IDE:File: MainActivity.java

  1. package com.example.helloandroid;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.widget.TextView;  
  6. public class MainActivity extends Activity {//(1)  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {//(2)  
  9.         super.onCreate(savedInstanceState);  
  10.                 
  11.         setContentView(R.layout.activity_main);//(3)  
  12.     }  
  13.     @Override  
  14.     public boolean onCreateOptionsMenu(Menu menu) {//(4)  
  15.         // Inflate the menu; this adds items to the action bar if it is present.  
  16.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  17.         return true;  
  18.     }  
  19. }  

(1) Activity is a java class that creates and default window on the screen where we can place different components such as Button, EditText, TextView, Spinner etc. It is like the Frame of Java AWT.

It provides life cycle methods for activity such as onCreate, onStop, OnResume etc.

(2) The onCreate method is called when Activity class is first created.

(3) The setContentView(R.layout.activity_main) gives information about our layout resource. Here, our layout resources are defined in activity_main.xml file.File: activity_main.xml

  1. <RelativeLayout xmlns:androclass=”http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=”http://schemas.android.com/tools”  
  3.     android:layout_width=”match_parent”  
  4.     android:layout_height=”match_parent”  
  5.     tools:context=”.MainActivity” >  
  6.     <TextView  
  7.         android:layout_width=”wrap_content”  
  8.         android:layout_height=”wrap_content”  
  9.         android:layout_centerHorizontal=”true”  
  10.         android:layout_centerVertical=”true”  
  11.         android:text=”@string/hello_world” />  
  12. </RelativeLayout>  

As you can see, a textview is created by the framework automatically. But the message for this string is defined in the strings.xml file. The @string/hello_world provides information about the textview message. The value of the attribute hello_world is defined in the strings.xml file.File: strings.xml

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <resources>  
  3.     <string name=”app_name”>helloandroid</string>  
  4.     <string name=”hello_world”>Hello world!</string>  
  5.     <string name=”menu_settings”>Settings</string>  
  6. </resources>  

You can change the value of the hello_world attribute from this file.


Generated R.java file

It is the auto-generated file that contains IDs for all the resources of res directory. It is generated by aapt(Android Asset Packaging Tool). Whenever you create any component on activity_main, a corresponding ID is created in the R.java file which can be used in the Java Source file later.File: R.java

  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY. 
  2.  * 
  3.  * This class was automatically generated by the 
  4.  * aapt tool from the resource data it found.  It 
  5.  * should not be modified by hand. 
  6.  */  
  7. package com.example.helloandroid;  
  8. public final class R {  
  9.     public static final class attr {  
  10.     }  
  11.     public static final class drawable {  
  12.         public static final int ic_launcher=0x7f020000;  
  13.     }  
  14.     public static final class id {  
  15.         public static final int menu_settings=0x7f070000;  
  16.     }  
  17.     public static final class layout {  
  18.         public static final int activity_main=0x7f030000;  
  19.     }  
  20.     public static final class menu {  
  21.         public static final int activity_main=0x7f060000;  
  22.     }  
  23.     public static final class string {  
  24.         public static final int app_name=0x7f040000;  
  25.         public static final int hello_world=0x7f040001;  
  26.         public static final int menu_settings=0x7f040002;  
  27.     }  
  28.     public static final class style {  
  29.         /**  
  30.         Base application theme, dependent on API level. This theme is replaced 
  31.         by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
  32.             Theme customizations available in newer API levels can go in 
  33.             res/values-vXX/styles.xml, while customizations related to 
  34.             backward-compatibility can go here. 
  35.          Base application theme for API 11+. This theme completely replaces 
  36.         AppBaseTheme from res/values/styles.xml on API 11+ devices. 
  37.   API 11 theme customizations can go here.  
  38.         Base application theme for API 14+. This theme completely replaces 
  39.         AppBaseTheme from BOTH res/values/styles.xml and 
  40.         res/values-v11/styles.xml on API 14+ devices. 
  41.  API 14 theme customizations can go here.  
  42.          */  
  43.         public static final int AppBaseTheme=0x7f050000;  
  44.         /**  Application theme.  
  45.  All customizations that are NOT specific to a particular API-level can go here.  
  46.          */  
  47.         public static final int AppTheme=0x7f050001;  
  48.     }  
  49. }  

APK File

An apk file is created by the framework automatically. If you want to run the android application on the mobile, transfer and install it.


Resources

It contains resource files including activity_main, strings, styles etc.


Manifest file

It contains information about package including components such as activities, services, content providers etc.

For more information about manifest file visit here: AndroidManifest.xml file.

About Author /

Hi everyone. This is Vishal Yadav, & I'm an Android developer at Techwarezen pvt. ltd. and here I'm sharing my experience and knowledge with you which I have learnt. Hope It may help you improving yourself. Thank you! *Note - Please specify if I uploaded any wrong content.*

Leave a Comment

Your email address will not be published.

Start typing and press Enter to search