0% found this document useful (0 votes)
252 views29 pages

06 - It-407 - Mobile Application Develeopment

Uploaded by

Hiba Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views29 pages

06 - It-407 - Mobile Application Develeopment

Uploaded by

Hiba Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

JAVA2S, THENEWBOSTON

University Of Gujrat
Faculty of Computing & IT

Title Mobile Application Development


Code

Credit Hours Theory/week:


Weight 3 Cr. Hrs.
Lectures: 2
Duration 1.5 Hrs. /Lecture
Instructor Mr. Muhammad Attique (Assistant Prof.)
Prerequisite Object Oriented Programming in Java
Follow Up Mobile Application Development
Category Elective
Aims and This course introduces mobile application development for the Android platform. Android is a software
Objectives stack for mobile devices that includes an operating system, middleware and key applications. The
Android SDK provides the tools and APIs necessary to begin developing applications on the Android
platform using the Java programming language. Students will learn skills for creating and deploying
Android applications, with particular emphasis on software engineering topics including software
architecture, software process, usability, and deployment.

Learning Students will be familiar with the process by which new applications are created for
Outcomes the Android operating system. Applications are usually developed in Java programming language using
the Android software development kit (SDK), but other development environments are also available.
Upon successful completion of this class, the learner will be able to:
 Use the development tools in the Android development environment
 Use the major components of Android API set to develop their own apps
 Describe the life cycles of Activities, Applications and Fragments
 Use the Java programming language to build Android apps
 Make UI-rich apps using all the major UI components
 Know UI best-practices
 Be familiar with new UI components like Fragments and the Action Bar
 Store and manipulate data using Content Providers, Shared Preferences and Notifications
 Do background processing with Services and Async Tasks
 Link mobile app with server data using web services
 Publish the application on google play store

Reference
Material 1) Wei-Menge Lee, Beginning Android 4 Application Development, John Wiley & Sons, 2012
2) Grant Allen, Beginning Android 4,Apress, 2011, ISBN: 1430239840. 3.
3) Neil Smyth, Android Studio Development Essentials, 2015

Assessment Sessional 25% Mid 25% Final 50%


Criteria Quizzes and Tests 10
Assignment and Presentations 15 Paper 25 Paper 50
Attendance and Class Participations
Tentative 16 Week Plan
Week Lecture Class Activity
Week#1  Overview
Introduction  Contents/Course Objectives
 Introduction to Mobile
Application Development
 Difference between
cross and native platforms
Week#2  Introduction to IDE
Preparing the Environment  Installing Android Studio
 Creating Android Project
 Executing Project On Device
 Launching Emulator

Week#3  Application Framework


Android Platform  Libraries Quiz#1
Architecture  Android RT
Core Libraries
 Linux Kernel

Week#4  Basic Layout


User Interface Widgets  Basic Widgets Assignment#1

Week#5  Activity Life Cycle


UI Architecture  Intent – Moving Data from One
activity to other
 Serialization
 Techniques and tools to Support
design on multi-screens

Week#6  Menus & Dialogs Quiz#2


Notification and Toast, Menus  Notifications and Toast
Week#7  Adapter Assignment#2
Adapter-Spinner  Spinner
Week#8  List View
Lists  Customize List View
Week#9  Fragments
Fragments  Fragments and Activities
 List Fragment
Week#10  Dialog Fragments Assignment#3
View Pager  View Pager
 Tabbed Application.
Week#11  Services
Services  Consuming system services
Week#12  Internet connectivity
Internet Connectivity  Receiving HTTP Response Quiz#3
(JSON )
 Consuming web services
 Json Paring and Data Modeling
Week#13  ServerSide Concepts
Application Design With Web  AppData Introduction
Services
Week#14 
Data Storage
Data Storage 
Shared Preferences Assignment#4

Android File System

Internal storage

External storage
Week#15 
Database connectivity
Content Provider 
SQLite

Introducing SQLite

SQLite OpenHelper and creating Quiz#4
a database
 Working with cursors Inserts,
updates, and deletes
 Content provider
Week#16  Packaging and Versioning Project Viva
Publishing your Application  Exporting a Signed Build
 Google Play Store
 Project/Presentation Submission
Android is an open source and Linux-based operating system for mobile devices
such as smartphones and tablet computers. Android was developed by the Open Handset
Alliance, led by Google, and other companies.

Prerequisites
Android programming is based on Java programming language so if you have
basic understanding on Java programming then it will be a fun to learn
Android application development.

Mobile application development


tutorial
Mobile app development is the act or process by which a mobile app is developed for mobile devices,
such as personal digital assistants, enterprise digital assistants or mobile phones. These applications
can be pre-installed on phones during manufacturing platforms,

The smaller screen sizes, decreased storage space, reduced memory, lower
computing power and unreliable network connections aren't the only things that
make mobile application development different than developing traditional Web
applications. The differences are largely driven by users experiencing variations
in the conditions in which they interact with the app. While most enterprise
applications are accessed from a desk in the office, mobile enterprise
applications are more frequently accessed out in the field, where workers are
focused on completing one particular task and moving on.

Let’s take a look at the difference between cross-platform framework or a


native development platforms.

Cross platform development?


A cross platform(platform independent) application runs across all mobile
operating systems without any extra coding. By all mobile operating systems, I
mean iOS and Android (windows phones are probably on their way out). A cross
platform framework provides all the tools to help you create cross-platform apps
easily. Some of the most popular cross- platform frameworks include:

 Xamarin
 Corona SDK
 appcelerator titanium
 PhoneGap

A native app(platform dependent) is specifically designed for one particular


operating system. This means it will work better in that specific environment than one
created for multiple platforms. One of the latest native android development framework is
Google Flutter. For iOS, it’s Xcode.
Here comes an IDE – Integrated Development Environment – a text
editor with additional support for developing, compiling and debugging applications.

The Android SDK includes an Android device emulator — a virtual device that runs
on your computer. The Android Emulator lets you develop and test Android apps without using a
physical device.
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.

1. Security:

The Linux kernel handles the security between the application and the system.

2.Memory Management:

It efficiently handles the memory management thereby providing the freedom to


develop our apps.
3.Process Management:

It manages the process well, allocates resources to processes whenever they need
them.

4.Network Stack:

It effectively handles the network communication.

5.Driver Model:

It ensures that the application works. Hardware manufacturers can build their drivers
into the Linux build.

Hardware Abstraction Layer (HAL)

a hardware abstraction layer (HAL) is a layer of programming that allows a


computer operating system to interact with a hardware device at a general
or abstract level rather than at a detailed hardware level. Windows 2000 is one of
several operating systems that include a hardware abstraction layer.

Libraries:

Running on the top of the kernel, the Android framework was developed with various
features. It consists of various C/C++ core libraries with numerous of open source tools.
Some of these are:

1. The Android runtime:

The Android runtime consist of core libraries of Java and ART(the Android RunTime).
Older versions of Android (4.x and earlier) had Dalvik runtime.

2. Open GL(graphics library):

This cross-language, cross-platform application program interface (API) is used to


produce 2D and 3D computer graphics.
3. WebKit:

This open source web browser engine provides all the functionality to display web
content and to simplify page loading.

4. Media frameworks:

These libraries allow you to play and record audio and video.

5. Secure Socket Layer (SSL):

These libraries are there for Internet security.

ART and Dalvik


Android runtime (ART) is the managed runtime used by applications and some system
services on Android. ART and its predecessor Dalvik were originally created specifically for
the Android project. ART as the runtime executes the Dalvik Executable format and Dex
bytecode specification.

ART and Dalvik are compatible runtimes running Dex bytecode, so apps developed for
Dalvik should work when running with ART. However, some techniques that work on Dalvik
do not work on ART.

the Android Runtime (ART) uses a process referred to as Ahead-of-Time


(AOT) compilation to translate the bytecode down to the native
instructions required by the device processor. This format is known as
Executable and Linkable Format (ELF).Each time the application is
subsequently launched, the ELF executable version is run, resulting in
faster application performance and improved battery life.

This contrasts with the Just-in-Time (JIT) compilation approach used in


older Android implementations whereby the bytecode was translated
within a virtual machine (VM) each time the application was launched.

ART Features

Here are some of the major features implemented by ART.

Ahead-of-time (AOT) compilation


ART introduces ahead-of-time (AOT) compilation, which can improve app performance.
ART also has tighter install-time verification than Dalvik.
At install time, ART compiles apps using the on-device dex2oat tool. This utility
accepts DEX files as input and generates a compiled app executable for the target device.
The utility should be able to compile all valid DEX files without difficulty. However, some
post-procesfsing tools produce invalid files that may be tolerated by Dalvik but cannot be
compiled by ART. For more information, see Addressing Garbage Collection Issues.

Improved garbage collection


Garbage collection (GC) can impair an app's performance, resulting in choppy display,
poor UI responsiveness, and other problems. ART improves garbage collection in several
ways:

 One GC pause instead of two


 Parallelized processing during the remaining GC pause
 Collector with lower total GC time for the special case of cleaning up recently-allocated,
short-lived objects
 Improved garbage collection ergonomics, making concurrent garbage collections more
timely, which makes GC_FOR_ALLOC events extremely rare in typical use cases
 Compacting GC to reduce background memory usage and fragmentation

Application Framework
An application framework is a software library that provides a fundamental structure to
support the development of applications for a specific environment. An application
framework acts as the skeletal support to build an application.
The Android framework includes the following key services:

 Activity Manager – Controls all aspects of the application lifecycle and activity
stack.

 Content Providers – Allows applications to publish and share data with other
applications.

 Resource Manager – Provides access to non-code embedded resources such as


strings, color settings and user interface layouts.

 Notifications Manager – Allows applications to display alerts and notifications to


the user.

 View System – An extensible set of views used to create application user


interfaces.

 Package Manager – The system by which applications are able to find out
information about other applications currently installed on the device.
 Telephony Manager – Provides information to the application about the telephony
services available on the device such as status and subscriber information.

 Location Manager – Provides access to the location services allowing an


application to receive updates about location changes.

Applications
Located at the top of the Android software stack are the applications. These comprise
both the native applications provided with the particular Android implementation (for
example web browser and email applications) and the third party applications installed
by the user after purchasing the device.

Just-in-Time (JIT), which compiles your


app in the browser at runtime.
 Ahead-of-Time (AOT), which compiles
your app at build time.
next →

← prev

Dalvik Virtual Machine | DVM


As we know the modern JVM is high performance and provides excellent memory management. But
low-powered handheld devices as well.

The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices.
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 clas
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 pla

The Android Assets Packaging Tool (aapt) handles the packaging process.

Implementing ART Just-In-Time (JIT)


Compiler
Android runtime (ART) includes a just-in-time (JIT) compiler with code profiling that
continually improves the performance of Android applications as they run. The JIT compiler
complements ART's current ahead-of-time (AOT) compiler and improves runtime
performance, saves storage space, and speeds application and system updates. It also
improves upon the AOT compiler by avoiding system slowdown during automatic
application updates or recompilation of applications during over-the-air (OTA) updates.

Although JIT and AOT use the same compiler with a similar set of optimizations, the
generated code might not be identical. JIT makes use of runtime type information, can do
better inlining, and makes on stack replacement (OSR) compilation possible, all of which
generates slightly different code.

The user runs the app, which then triggers ART to load the .dex file.
 If the .oat file (the AOT binary for the .dex file) is available, ART uses it directly.
Although .oatfiles are generated regularly, they don't always contain compiled code (AOT
binary).
 If no .oat file is available, ART runs through JIT or an interpreter to execute the .dex file.
ART always uses the .oat files if available. Otherwise, it uses the APK and extracts it in
memory to get to the .dex; this incurs a big memory overhead that is equal to the size of
the dex files.

 Jit fast from jvm and slow from aot


Android - Activities

Advertisements

Previous Page
Next Page
An activity represents a single screen with a user interface just like window or frame of
Java.Android activity is the subclass of ContextThemeWrapper class.

If you have worked with C, C++ or Java programming language then you
must have seen that your program starts from main() function. Very similar
way, Android system initiates its program with in an Activity starting with a
call on onCreate() callback method. There is a sequence of callback methods
that start up an activity and a sequence of callback methods that tear down an
activity as shown in the below Activity life cycle diagram: (image courtesy :
android.com )

The Activity class defines the following call backs i.e. events. You don't need to
implement all the callbacks methods. However, it's important that you
understand each one and implement those that ensure your app behaves the
way users expect.

Sr.No Callback & Description

onCreate()
1
This is the first callback and called
when the activity is first created.

onStart()

2 This callback is called when the


activity becomes visible to the user.

onResume()

3 This is called when the user starts


interacting with the application.

onPause()

The paused activity does not receive


user input and cannot execute any
4
code and called when the current
activity is being paused and the
previous activity is being resumed.

onStop()

5 This callback is called when the


activity is no longer visible.

onDestroy()

6 This callback is called before the


activity is destroyed by the system.

7 onRestart()

This callback is called when the


activity restarts after stopping it.

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.

Example
This example will take you through simple steps to show Android application
activity life cycle. Follow the following steps to modify the Android application
we created in Hello World Example chapter −

Step Description

1 You will use Android studio to create an Android application and name it
as HelloWorld under a package com.example.helloworld as explained in
the Hello World Example chapter.

2 Modify main activity file MainActivity.java as explained below. Keep rest of


the files unchanged.

3 Run the application to launch Android emulator and verify the result of the
changes done in the application.
Following is the content of the modified main activity
file src/com.example.helloworld/MainActivity.java. This file includes
each of the fundamental life cycle methods. The Log.d() method has been
used to generate log messages −

package com.example.helloworld;

import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

public class MainActivity extends Activity {

String msg = "Android : ";

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Log.d(msg, "The onCreate() event");

/** Called when the activity is about to become visible. */

@Override

protected void onStart() {

super.onStart();

Log.d(msg, "The onStart() event");

/** Called when the activity has become visible. */

@Override
protected void onResume() {

super.onResume();

Log.d(msg, "The onResume() event");

/** Called when another activity is taking focus. */

@Override

protected void onPause() {

super.onPause();

Log.d(msg, "The onPause() event");

/** Called when the activity is no longer visible. */

@Override

protected void onStop() {

super.onStop();

Log.d(msg, "The onStop() event");

/** Called just before the activity is destroyed. */

@Override

public void onDestroy() {

super.onDestroy();

Log.d(msg, "The onDestroy() event");

An activity class loads all the UI component using the XML file available
in res/layout folder of the project. Following statement loads UI components
from res/layout/activity_main.xml file:

setContentView(R.layout.activity_main);
An application can have one or more activities without any restrictions. Every
activity you define for your application must be declared in
your AndroidManifest.xml file and the main activity for your app must be
declared in the manifest with an <intent-filter> that includes the MAIN action
and LAUNCHER category as follows:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.tutorialspoint7.myapplication">

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

If either the MAIN action or LAUNCHER category are not declared for one of
your activities, then your app icon will not appear in the Home screen's list of
apps.

Let's try to run our modified Hello World! application we just modified. I
assume you had created your AVD while doing environment setup. To run the
app from Android studio, open one of your project's activity files and click
Run icon from the toolbar. Android studio installs the app on your AVD and
starts it and if everything is fine with your setup and application, it will display
Emulator window and you should see following log messages
in LogCatwindow in Android studio −
08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
08-23 10:32:07.683 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event

Let us try to click lock screen button on the Android emulator and it will
generate following events messages in LogCat window in android studio:
08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:32:53.294 4480-4480/com.example.helloworld D/Android :: The onStop() event

Let us again try to unlock your screen on the Android emulator and it will
generate following events messages in LogCat window in Android studio:
08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:34:41.392 4480-4480/com.example.helloworld D/Android :: The onResume() event

Next, let us again try to click Back button on the Android emulator and it
will generate following events messages in LogCat window in Android studio
and this completes the Activity Life Cycle for an Android Application.
08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:37:25.668 4480-4480/com.example.helloworld D/Android :: The onStop() event
08-23 10:37:25.669 4480-4480/com.example.helloworld D/Android :: The onDestroy() event

Android - UI Layouts

Advertisements

Previous Page
Next Page

The basic building block for user interface is a View object which is created
from the View class and occupies a rectangular area on the screen and is
responsible for drawing and event handling. View is the base class for widgets,
which are used to create interactive UI components like buttons, text fields,
etc.
The ViewGroup is a subclass of View and provides invisible container that
hold other Views or other ViewGroups and define their layout properties.

At third level we have different layouts which are subclasses of ViewGroup


class and a typical layout defines the visual structure for an Android user
interface and can be created either at run time
using View/ViewGroupobjects or you can declare your layout using simple
XML file main_layout.xml which is located in the res/layout folder of your
project.

LAYOUT PARAMS
This tutorial is more about creating your GUI based on layouts defined in XML
file. A layout may contain any type of widgets such as buttons, labels,
textboxes, and so on. Following is a simple example of XML file having
LinearLayout −

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView android:id="@+id/text"

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="This is a TextView" />

<Button android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is a Button" />

<!-- More GUI components go here -->

</LinearLayout>

Once your layout has created, you can load the layout resource from your
application code, in your Activity.onCreate() callback implementation as shown
below −

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setContentView() Method is a part of activity


class. Helps to set layout through XML and
programmatically. R.layout.main means an integer value holding inside layout class
in R.java file. it will helps system to easily access the layout from resources/layout directory.
(3) The setContentView(R.layout.activity_main) gives information about our layout
resource. Here, our layout resources are defined in activity_main.xml file.

Android Layout Types


There are number of Layouts provided by Android which you will use in almost
all the Android applications to provide different view, look and feel.

Sr.No Layout & Description

1 Linear Layout

LinearLayout is a view group that aligns all children in a single direction,


vertically or horizontally.

2 Relative Layout
RelativeLayout is a view group that displays child views in relative
positions.

3 Table Layout
TableLayout is a view that groups views into rows and columns.

4 Absolute Layout
AbsoluteLayout enables you to specify the exact location of its children.

5 Frame Layout
The FrameLayout is a placeholder on screen that you can use to display a
single view.

6 List View
ListView is a view group that displays a list of scrollable items.

7 Grid View
GridView is a ViewGroup that displays items in a two-dimensional,
scrollable grid.

Layout Attributes
Each layout has a set of attributes which define the visual properties of that
layout. There are few common attributes among all the layouts and their are
other attributes which are specific to that layout. Following are common
attributes and will be applied to all the layouts:

Sr.No Attribute & Description

1 android:id

This is the ID which uniquely identifies the view.

2 android:layout_width

This is the width of the layout.

3 android:layout_height

This is the height of the layout


4
android:layout_marginTop

This is the extra space on the top side of the layout.

5 android:layout_marginBottom

This is the extra space on the bottom side of the layout.

6
android:layout_marginLeft

This is the extra space on the left side of the layout.

7 android:layout_marginRight

This is the extra space on the right side of the layout.

8 android:layout_gravity

This specifies how child Views are positioned.

9 android:layout_weight

This specifies how much of the extra space in the layout should be
allocated to the View.

10
android:layout_x

This specifies the x-coordinate of the layout.

11 android:layout_y

This specifies the y-coordinate of the layout.

12 android:layout_width

This is the width of the layout.

13 android:layout_width

This is the width of the layout.

14 android:paddingLeft
This is the left padding filled for the layout.

15 android:paddingRight

This is the right padding filled for the layout.

16 android:paddingTop

This is the top padding filled for the layout.

17 android:paddingBottom

This is the bottom padding filled for the layout.

Here width and height are the dimension of the layout/view which can be
specified in terms of dp (Density-independent Pixels), sp ( Scale-independent
Pixels), pt ( Points which is 1/72 of an inch), px( Pixels), mm ( Millimeters)
and finally in (inches).

You can specify width and height with exact measurements but more often,
you will use one of these constants to set the width or height −

 android:layout_width=wrap_content tells your view to size itself to the


dimensions required by its content.
 android:layout_width=fill_parent tells your view to become as big as its parent
view.

Gravity attribute plays important role in positioning the view object and it can
take one or more (separated by '|') of the following constant values.

Constant Value Description

top 0x30 Push object to the top of its container, not


changing its size.

bottom 0x50 Push object to the bottom of its container, not


changing its size.

left 0x03 Push object to the left of its container, not


changing its size.
right 0x05 Push object to the right of its container, not
changing its size.

center_vertical 0x10 Place object in the vertical center of its


container, not changing its size.

fill_vertical 0x70 Grow the vertical size of the object if needed so


it completely fills its container.

center_horizonta 0x01 Place object in the horizontal center of its


l container, not changing its size.

fill_horizontal 0x07 Grow the horizontal size of the object if needed


so it completely fills its container.

center 0x11 Place the object in the center of its container in


both the vertical and horizontal axis, not
changing its size.

fill 0x77 Grow the horizontal and vertical size of the


object if needed so it completely fills its
container.

clip_vertical 0x80 Additional option that can be set to have the top
and/or bottom edges of the child clipped to its
container's bounds. The clip will be based on the
vertical gravity: a top gravity will clip the
bottom edge, a bottom gravity will clip the top
edge, and neither will clip both edges.

clip_horizontal 0x08 Additional option that can be set to have the left
and/or right edges of the child clipped to its
container's bounds. The clip will be based on the
horizontal gravity: a left gravity will clip the
right edge, a right gravity will clip the left edge,
and neither will clip both edges.

start 0x00800003 Push object to the beginning of its container,


not changing its size.

end 0x00800005 Push object to the end of its container, not


changing its size.

View Identification
A view object may have a unique ID assigned to it which will identify the View
uniquely within the tree. The syntax for an ID, inside an XML tag is −
android:id="@+id/my_button"

Following is a brief description of @ and + signs −

 The at-symbol (@) at the beginning of the string indicates that the XML parser
should parse and expand the rest of the ID string and identify it as an ID resource.
 The plus-symbol (+) means that this is a new resource name that must be created
and added to our resources. To create an instance of the view object and capture it
from the layout, use the following −

Button myButton = (Button) findViewById(R.id.my_button);

 Input controls are the interactive components in your app's user interface. Android
provides a wide variety of controls you can use in your UI, such as buttons, text
fields, seek bars, check box, zoom buttons, toggle buttons, and many more.


 UI ELEMENTS
 A View is an object that draws something on the screen that the user
can interact with and a ViewGroup is an object that holds other View
(and ViewGroup) objects in order to define the layout of the user
interface.
 You define your layout in an XML file which offers a human-readable
structure for the layout, similar to HTML. For example, a simple vertical
layout with a text view and a button looks like this −

 <?xml version="1.0" encoding="utf-8"?>


 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:orientation="vertical" >

 <TextView android:id="@+id/text"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="I am a TextView" />

 <Button android:id="@+id/button"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="I am a Button" />

 </LinearLayout>

 Android UI Controls
 There are number of UI controls provided by Android that allow you to
build the graphical user interface for your app.

Sr.No UI Control & Description


.

1 TextView

This control is used to display text to the user.

2 EditText
EditText is a predefined subclass of TextView that includes rich editing
capabilities.

3 AutoCompleteTextView
The AutoCompleteTextView is a view that is similar to EditText, except
that it shows a list of completion suggestions automatically while the
user is typing.

4 Button
A push-button that can be pressed, or clicked, by the user to perform an
action.

5 ImageButton
An ImageButton is an AbsoluteLayout which enables you to specify the
exact location of its children. This shows a button with an image (instead
of text) that can be pressed or clicked by the user.

6 CheckBox
An on/off switch that can be toggled by the user. You should use check
box when presenting users with a group of selectable options that are
not mutually exclusive.

7 ToggleButton
An on/off button with a light indicator.

8 RadioButton
The RadioButton has two states: either checked or unchecked.

9 RadioGroup
A RadioGroup is used to group together one or more RadioButtons.

10 ProgressBar
The ProgressBar view provides visual feedback about some ongoing
tasks, such as when you are performing a task in the background.

11 Spinner
A drop-down list that allows users to select one value from a set.

12 TimePicker
The TimePicker view enables users to select a time of the day, in either
24-hour mode or AM/PM mode.

13 DatePicker
The DatePicker view enables users to select a date of the day.
 Create UI Controls
 Input controls are the interactive components in your app's user
interface. Android provides a wide variety of controls you can use in your
UI, such as buttons, text fields, seek bars, check box, zoom buttons,
toggle buttons, and many more.
 As explained in previous chapter, a view object may have a unique ID
assigned to it which will identify the View uniquely within the tree. The
syntax for an ID, inside an XML tag is −

 android:id="@+id/text_id"

 To create a UI Control/View/Widget you will have to define a view/widget


in the layout file and assign it a unique ID as follows −

 <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:orientation="vertical" >

 <TextView android:id="@+id/text_id"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="I am a TextView" />

 </LinearLayout>

 Then finally create an instance of the Control object and capture it from
the layout, use the following −

 TextView myText = (TextView) findViewById(R.id.text_id);

You might also like