Android Tips
Android Tips
Android is a software package and linux based operating system for mobile devices such as
tablet computers and smartphones.
It is developed by Google and later the OHA (Open Handset Alliance). Java language is
mainly used to write the android code even though other languages can be used.
The goal of android project is to create a successful real-world product that improves the
mobile experience for end users.
There are many code names of android such as Lollipop, Kitkat, Jelly Bean, Ice cream
Sandwich, Froyo, Ecliar, Donut etc which is covered in next page.
It was established on 5th November, 2007, led by Google. It is committed to advance open
standards, provide services and deploy handsets using the Android Plateform.
Features of Android
After learning what is android, let's see the features of android. The important features of
android are given below:
1) It is open-source.
3) There are a lot of mobile applications that can be chosen by the consumer.
4) It provides many interesting features like weather details, opening screen, live RSS
(Really Simple Syndication) feeds etc.
It provides support for messaging services(SMS and MMS), web browser, storage (SQLite),
connectivity (GSM, CDMA, Blue Tooth, Wi-Fi etc.), media, handset layout etc.
Entertainment
Tools
Communication
Productivity
Personalization
Social
History of Android
The history and versions of android are interesting to know. The code names of
android ranges from A to J currently, such
as Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb,
Ice Cream Sandwitch,Jelly Bean, KitKat and Lollipop. Let's understand the
android history in a sequence.
1) Initially, Andy Rubin founded Android Incorporation in Palo Alto, California,
United States in October, 2003.
2) In 17th August 2005, Google acquired android Incorporation. Since then, it is in
the subsidiary of Google Incorporation.
3) The key employees of Android Incorporation are Andy Rubin, Rich Miner, Chris
White and Nick Sears.
4) Originally intended for camera but shifted to smart phones later because of low
market for camera only.
5) Android is the nick name of Andy Rubin given by coworkers because of his love to
robots.
6) In 2007, Google announces the development of android OS.
7) In 2008, HTC launched the first android mobile.
1.5 Cupcake 3
1.6 Donut 4
2.1 Eclair 7
2.2 Froyo 8
4.4 KitKat 19
5.0 Lollipop 21
Android Architecure
android architecture or Android software stack is categorized into five parts:
1. linux kernel
3. Android Runtime
4. Application Framework
5. Applications
2) Native Libraries
On the top of linux kernel, their are Native libraries such as WebKit, OpenGL, FreeType,
SQLite, Media, C runtime library (libc) etc.
The WebKit library is responsible for browser support, SQLite is for database, FreeType for
font support, Media for playing and recording audio and video formats.
3) Android Runtime
In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is
responsible to run android application. DVM is like JVM but it is optimized for mobile
devices. It consumes less memory and provides fast performance.
4) Android Framework
On the top of Native libraries and android runtime, there is android framework. Android
framework includes Android API's such as UI (User Interface), telephony, resources,
locations, Content Providers (data) and package managers. It provides a lot of classes and
interfaces for android application development.
5) Applications
On the top of android framework, there are applications. All applications such as home,
contact, settings, games, browsers are using android framework that uses android runtime
and libraries. Android runtime and native libraries are using linux kernal.
An android component is simply a piece of code that has a well defined life cycle e.g.
Activity, Receiver, Service etc.
The core building blocks or fundamental components of android are activities, views,
intents, services, content providers, fragments and AndroidManifest.xml.
Activity
An activity is a class that represents a single screen. It is like a Frame in AWT.
View
A view is the UI element such as button, label, text field etc. Anything that you see is a
view.
Intent
Intent is used to invoke components. It is mainly used to:
Launch an activity
Display a web page
Broadcast a message
For example, you may write the following code to view the webpage.
Service
Service is a background process that can run for a long time.
There are two types of services local and remote. Local service is accessed from within the
application whereas remote service is accessed remotely from other applications running on
the same device.
Content Provider
Content Providers are used to share data between the applications.
Fragment
Fragments are like parts of activity. An activity can display one or more fragments on the
screen at the same time.
AndroidManifest.xml
It contains informations about activities, content providers, permissions etc. It is like the
web.xml file in Java EE.
Android Virtual Device (AVD)
It is used to test the android application without the need for mobile or tablet etc. It can be
created in different configurations to emulate different types of real devices.
Android Emulator
Android Emulator is used to run, debug and test the android application. If you don't have
the real device, it can be the best way to run, debug and test the application.
The emulator tool enables you to start the emulator from the command line. You need to
write:
In case of Eclipse IDE, you can create AVD by Window menu > AVD Manager > New.
In the given image, you can see the android emulator, it displays the output of the hello
android example.
Install Android
Android supports java, c++, c# etc. language to develop android applications. Java is
the officially supported language for android. All the android examples of this site is
developed using Java language and Eclipse IDE.
Here, we are going to tell you, the required softwares to develop android applications
using Eclipse IDE.
1. By ADT Bundle
Eclipse IDE
Android SDK
Eclipse Plugin
If you download the ADT from android site, you don't need to have eclipse IDE, android
SDK and eclipse Plugin because it is already included in adt bundle.
If you have downloaded the ADT bundle, unjar it, go to eclipse IDE and start the eclipse
by clicking on the eclipse icon. You don't need to do any extra steps here.
If eclipse is not started, paste the JRE directory inside the eclipse directory.
Now double click on the exe file, it will be installed. I am using the android 2.2 version here.
1) Start the eclipse IDE, then select Help > Install new software...
5) click finish
2. Now select the android from the left panel. Here you may see a dialog box asking if
you want to send the statistics to the google. Click proceed.
3. Click on the browse button and locate your SDK directory e.g. my SDK location is
C:\Program Files\Android\android-sdk .
3. Now a dialog appears, write the AVD name e.g. myavd. Now choose the target
android version e.g. android2.2.
package com.example.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
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).
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:
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.
Java Source Code
Let's see the java source file created by the Eclipse IDE:
File: MainActivity.java
package com.example.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {//(1)
@Override
protected void onCreate(Bundle savedInstanceState) {//(2)
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.
File: activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/androi
d"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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
File: R.java
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 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:
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.
It also declares the android api that the application is going to use.
This is the required xml file for all the android application and located inside the root
directory.
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>
<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.
<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.
<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.
Let's see the android R.java file. It includes a lot of static nested classes such as menu, id,
layout, attr, drawable, string etc.
1. @Override
2. protected void onCreate(Bundle savedInstanceState) {
3. super.onCreate(savedInstanceState);
4.
5. requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title not th
e 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. }
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. }
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:
Value Description
unspecified It is the default value. In such case, system chooses the orientation.
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
Output:
<<prevnext>>