Module - 3: Semester Vi Bcs/Bca S.A
Module - 3: Semester Vi Bcs/Bca S.A
SEMESTER VI BCS/BCA
S.A
Android User Interfaces
➢ The graphical user interface is an interface that allows users to
interact with electronic devices through graphical icons and
visual indicators.
➢ User Interface is everything that user can see and interact with.
Android supports pre-built UI components such as structured
layout objects and UI controls.
➢ A view is an object that draws something on the screen.
➢ A ViewGroup is an object that holds other ViewGroups and
view objects. It is an invisible container that organizes other
child views.
➢ UI can be built using three methods – building UI using code,
building UI using XML, building UI using code and XML
View is the base class for widgets, which are used to create interactive UI
components like buttons, text fields, etc. View is a basic building block of UI
(User Interface) in android. A view responds to user inputs. Eg: EditText, Button,
CheckBox, etc. ViewGroup is an invisible container of other views (child views)
and other ViewGroup. Eg: LinearLayout is a ViewGroup that can contain other
views in it. View refer to the android.view.View class, which is the base class of
all UI classes.
1. Understanding
Android’s Common Controls
➢ Views include text controls and then cover buttons, check boxes, radio
buttons, lists, grids, date and time controls, and a map-view control.
Type Description
Text that is a password that should be
textPassword
obscured from others.
textVisiblePassword Text
number A numeric only field
phone For entering a phone number
date For entering a date
time For entering a time
textMultiLine Allow multiple lines of text in the field
➢If multiline typing is required, you need to specify inputType with
textMultiLine.
➢One of the nice features of EditText is that you can specify hint text.
➢This text will be displayed slightly faded and disappears as soon as the
user starts to type text.
➢The purpose of the hint is to let the user know what is expected in this
field, without the user having to select and erase default text.
➢In XML, this attribute is android:hint="your hint text here" or
android:hint="@string/your_hint_name", where your_hint_name is a
resource name of a string to be found in /res/values/strings.xml.
III). AutoCompleteTextView
➢The AutoCompleteTextView control is a TextView with auto-complete
functionality.
➢In other words, as the user types in the TextView, the control can
display suggestions for selection.
<AutoCompleteTextView
android:id="@+id/actv“
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
IV). MultiAutoCompleteTextView
➢In Android, MultiAutoCompleteTextView is an
editable TextView extends AutoCompleteTextView that can show the
complete suggestion for the sub-string of a text allowing user to quickly
select instead of typing whole.
➢One most important difference between
MultiAutoCompleteTextView and AutoCompleteTextView is that
AutoCompleteTextView can hold or select only single value at single
time but MultiAutoCompleteTextView can hold multiple string words
value at single time.
➢ These all values are separated by comma(,).
Example :-
<MultiAutoCompleteTextView
android:id="@+id/mactv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
b). Button Controls
➢Buttons are common in any widget toolkit.
➢Android offers the typical set of buttons as well as a few extras.
➢In this section, we will discuss three types of button controls:
❖ basic button
❖ image button
❖ toggle button.
➢The button at the top is the basic button, the
➢middle button is an image button, and the last one is a toggle button.
I). The Button Control
➢The basic button class in Android is android.widget.Button.
➢There’s not much to this type of button, beyond how you use it to
handle click events.
Handling Click Events on a Button :-
<Button
android:id="@+id/button1"
android:text="@string/basicBtnLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Java Code
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:src="@drawable/icon" />
➢Here we’ve created the image button in XML and set the button’s
image from a drawable resource.
➢The image file for the button must exist under /res/drawable.
III). The ToggleButton Control
➢The ToggleButton control, like a check box or
a radio button, is a two-state button.
➢ToggleButton’s default behavior is to show a
green bar when in the On state and a grayed-out
bar when in the Off state.
➢Moreover, the default behavior also sets the button’s text to On when
it’s in the On state and Off when it’s in the Off state.
➢You can modify the text for the ToggleButton if On/Off is not
appropriate for your application.
➢For example, if you have a background process that you want to start
and stop via a ToggleButton, you could set the button’s text to Stop and
Run by using android:textOn and android:textOff properties.
➢This button can be in either the On or Off state.
➢ToggleButton’s default behavior is to show a green bar when in the
On state and a grayed-out bar when in the Off state.
<ToggleButton
android:id="@+id/cctglBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Button"
android:textOn="Stop"
android:textOff="Run"/>
c). The CheckBox Control
➢A CheckBox is an on/off switch that can
be toggled by the user. Using checkbox
user can select multiple options at a time.
➢You should use check-boxes when
presenting users with a group of selectable
options that are not mutually exclusive.
➢For example, it can be used to know the
hobby of the user, activate/deactivate the
specific action etc.
➢In Android, you can create a check box
by creating an instance of
android.widget.CheckBox.
Attributes of Checkbox :-
Attribute Description
If set, specifies that this TextView has a
android:autoText textual input method and automatically
corrects some common spelling errors.
android:text This is the Text to display.
This is the name of the method in this
android:onClick View's context to invoke when the
view is clicked.
This controls the initial visibility of the
android:visibility
view.
Methods of Checkbox :-
Method Description
android:maxHeight
An optional argument to supply a maximum height for this view.
android:maxWidth
An optional argument to supply a maximum width for this view.
android:src
Sets a drawable as the content of this ImageView.
Methods of ImageView Control :-
❖public void setImageBitmap(Bitmap bm)
➢Sets a Bitmap as the content of this ImageView.
<LinearLayout >
<TextView
android:id="@+id/dateDefault"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2. Time Picker
➢Android Time Picker allows you to select the time of day in either
24 hour or AM/PM mode.
➢The time consists of hours, minutes and clock format.
➢Android provides this functionality through TimePicker class.
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Method Description
is24HourView() This method returns true if this is in 24
hour view else false
isEnabled() This method returns the enabled status
for this view
setCurrentHour(Integer This method sets the current hour
currentHour)
setCurrentMinute(Integer This method sets the current minute
currentMinute)
setEnabled(boolean enabled) This method set the enabled state of this
view
setIs24HourView(Boolean This method set whether in 24 hour or
is24HourView) AM/PM mode
setOnTimeChangedListener(TimePi
cker.OnTimeChangedListener This method Set the callback that
onTimeChangedListener) indicates the time has been adjusted by
the user
3. Analog and Digital Clock Control
➢In Android, the AnalogClock is a two-handed clock, one for hour
indicator and the other for minute indicator.
➢ The DigitalClock is look like your normal digital watch on hand,
which display hours, minutes and seconds in digital format.
➢Both AnalogClock and DigitalClock are UNABLE to modify the
time, if you want to change the time, use “TimePicker” instead.
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DigitalClock" />
g ). Map View Controls
➢The com.google.android.maps.MapView control can display a map.
➢This control can be instantiated either via XML layout or code, but
the activity that uses it must extend MapActivity.
➢MapActivity takes care of multithreading requests to load a map,
perform caching, and so on.
<LinearLayout
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="myAPIKey"
/>
</LinearLayout>
• Map Controls API has attributes such as UI Controls, Zoom
controls that appears in the right bottom corner of the
application.
• It also has compass graphic that appears in the top left corner
of the app.
• Map Control also includes Map gestures, My Location, Map
Tool bar etc.
Understanding Adapters
➢List controls (AdapterViews) are used to display collections of
data. Adapters are bridge between AdapterView [ListView, GridView]
and underlying data for that view.
➢ArrayAdapter
➢CursorAdapter
➢SimpleAdapter
➢SimpleCursorAdapter
AdapterView Class hierarchy
Adapter and AdapterView
➢ Adapter View handles the data display.
➢ AdapterView class is inherited from ViewGroup class.
AdapterView are classes that extend
android.widget.AdapterView
➢ AdapterView includes ListView, GridView, Spinner Class
etc.
➢ Adapter class manages data for an AdapterView and
provides child view for it.
➢ Adapters are the link between a set of data and the
AdapterView.
➢ Adapter retrieves data from a source such as array or
database and converts each data into view that can be added
into Adapter View layout.
Types of Adapters
1.SimpleCursorAdapter
➢The SimpleCursorAdapter links the data contained in a Cursor to an
Adapter View.
Cursors
❖A cursor is a set of data.
❖A cursor is returned when a database query is executed .
❖The result of your query is contained in the cursor.
❖The SimpleCursorAdapter binds the Cursor data to an
Adapter View.
❖ You define a layout that controls how each row of data is
displayed.
❖This layout is then displayed in the Adapter View, like a
ListView for example.
Cursor
SimpleCurserAdapter
➢To map the data rows to the ListView, the SimpleCursorAdapter
needs to have a child layout resource ID.
➢The constructor of SimpleCursorAdapter looks like this:
SimpleCursorAdapter(Context context, int childLayout, Cursor
c, String[] from, int[] to)
➢This adapter converts a row from the cursor to a child view for the
container control.
➢The definition of the child view is defined in an XML resource
(childLayout parameter).
➢Context :- The context where the ListView associated with this
SimpleListItem is running
➢Layout :- Resource identifier of a layout file that defines the views
for this list item.
➢C :- The database cursor. Can be null if the cursor is not available
yet.
➢From :- A list of column names representing the data to bind
to the UI.
➢To :- The views that should display column in the "from"
parameter.
2.ArrayAdapter
Activity_main.xml
<LinearLayout>
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/hello_world"
/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner sp = (Spinner)findViewById(R.id.planets_spinner);
String[] items = { "this", "is", "a", "really", "silly", "list" };
ArrayAdapter<String> ad = new ArrayAdapter
<String>(this,android.R.layout.simple_list_item_1,items);
sp.setAdapter(ad);
}}
Output :-
4. Gallery Control
➢The Gallery control is a horizontally
scrollable list control that always focuses at
the center of the list.
➢This control generally functions as a photo
gallery in touch mode.
➢The Gallery control is typically used to
display images.
➢Gallery can be Instantiated via either XML
layout or code:
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content />
Styles and Themes
➢Android provides several ways to alter the style of views in your
application.
➢We’ll first cover using markup tags in strings and then how to use
spannables to change specific visual attributes of text.
1. Styles
➢A style is a collection of View attributes that is given a name.
➢A style can be applied to a view or to an entire activity.
➢ That collection can be referred by its name.
➢Style can be applied statically or dynamically.
➢Statically, you can apply markup directly to the strings in your string
resources, as shown here:
➢You can then reference it in your XML or from code. Note that you
can use the following
➢HTML tags with string resources:
❖ <i>, <b>, <u>, <strike>, <big>, <small>, <monospace>
<string name="styledText"><i>Static</i> style in
<b>TextView</b>
.</string>
➢A style is a collection of View attributes that is given a name
➢ The collection can be referred by its name and assign that style by
name to views.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ErrorText">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0000</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
➢We can now use this style for a TextView, as shown
<TextView
android:id="@+id/errorText"
style="@style/ErrorText"
android:text="No errors at this time"/>
<activity android:theme="@style/MyActivityTheme">
<application android:theme="@style/MyApplicationTheme">
<application android:theme="@android:style/Theme.NoTitleBar">
Understanding Layout Managers
➢Android offers a collection of view classes that act as containers for
views.
➢These container classes are called layouts (or layout managers), and
each implements a specific strategy to manage the size and position of its
children – [child views].
Layout Manager Description
LinearLayout Organizes its children either
horizontally or vertically
TableLayout Organizes its children in tabular form
RelativeLayout Organizes its children relative to one
another or to the parent
FrameLayout Allows you to dynamically change the
control(s) in the layout
GridLayout Organizes its children in a grid
arrangement
1. The LinearLayout Layout Manager
➢The LinearLayout layout manager is the most basic layout.
➢ This layout manager organizes its children either horizontally or
vertically based on the value of the orientation property.
➢LinearLayout with a horizontal configuration.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
2. The TableLayout Layout Manager
➢The TableLayout layout manager is an extension of LinearLayout.
➢This layout manager structures its child controls into rows and
columns.
Example of Simple Table Layout :-
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:text="First Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:text="Edgar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:text="Last Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:text="Poe"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
➢To use this layout manager, you create an instance of TableLayout and
place TableRow elements within it.
➢These TableRow elements contain the controls of the table.
Output :-
Example Using android:padding :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:text="one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40px" />
</LinearLayout>
➢This creates 40 pixels of whitespace between the EditText control’s
outer boundary and the text displayed within it.
➢android:padding sets the padding for all sides: left, right, top, and
bottom.
➢You can control the padding for each side by using android:
leftPadding , android:rightPadding, android:topPadding, and
android:bottomPadding.
3. The RelativeLayout Layout Manager
➢One of the most important layout manager is RelativeLayout.
➢As the name suggests, this layout manager implements a policy
where the controls in the container are laid out relative to either the
container or another control in the container.
Example of Relative Layout :-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/userNameLbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Username: " />
<EditText
android:id="@+id/userNameText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/pwdCriteria"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</RelativeLayout>
4. The FrameLayout Layout Manager
➢Android also offers a layout manager that is
mainly used to display a single item:
FrameLayout.
➢ This utility layout class is mainly used to
dynamically display a single view, but it can
be populatde it with many items, by setting
one to visible while the others are invisible.
Example of Frame Layout :-
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frmLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/oneImgView"
android:src="@drawable/one"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView
android:id="@+id/twoImgView"
android:src="@drawable/two"
android:scaleType="fitCenter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
</FrameLayout>
Output :-
5. The GridLayout Layout Manager
➢Android 4.0 brought with it a new
layout manager called GridLayout.
➢GridLaout lays out views in a grid
pattern of rows and columns,
somewhat like TableLayout.
➢Views can span multiple grid cells.
More than one view can be put into
the same grid cell.