Thursday, 23 April 2015

How to Create new Application

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 project:
1) Select File > New > Project...
2) Select the android project and click next
hello android example
3) Fill the Details in this dialog box and click finish
hello android example
Now an android project have been created. You can explore the android project and see the simple program, it looks like this:
hello android example

2) Write the message

For writing the message we are using the TextView class. Change the onCreate method as:
  1. TextView textview=new TextView(this);  
  2. textview.setText("Hello Android!");  
  3. setContentView(textview);  
Let's see the full code of MainActivity.java file.
  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 {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         TextView textview=new TextView(this);  
  11.         textview.setText("Hello Android!");  
  12.         setContentView(textview);  
  13.     }  
  14.     @Override  
  15.     public boolean onCreateOptionsMenu(Menu menu) {  
  16.         // Inflate the menu; this adds items to the action bar if it is present.  
  17.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  18.         return true;  
  19.     }  
  20. }  

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: Right click on your project > Run As.. > Android Application
The android emulator might take 2 or 3 minutes to boot. So please have patience. After booting the emulator, the eclipse plugin installs the application and launches the activity. You will see something like this:
hello android example


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.

Dalvik Virtual Machine | DVM

As we know the modern JVM is high performance and provides excellent memory management. But it needs to be optimized for low-powered handheld devices as well.
The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices. It optimizes the virtual machine for memory, battery life and performance.
Dalvik is a name of a town in Iceland. The Dalvik VM was written by Dan Bornstein.
The Dex compiler converts the class files into the .dex file that run on the Dalvik VM. Multiple class files are converted into one dex file.
Let's see the compiling and packaging process from the source file:
Dalvik Virtual Machine Flow
The javac tool compiles the java source file into the class file.
The dx tool takes all the class files of your application and generates a single .dex file. It is a platform-specific tool.
The Android Assets Packaging Tool (aapt) handles the packaging process.


AndroidManifest.xml file in android

The AndroidManifest.xml file contains information of your package, including components of the application such as activities, services, broadcast receivers, content providers etc.
It performs some other tasks also:
  • It is responsible to protect the application to access any protected parts by providing the permissions.
  • It also declares the android api that the application is going to use.
  • It lists the instrumentation classes. The instrumentation classes provides profiling and other informations. These informations are removed just before the application is published etc.
This is the required xml file for all the android application and located inside the root directory.
A simple AndroidManifest.xml file looks like this:
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.javatpoint.hello"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="15" />  
  9.   
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name=".MainActivity"  
  16.             android:label="@string/title_activity_main" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.   
  20.                 <category android:name="android.intent.category.LAUNCHER" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.     </application>  
  24.   
  25. </manifest>  

Elements of the AndroidManifest.xml file

The elements used in the above xml file are described below.

<manifest>

manifest is the root element of the AndroidManifest.xml file. It has package attribute that describes the package name of the activity class.

<application>

application is the subelement of the manifest. It includes the namespace declaration. This element contains several subelements that declares the application component such as activity etc.
The commonly used attributes are of this element are icon, label, theme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components.
android:theme represents a common theme for all the android activities.

<activity>

activity is the subelement of application and represents an activity that must be defined in the AndroidManifest.xml file. It has many attributes such as label, name, theme, launchMode etc.
android:label represents a label i.e. displayed on the screen.
android:name represents a name for the activity class. It is required attribute.

<intent-filter>

intent-filter is the sub-element of activity that describes the type of intent to which activity, service or broadcast receiver can respond to.

<action>

It adds an action for the intent-filter. The intent-filter must have at least one action element.

<category>

It adds a category name to an intent-filter.


Android Hide Title Bar Example

In this example, we are going to explain how to hide the title bar and how to display content in full screen mode.
The requestWindowFeature(Window.FEATURE_NO_TITLE) method of Activity must be called to hide the title. But, it must be coded before the setContentView method.
  1.     @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.           
  5.         requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title not the title bar  
  6.           
  7.         setContentView(R.layout.activity_main);  
  8.         
  9.     }  
  10. }  
The setFlags() method of Window class is used to display content in full screen mode. You need to pass theWindowManager.LayoutParams.FLAG_FULLSCREEN constant in the setFlags method.
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.       
  5.     requestWindowFeature(Window.FEATURE_NO_TITLE);  
  6.     //code that displays the content in full screen mode  
  7.     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  8.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);//int flag, int mask  
  9.       
  10.     setContentView(R.layout.activity_main);  
  11.     
  12. }  

Android Hide Title Bar Example

Let's see the full code to hide the title bar in android.

activity_main.xml

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.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world" />  
  15.   
  16. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.javatpoint.hidetitle;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.Window;  
  7. import android.view.WindowManager;  
  8.   
  9. public class MainActivity extends Activity {  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.           
  15.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  16.        
  17.       /*this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
  18.                   WindowManager.LayoutParams.FLAG_FULLSCREEN);//int flag, int mask 
  19.       */  
  20.         setContentView(R.layout.activity_main);  
  21.           
  22.     }  
  23.   
  24.   
  25. }  


Output: Hiding the Title Only

android hide title bar example output 1

Output: Hiding the TitleBar and enabling FullScreen

android hide title bar example output 2

Android Screen Orientation Example

The screenOrientation is the attribute of activity element. The orientation of android activity can be portrait, landscape, sensor, unspecified etc. You need to define it in the AndroidManifest.xml file. For example:
  1. <activity  
  2.             android:name="com.example.screenorientation.MainActivity"  
  3.             android:label="@string/app_name"   
  4.             android:screenOrientation="landscape"  
  5.             >  
The common values for screenOrientation attribute are as follows:
ValueDescription
unspecifiedIt is the default value. In such case, system chooses the orientation.
portraittaller not wider
landscapewider not taller
sensororientation is determined by the device orientation sensor.

Android landscape mode screen orientation example

activity_main.xml

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.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/button1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_marginLeft="66dp"  
  16.         android:layout_marginTop="73dp"  
  17.         android:text="Button"  
  18.         android:onClick="onClick"  
  19.          />  
  20.   
  21.     <EditText  
  22.         android:id="@+id/editText1"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_centerHorizontal="true"  
  26.         android:ems="10" />  
  27.   
  28. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.f;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.   
  11. public class MainActivity extends Activity{  
  12.     EditText editText1;  
  13.     Button button1;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         editText1=(EditText)findViewById(R.id.editText1);  
  20.         button1=(Button)findViewById(R.id.button1);  
  21.     }  
  22.     public void onClick(View v) {  
  23.         editText1.setText("O android");  
  24.     }  
  25. }  

AndroidManifest.xml

File: AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.screenorientation"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="16" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.screenorientation.MainActivity"  
  18.             android:label="@string/app_name"   
  19.             android:screenOrientation="landscape"  
  20.             >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  

Output:

android screen orientation example output 1

No comments:

Post a Comment