Beruflich Dokumente
Kultur Dokumente
What is Android :
o Android is a software package and linux based operating system for mobile
devices such as tablet computers and smartphones.
o 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.
Features of Android
o 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.
2) Anyone can customize the Android Platform.
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.
o 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.
Android applications
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.
o Shared Preferences
o Internal Storage
o External Storage
o SQLite Databases
o Network Connection
Shared Preferences
Internal Storage is useful to store the data files locally on the device’s internal memory
using FileOutputStream object. After storing the data files in device internal storage, we can read
the data file from device using FileInputStream object.
External Storage
External Storage is useful to store the data files publically on the shared external storage
using FileOutputStream object. After storing the data files on external storage, we can read the data
file from external storage media using FileInputStream object.
The data files saved in external storage are word-readable and can be modified by the user when
they enable USB mass storage to transfer files on a computer.
SQLite Databases
In android applications Shared Preferences, Internal Storage and External Storage options are useful
to store and maintain the small amount of data. In case, if we want to deal with large amount of
data, then SQLite database is the preferable option to store and maintain the data in structured
format.
By default, Android comes with built-in SQLite Database support so we don’t need to do any
configurations.
Just like we save the files on device’s internal storage, Android stores our database in a private disk
space that’s associated to our application and the data is secure, because by default this area is not
accessible to other applications.
The package android.database.sqlite contains all the required API’s to use SQLite database in our
android applications.
Context Menu is like a floating menu and that appears when the user performs a long press or click
on an element and it is useful to implement an actions that effect the selected content or context
frame.
The android Context Menu is more like the menu which displayed on right click in Windows or Linux.
It affects the selected content while doing action on it.
It doesn't support item shortcuts and icons.
3) Popup Menu
Android Popup Menu displays the menu below the anchor text if space is available otherwise above
the anchor text. It disappears if you click outside the popup menu.
The android Popup Menu provides an overflow style menu for actions that are related to specific
content.
The android.widget.PopupMenu is the direct subclass of java.lang.Object class.
Andorid Toast can be used to display information for the short period of time. A toast
contains message to be displayed quickly and disappears after sometime.
, Toast is a small popup notification which is used to display an information about the operation
which we performed in our app. The Toast will show the message for a small period of time and it
will disappear automatically after a timeout.
Create a Toast in Android
we can create a Toast by instantiating an android.widget.Toast object using makeText() method.
The makeText() method will take three parameters: application context, text message and the
duration for the toast. We can display the Toast notification by using show() method.
message It’s our custom message which we want to show in Toast notification.
We have a two ways to define the Toast duration, either in LENGTH_SHORT or LENGTH_LONG to
display the toast notification for short or longer period of time.
Syntax :
Example :
Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT).show();
Example of Toast : -
MainActivity.java
package example.hello.com.toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"HelloTEAM",Toast.LENGTH_SHORT).show();
}
}
Q3
What is Layout ?
Layouts structure
User interface in Android apps is built using Layouts. Each Layout is a subclass
of ViewGroup class, which derives from View class, which is the basic UI building
block. View is the base class for buttons, text fields etc.
The six different layouts are
1. Linear Layout
2. Relative Layout
3. Table Layout
4. Grid View
5. Tab Layout
6. List View
1. Linear Layout
In a linear layout, like the name suggests, all the elements are displayed in a linear
fashion(below is an example of the linear layouts), either Horizontally or Vertically and this
behavior is set in android:orientation which is an attribute of the node LinearLayout.
2. Relative Layout
In a relative layout every element arranges itself relative to other elements or a parent
element.
, lets consider the layout defined below. The “Cancel” button is placed relatively, to the right
ofthe “Login” button parallely.
Example:-
<Button android:layout_toRightOf="@id/btnLogin"
android:layout_alignTop="@id/btnLogin" ..></Button>
Table Layout
Table layouts in Android works in the same way HTML table layouts work. We can divide
your layouts into rows and columns.
TableLayout allows to group elements into rows and columns:
Grid View
GridView displays items in two-dimensional grid. This type of Layout is often
used on screens displaying photos or similar sets of “blocks” to click:
In android, Grid View is a ViewGroup which is used to display items in a two dimensional, scrollable
grid and grid items are automatically inserted to the gridview layout using a list adapter.
Generally, the adapter pulls data from a sources such as an array or database and converts each item
into a result view and that’s placed into the list.
Tab Layout
Tabbed layouts allow to introduce tabs in our Android application. Then, a single
Activity may contain several tabs and user can easily switch between them
List View
ListView allows to display a list of items. It may be used in multiple places,
from short lists of menu options to long list of emails or news feed. It
allows to easily and quickly present a list of items:
Android virtual device (AVD) is an emulator which is used to replicate the functionality of an android
phone, tablet, android wear or TV to test our android applications locally. By using AVD manager
interface in android studio we can setup android virtual device emulator to test our applications.
AVDs are essentially emulators that allow Android applications to be tested without
the necessity to install the application on a physical Android based device. An AVD
may be configured to emulate a variety of hardware features including options such
as screen size, memory capacity and the presence or otherwise of features such as
a camera, GPS navigation support or an accelerometer.
Step 3: After that choose the Category, phone size and choose the pixels according to
your requirement. After this click on Next button.
Step 4: After that choose the SDK Version and Click on Next button. If you have various
SDK Versions like Kitkat, Lolipop and Marshmallow etc in your SDK then you can select
one of them. Here we have only Marshmallow SDK Version. So, we can go with the
Marshmallow SDK Version. Click on the next.
Step 5: After that Enter the AVD Name in Android Virtual Device and Click on Finish
button. Here you can do customization to AVD which you are creating as per your
requirement.
2) J2Me
Designed for embedded System and mobile platform . Java ME
technology was originally created in order to deal with the constraint
associated with building application for small devices. For this
purpose Oracle defined the basics for Java ME Technology to fit such a
limited environment and make it possible to create Java Application
running on small devices with limited memory display and power
capacity . Java ME is an Open source implementation , Mika VM
,which contains the class libraries for implementing the Connected
Device Configuration .
There can be two forms of a service.The lifecycle of service can follow two different paths:
started or bound.
1. Started
2. Bound
1) Started Service
A service is started when component (like activity) calls startService() method, now it
runs in the background indefinitely. It is stopped by stopService() method. The service
can stop itself by calling the stopSelf() method.
2) Bound Service
A service is bound when another component (e.g. client) calls bindService() method. The
client can unbind the service by calling the unbindService() method.
The service cannot be stopped until all clients unbind the service.
In android the application component such as an activity can start the service by
calling startService() which results in calling the service’s onStartCommand() method.
Start a Service
In android, the component such as an activity, service or receiver can start the service
using startService() method. Following is the sample code snippet of starting a service
using startService method.
The system will invoke this method when an another component wants to bind with the service by
calling bindService(). During implementation of this method, we must need to provide an interface
to the clients to communicate with the service by returning an IBinder object.
onCreate()
The system will invoke this method when the service is created initially
using onStartCommand() or onBind() methods to do one time setup procedures. In case, if the service
is already running, then this method will not call.
onDestroy()
The system will invoke this method when the service is no longer used and is being destroyed. This is
the final call that the service will receive and we need to implement this method in our service to
clean up any unused resources such as threads, receivers or listeners.
Dialog is a small window that prompt messages to the user to make a decision or enter additional
details. Generally, the Dialogs are used with modals event and these useful to prompt users to
perform a particular action to proceed further in application.
AlertDialog This dialog is used to display prompt to the user with title, upto three buttons, list of selectable
items or a custom layout.
DatePickerDialog This dialog is a predefined UI control and it allow user to select Date.
ProgressDialog This dialog box displays a progress wheel or progress bar. It is an extension of AlertDialog and suppo
adding buttons.
1) AlertDialog
Android AlertDialog can be used to display the dialog message with OK and Cancel
buttons. It can be used to interrupt and ask the user about his/her choice to continue or
discontinue.
Android AlertDialog is composed of three regions: title, content area and action buttons.
setTitle() It is used to set the title of alertdialog and its an optional component.
setIcon() It is used to set the icon before the title
setCancelable() It is used to allow users to cancel alertdialog by clicking on outside of dialog area by setting tru
/ false.
setPositiveButton() It is used to set the positive button for alertdialog and we can implement click event of positive
button.
setNegativeButton() It is used to set the negative button for alertdialog and we can implement click event of negati
button.
2) ProgressBar
Progress bars are used to show progress of a task. For example, when
you are uploading or downloading something from the internet, it is
better to show the progress of download/upload to the user.
In android there is a class called ProgressDialog that allows you to
create progress bar.
By default the ProgressBar will be displayed as a spinning wheel, in case if we want to show it
like horizontal bar then we need to change the style property to horizontal
like style="?android:attr/progressBarStyleHorizontal".
3) DatePicker is a control which will allow users to select the date by day, month and year in our
application user interface.
If we use DatePicker in our application, it will ensure that the users will select a valid date.
in android DatePicker available in two modes, one is to show the complete calendar and another
one is to show the dates in spinner view.
It allows you to select date by day, month and year. Like DatePicker, android also provides
TimePicker to select time.
android:padding It is used to set the padding for left, right, top or bottom of date picker
4)TimePicker
Android TimePicker widget is used to select date. It allows you to select time by hour and
minute. You cannot select time by seconds.
If we use TimePicker in our application, it will ensure that the users will select a valid time for the
day.
in android TimePicker available in two modes, one is to show the time in clock mode and another
one is to show the time in spinner mode.
We can change the TimePicker in spinner mode to AM / PM format instead of 24 Hours format by
using setIs24HourView(true) method in Activity file like as shown below.
TimePicker picker=(TimePicker)findViewById(R.id.timePicker1);
picker.setIs24HourView(true);
android:padding It is used to set the padding for left, right, top or bottom of date picker.
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:
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.
OnClickListener()
OnFocusChangeListener()
OnTouchListener()
onTouch() This is called when the user presses the key, releases
the key, or any movement gesture on the screen. You
will use onTouch() event handler to handle such event.
OnMenuItemClickListener()
onCreateContextMenuItemListener()
onKey() OnFocusChangeListener()
This is called when the user is focused on the
item and presses or releases a hardware key on
the device. You will use onKey() event handler to
handle such event.
There are many more event listeners available as a part of View class like
OnHoverListener, OnDragListener etc
Touch Mode
Users can interact with their devices by using hardware keys or buttons or
touching the screen.Touching the screen puts the device into touch mode.
The user can then interact with it by touching the on-screen virtual buttons,
images, etc.You can check if the device is in touch mode by calling the View
class’s isInTouchMode() method.
Focus
A view or widget is usually highlighted or displays a flashing cursor when
it’s in focus. This indicates that it’s ready to accept input from the user.
onTouchEvent()
public boolean onTouchEvent(motionEvent event){
switch(event.getAction()){
case TOUCH_DOWN:
break();
case TOUCH_UP:
break;
case TOUCH_MOVE:
break;
return super.onTouchEvent(event) ;
This is the required xml file for all the android application and located inside the root
directory.
<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>
b) Screen Orientation
Syntax:
<activity android:name="package_name.Your_ActivityName"
android:screenOrientation="orirntation_type">
</activity>
Example:
<activity android:name=".SecondActivity"
android:screenOrientation="landscape">
</activity>
Value Description
Unspecified It is the default value. In such case, system chooses the orientation.