Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Introduction To Mobile Computing With Android: Prof. Manish Kumar Joshi, Assistant Professor

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Introduction to Mobile Computing

with Android
Prof. Manish Kumar Joshi, Assistant Professor
IT & Computer Science
CHAPTER-4
Master Android Basics
Showing Alert Message to The User
We can show alert messages to the users in 2 different ways
1. Using Toast message
2. Using Alert Dialogue Box
Toast Message
A Toast is a feedback message. It takes a
very little space for displaying while overall
activity is interactive and visible to the user.
It disappears after a few seconds. It
disappears automatically.
Constants of Toast Class
• public static final int LENGTH_LONG
• public static final int LENGTH_SHORT
Method of Toast Class
• public static Toast makeText(Context context, CharSequence text, int
duration)
• public void show()
• public void setMargin (float horizontalMargin, float verticalMargin)
Alert Dialog Box
A Dialog is small window that prompts
the user to a decision or enter additional
information.
Alert Dialog Box
Some times in your application, if you
wanted to ask the user about taking a
decision between yes or no in
response of any particular action
taken by the user, by remaining in the
same activity and without changing
the screen, you can use Alert Dialog.
Method of Alert Dialog Box
• setIcon(Drawable icon)
• setCancelable(boolean cancel able)
• setMessage(CharSequence message)
• setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
DialogInterface.OnMultiChoiceClickListener listener)
• setOnCancelListener( DialogInterface.OnCancelListener
onCancelListener)
• setTitle(CharSequence title)
Intent and Passing Data Between Activities
An Android Intent is an abstract description of an operation to be
performed. It can be used with startActivity to launch an
Activity, broadcastIntent to send it to any interested BroadcastReceiver
components, and startService(Intent) or bindService(Intent,
ServiceConnection, int) to communicate with a background Service.
The intent itself, an Intent object, is a passive data structure holding an
abstract description of an operation to be performed.
Types of Intent

There are following two


types of intents supported Intent
by Android
1. Implicit Intent
2. Explicit Intent

Implicit Intent Explicit Intent


Explicit Intent
Explicit intent going to be
connected internal world of
application, suppose if you
wants to connect one activity to
another activity, we can do this
quote by explicit intent. These
intents designate the target
component by its name and they
are typically used for
application-internal messages -
such as an activity starting a
subordinate service or launching
a sister activity.
Implicit Intent
These intents do not name a
target and the field for the
component name is left blank.
Implicit intents are often used to
activate components in other
applications.
Intent Objects
An Intent object is a bundle of
information which is used by
the component that receives
the intent as well as information
used by the Android system.
An Intent object can contain the
following components based on
what it is communicating or
going to perform.
Action
This is mandatory part of the Intent object and is a string naming the action
to be performed — or, in the case of broadcast intents, the action that took
place and is being reported. The action largely determines how the rest of
the intent object is structured . The Intent class defines a number of action
constants corresponding to different intents.
The action in an Intent object can be set by the setAction() method and
read by getAction().
Data
Adds a data specification to an intent filter. The specification can be just a
data type (the mimeType attribute), just a URI, or both a data type and a
URI. A URI is specified by separate attributes for each of its parts −
These attributes that specify the URL format are optional, but also
mutually dependent
If a scheme is not specified for the intent filter, all the other URI attributes
are ignored.
If a host is not specified for the filter, the port attribute and all the path
attributes are ignored.
The setData() method specifies data only as a URI, setType() specifies it
only as a MIME type, and setDataAndType() specifies it as both a URI and
a MIME type. The URI is read by getData() and the type by getType().
SavedInstanceState Bundle
The savedInstanceState is a reference to a Bundle object that is passed
into the onCreate method of every Android Activity. Activities have the
ability, under special circumstances, to restore themselves to a previous
state using the data stored in this bundle. If there is no available instance
data, the savedInstanceState will be null. For example, the
savedInstanceState will always be null the first time an Activity is started,
but may be non-null if an Activity is destroyed during rotation.
When to save things in Bundle?
All activities have an onSaveInstanceState method that can be overridden.
When this method is called, any state-related data should be placed into
the outState Bundle. This method is called when an Activity is being
backgrounded (either after onPause() or onStop(), depending on different
factors).
What to Save
The savedInstanceState Bundle should only save information directly
related to the current Activity state. Example:
• User selections
• Scroll view positions
• User-submitted data
What not to Save
The following kinds of data should never be saved into the Bundle:
• Files
• Database data
• Images
• Videos
• Anything downloaded from the web
• Models
When SaveInstanceState is Useful?
There is one situation where using the savedInstanceState is almost
mandatory: when the Activity gets destroyed during rotation. One way that
Android handles rotation is to completely destroy and then re-create the
current Activity. When the Activity is being destroyed, any state related
information that is saved in onSaveInstanceState will be available when
the Activity comes back online.
Another situation is when your Activity gets backgrounded. When an
Activity is in a backgrounded state (either after onPause or onStop) it can
be destroyed at any time with no notice. In the event that the OS kills your
Activity without killing your Application, the OS will first save your outState
Bundle so you can later return to your previous state.
SaveInstanceState Example
Before using SaveInstanceState

On Device Rotation
SaveInstanceState Example
After using SaveInstanceState

On Device Rotation
CheckBox
A CheckBox is an on/off switch that
can be toggled by the user. You
should use check-boxes when
presenting users with a group of
selectable options that are not
mutually exclusive.
CheckBox Attributes
Inherited from android.widget.TextView Class
• android:autoText
• android:drawableBottom
• android:drawableRight
• android:editable
• android:text
CheckBox Attributes
Inherited from android.view.View Class
• android:background
• android:contentDescription
• android:id
• android:onClick
• android:visibility
Seek Bar
Seek Bar is one of the very useful
user interface element in Android
that allows the selection of integer
values using a natural user
interface. An example of Seek Bar is
your device’s brightness control and
volume control.
Seek Bar Methods
• getMax
• getProgress
Seek Bar Attributes
• android:id
• android:max
• android:progress
• android:indeterminate
Rating Bar
Rating bar is a subclass of
absSeekbar class in android. It is
used to show the rating on view
Group or window manager.
Radio Button
A RadioGroup class is used for set
of radio buttons.
If we check one radio button that
belongs to a radio group, it
automatically unchecks any
previously checked radio button
within the same group.
Radio Button Attributes
Inherited from android.view.View Class
• android:background
• android:contentDescription
• android:id
• android:onClick
• android:visibility
Accessing Phones Services
Opening URL in a
browser from the
Android Application.
Accessing Phones Services
Making Call to Someone from Android
Application.
Before we can call on any number, in
order to access phones services we
need to ask user permissions first.
Accessing Phones Services
Now if the user has
allowed the permission
to make calls from the
application, we can now
use call functionality.
Accessing Phones Services
Same like as calling
someone from android
application we can also
open dialer pad of our
mobile phone.
Accessing Phones Services
We can also use mailing
service in our android
application, which will open
the default email application
of our android device.
www.paruluniversity.ac.in

You might also like