Assignment 3 Group B Graphics Primitive
Assignment 3 Group B Graphics Primitive
Engineering
5 5 5 5 20
Problem Definition:
3.1 Prerequisite:
3.3 Theory:
3.3.1 Introduction
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.
1
Laboratory Practice – IV BE Computer
Engineering
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/ViewGroup objects or you can declare your layout
using simple XML file main_layout.xml which is located in the res/layout folder of
your project.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="f
ill_parent"
android:orientation="ve
rtical" >
<TextView
android:id="@+id/text"
2
Laboratory Practice – IV BE Computer
Engineering
android:layout_width="wra
p_content"
android:layout_height="wra
p_content"
android:text="This is a
TextView" />
<Button
android:id="@+id/button"
android:layout_width="wra
p_content"
android:layout_height="wra
p_content"
android:text="This is a
Button" />
</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 –
3
Laboratory Practice – IV BE Computer
Engineering
4 Absolute Layout : Absolute Layout enables you to specify the exact location of its
children.
5 Frame Layout :The Frame Layout is a placeholder on screen that you can use to
display a single view.
6 List View : List View is a view group that displays a list of scrollable items.
Linear Layout:
Android Linear Layout is a view group that aligns all children in either vertically or
horizontally.
LinearLayout Attributes:-
Following are the important attributes specific to LinearLayout –
android:gravity: This specifies how an object should position its content, on both
5
the X and Y axes. Possible values are top, bottom, left, right, center, center_vertical,
center_horizontal etc.
android:orientation : This specifies the direction of arrangement and you will use
6 "horizontal"
for a row, "vertical" for a column. The default is horizontal.
Table Layout:
Android TableLayout going to be arranged groups of views into rows and columns.
You will use the <TableRow> element to build a row in the table. Each row has zero
or more cells; each cell can hold one View object.
TableLayout containers do not display border lines for their rows, columns, or cells.
TableLayout Attributes
Following are the important attributes specific to TableLayout −
Sr.
Attribute &
No.
Description
1
android:id :This is the ID which uniquely identifies the layout.
Widgets:
Widgets are an essential aspect of home screen customization. You can imagine
them as "at-a- glance" views of an app's most important data and functionality that
is accessible right from the user's home screen. Users can move widgets across their
home screen panels, and, if supported, resize them to tailor the amount of
information within a widget to their preference.
List view:
Android ListView is a view which groups several items and display them in vertical
scrollable list. The list items are automatically inserted to the list using an Adapter
that pulls content from a source such as an array or database.
An adapter actually bridges between UI components and the data source that fill
data into UI Component. Adapter holds the data and send the data to adapter view,
the view can takes the data from adapter view and shows the data on different
views like as spinner, list view, grid view etc.
The List View and Grid View are subclasses of Adapter View and they can be
populated by binding them to an Adapter, which retrieves data from an external
source and creates a View that represents each data entry.
List View
Android provides several subclasses of Adapter that are useful for retrieving
different kinds of data and building views for an AdapterView ( i.e. ListView or
GridView). The common adapters are ArrayAdapter, BaseAdapter,
6
Laboratory Practice – IV BE Computer
Engineering
CursorAdapter,SimpleCursorAdapter,SpinnerAdapter and
WrapperListAdapter. We will see separate examples for both the adapters.
Sr.
Attribute &
No Description
android:id: This is the ID which uniquely identifies the layout.
1
3 android:dividerHeight :This specifies height of the divider. This could be in px, dp,
sp, in, or mm.
android:entries :Specifies the reference to an array resource that will populate the
4 ListView.
ArrayAdapter:
You can use this adapter when your data source is an array. By default,
ArrayAdapter creates a view for each array item by calling toString() on each item
and placing the contents in a Text View. Consider you have an array of strings you
want to display in a ListView, initialize a new ArrayAdapter using a constructor to
specify the layout for each string and the string array −
ArrayAdapter adapter = new
ArrayAdapter<String>(this,R.layout.ListView,StringArray); Here are
arguments for this constructor −
⮚ First argument this is the application context. Most of the case, keep it this.
7
Laboratory Practice – IV BE Computer
Engineering
⮚ Second argument will be layout defined in XML file and having TextView for
each string in the array.
⮚ Final argument is an array of strings which will be populated in the text view.
Once you have array adapter created, then simply call setAdapter() on your
ListView object as follows −
ListView listView = (ListView)
findViewById(R.id.listview);
listView.setAdapter(adapter);
You will define your list view under res/layout directory in an XML file. For our
example we are going to using activity_main.xml file.
Option Menu :
Menu items are a very old and famous user interface entity. Almost all users are
comfortable using menus in their apps. Android provides an easy and flexible
infrastructure to add menus in your app. It lets you create menus through XML
resources or directly via code. Based on the menu item clicked, a certain action can
be performed by your app.
Code for Activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
8
Laboratory Practice – IV BE Computer
Engineering
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>
{ @Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating a Bitmap
i.setBackgroundDrawable(new BitmapDrawable(bg));
//Creating the Paint Object and set its color & TextSize
paint.setColor(Color.BLUE);
paint.setTextSize(50);
9
Laboratory Practice – IV BE Computer
Engineering
Output:
10
Laboratory Practice – IV BE Computer
Engineering
3.3.2 Questions:
1. Does android support other languages than Java?
2. What are the advantages of Android?
3. Define Android Toast.
4. What is Innet?
5. What are Android Layout Types.
11