Академический Документы
Профессиональный Документы
Культура Документы
There are many code names of android such as Lollipop, Kitkat, Jelly
Bean, Ice cream Sandwich, Froyo, Ecliar, Donut etc
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.
There are many android applications in the market. The top categories
are:
o Entertainment
o Tools
o Communication
o Productivity
o Personalization
o Music and Audio
o Social
o Media and Video
o Travel and Local etc.
1.5 Cupcake 3
1.6 Donut 4
2.1 Eclair 7
2.2 Froyo 8
4.4 KitKat 19
5.0 Lollipop 21
6.0 Marshmallow 23
Android Architecture
1. linux kernel
2. native libraries (middleware),
3. Android Runtime
4. Application Framework
5. Applications
1) Linux kernel
2) Native Libraries
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
Activity
View
A view is the UI element such as button, label, text field etc. Anything
that you see is a view.
Intent
For example, you may write the following code to view the webpage.
1. Intent intent=new Intent(Intent.ACTION_VIEW);
2. intent.setData(Uri.parse("http://www.google.com"));
3. startActivity(intent);
Service
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
Fragment
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
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:
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 pr
esent.
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.
(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>
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.
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
APK File
Resources
Manifest file
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.
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/an
droid"
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.LAU
NCHER" />
21. </intent-filter>
22. </activity>
23. </application>
24.
25. </manifest>
AndroidManifest.xml file in android
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/an
droid"
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.LAU
NCHER" />
21. </intent-filter>
22. </activity>
23. </application>
24.
25. </manifest>
The elements used in the above xml file are described below.
<manifest>
<application>
<intent-filter>
<action>
<category>
There are many more items which you use to build a good Android
application. Apart from coding for the application, you take care of
various other resources like static content that your code uses, such
as bitmaps, colors, layout definitions, user interface strings,
animation instructions, and more. These resources are always
maintained separately in various sub-directories under res/ directory
of the project.
This tutorial will explain you how you can organize your application
resources, specify alternative resources and access them in your
applications.
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
res/
drawable/
icon.png
layout/
activity_main.xml
info.xml
values/
strings.xml
1 anim/
2 color/
XML files that define a state list of colors. They are saved in
res/color/ and accessed from the R.color class.
3 drawable/
Image files like .png, .jpg, .gif or XML files that are compiled into
bitmaps, state lists, shapes, animation drawable. They are
saved in res/drawable/ and accessed from the R.drawable class.
4 layout/
XML files that define a user interface layout. They are saved in
res/layout/ and accessed from the R.layout class.
5 menu/
6 raw/
7 values/
8 xml/
There are following four main components that can be used within an
Android application −
1 Activities
They dictate the UI and handle the user interaction to the smart
phone screen.
Services
2
They handle background processing associated with an
application.
Broadcast Receivers
3
They handle communication between Android OS and
applications.
Content Providers
4
They handle data and database management issues.
Activities
Services
Broadcast Receivers
Content Providers
Additional Components
There are additional components which will be used in the
construction of above mentioned entities, their logic, and wiring
between them. These components are −
1 Fragments
Views
2
UI elements that are drawn on-screen including buttons, lists
forms etc.
Layouts
3
View hierarchies that control screen format and appearance of
the views.
Intents
4
Messages wiring components together.
Resources
5
External elements, such as strings, constants and drawable
pictures.
Manifest
6
Configuration file for the application.
Android - UI Design
UI screen components
Types of layout
There are many types of layout. Some of which are listed below −
Linear Layout
Absolute Layout
Table Layout
Frame Layout
Relative Layout
Linear Layout
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello” />
</LinearLayout>
AbsoluteLayout
<AbsoluteLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
<Button
android:layout_width=”188dp”
android:layout_height=”wrap_content”
android:text=”Button”
android:layout_x=”126px”
android:layout_y=”361px” />
</AbsoluteLayout>
TableLayout
<TableLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_height=”fill_parent”
android:layout_width=”fill_parent” >
<TableRow>
<TextView
android:text=”User Name:”
android:width =”120dp”
/>
<EditText
android:id=”@+id/txtUserName”
android:width=”200dp” />
</TableRow>
</TableLayout>
RelativeLayout
<RelativeLayout
android:id=”@+id/RLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
</RelativeLayout>
FrameLayout
<ImageView
android:src = “@drawable/droid”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</FrameLayout>
Apart form these attributes, there are other attributes that are
common in all views and ViewGroups. They are listed below −
1 layout_width
2 layout_height
3 layout_marginTop
4 layout_marginBottom
5 layout_marginLeft
Specifies extra space on the left side of the View or ViewGroup
6 layout_marginRight
7 layout_gravity
8 layout_weight
Units of Measurement
When you are specifying the size of an element on an Android UI, you
should remember the following units of measurement.
1 dp
2 sp
3 pt
4 px
Screen Densities
120 dpi
160 dpi
240 dpi
320 dpi
TextView in Android
<TextView android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid" />
Edit Text
<EditText
android:id="@+id/simpleEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
The below code will create Button and write “Abhi Android” text on it.
<Button
android:id="@+id/simpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abhi Android"/>
Image Button
<ImageButton
android:id="@+id/simpleImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home" />
Toggle Button
<ToggleButton
android:id="@+id/simpleToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
How To Check Current State Of ToggleButton:
android:id="@+id/simpleToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Android RadioButton
Checkbox in Android
Important Note: A progress bar can also be made indeterminate.
In this mode a progress bar shows a cyclic animation without an
indication of progress. This mode is used in application when we
don’t know the amount of work to be done.
android:id="@+id/simpleProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="abhiandroid.com.listexample.MainActivity">
</ListView>
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"/>
<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion" />
Scroll View
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ScrollView>
Toast
Important Methods Of Toast:
Below we Firstly initiate the Toast and then display it using show()
method.
Below we Firstly initiate the Toast, set top and left gravity and then
display it using show() method.
Below we retrieve the Layout Inflater and then inflate the layout from
the xml file.
// Retrieve the Layout Inflater and inflate the layout from xml
(ViewGroup) findViewById(R.id.toast_layout_root));
Below we firstly retrieve the layout inflater and then inflate the layout
and finally create a new Toast and pass the inflated layout in the
setView() method.
// Retrieve the Layout Inflater and inflate the layout from xml
(ViewGroup) findViewById(R.id.toast_layout_root));
// create a new Toast using context
Step 4: Display the Toast on the screen using show() method of Toast.
<DatePicker
android:id="@+id/simpleDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"/>
Methods of DatePicker
1. setSpinnersShown(boolean shown):
DatePicker simpleDatePicker =
(DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date
picker
This method is used to get the selected day of the month from a date
picker. This method returns an integer value.
Below we get the selected day of the month from a date picker.
3. getMonth():
This method is used to get the selected month from a date picker.
This method returns an integer value.
DatePicker simpleDatePicker =
(DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date
picker
int month = simpleDatePicker.getMonth(); // get the selected month
4. getYear():
This method is used to get the selected year from a date picker. This
method returns an integer value.
Below code is used to get the selected year from a date picker.
DatePicker simpleDatePicker =
(DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date
picker
5. getFirstDayOfWeek():
This method is used to get the first day of the week. This method
returns an integer value.
DatePicker simpleDatePicker =
(DatePicker)findViewById(R.id.simpleDatePicker); // initiate a date
picker
Attributes of DatePicker
<DatePicker
android:id="@+id/simpleDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Intent
startActivity(intent);
Types of Intents:
startActivity(intent);
intentObj.setData(Uri.parse("https://www.abhiandroid.com"));
startActivity(intentObj);
Unlike Explicit Intent you do not use any class name to pass through
Intent(). In this example we has just specified an action. Now when we
will run this code then Android will automatically start your web
browser and it will open AbhiAndroid home page.
Intent Filter
Intent Filter are the components which decide the behavior of
an intent. As we have read in our previous tutorial of Intent about the
navigation of one activity to another, that can be achieve by
declaring intent filter. We can declare an Intent Filter for an Activity in
manifest file.
<intent-filter android:icon="@drawable/icon"
android:label="@string/label"
>
</intent-filter>
</activity>
Activity have different states or it’s known as Activity life cycle. All life
cycle methods aren’t required to override but it’s quite important to
understand them. Lifecycles methods can be overridden according to
requirements.
onStart() method is called just after it’s creation. In other case Activity
can also be started by calling restart method i.e after activity stop. So
this means onStart() gets called by Android OS when
user switch between applications. For example, if a user was using
Application A and then a notification comes and user clicked on
notification and moved to Application B, in this case Application A will
be paused. And again if a user again click on app icon of Application A
then Application A which was stopped will again gets started.
Activity Resumed:.onResume():
Activity also gets paused before stop called in case user press the
back navigation button. The activity will go in paused state for these
reasons also if a notification or some other dialog is overlaying any
part (top or bottom) of the activity (screen). Similarly, if the other
screen or dialog is transparent then user can see the screen but
cannot interact with it. For example, if a call or notification comes in,
the user will get the opportunity to take the call or ignore it.
Learn More About onPause() With Example
Activity is called stopped when it’s not visible to user. Any activity
gets stopped in case some other activity takes place of it. For
example, if a user was on screen 1 and click on some button and
moves to screen 2. In this case Activity displaying content for screen 1
will be stopped.
Every activity gets stopped before destroy in case of when user press
back navigation button. So Activity will be in stopped state when
hidden or replaced by other activities that have been launched or
switched by user. In this case application will not present anything
useful to the user directly as it’s going to stop.
First is if user pressed the back navigation button then activity will be
destroyed after completing the lifecycle of pause and stop.
In case if user press the home button and app moves to background.
User is not using it no more and it’s being shown in recent apps list. So
in this case if system required resources need to use somewhere else
then OS can destroy the Activity.
After the Activity is destroyed if user again click the app icon, in this
case activity will be recreated and follow the same lifecycle again.
Another use case is with Splash Screens if there is call to finish()
method from onCreate() of an activity then OS can directly call
onDestroy() with calling onPause() and onStop().
Broadcast
1.2. Implementation
If the event for which the broadcast receiver has registered happens,
the onReceive() method of the receiver is called by the Android system.
Before API level 11, you could not perform any asynchronous operation
in the onReceive() method, because once the onReceive() method had
been finished, the Android system was allowed to recycle that
component. If you have potentially long running operations, you should
trigger a service instead.
Since Android API 11 you can call the goAsync() method. This method
returns an object of the PendingResult type. The Android system
considers the receiver as alive until you call
the PendingResult.finish() on this object. With this option you can
trigger asynchronous processing in a receiver. As soon as that thread
has completed, its task calls finish() to indicate to the Android system
that this component can be recycled.
Fragments in android
A fragment has its own layout and its own behaviour with its
own life cycle callbacks.
You can add or remove fragments in an activity while the activity
is running.
You can combine multiple fragments in a single activity to build
a multi-pane UI.
A fragment can be used in multiple activities.
Fragment life cycle is closely related to the life cycle of its host
activity which means when the activity is paused, all the
fragments available in the activity will also be stopped.
A fragment can implement a behaviour that has no user interface
component.
Fragments were added to the Android API in Honeycomb version
of Android which API version 11.
Android fragments have their own life cycle very similar to an android
activity. This section briefs different stages of its life cycle.
Fragment lifecycle
Here is the list of methods which you can to override in your fragment
class −
Android Service
1. Started
2. Bound
1) Started Service
2) Bound Service
The service cannot be stopped until all clients unbind the service.
Linux kernel
At the bottom of the layers is Linux - Linux 3.6 with approximately 115
patches. This provides a level of abstraction between the device
hardware and it contains all the essential hardware drivers like
camera, keypad, display etc. Also, the kernel handles all the things
that Linux is really good at such as networking and a vast array of
device drivers, which take the pain out of interfacing to peripheral
hardware.
Libraries
Android Libraries
The Android runtime also provides a set of core libraries which enable
Android application developers to write Android applications using
standard Java programming language.
Application Framework
Applications
You will find all the Android application at the top layer. You will write
your application to be installed on this layer only. Examples of such
applications are Contacts Books, Browser, Games etc.