0% found this document useful (0 votes)
10 views

Android Layout

The document provides an overview of Android Layout, explaining its role in defining user interfaces through various layout types such as LinearLayout, RelativeLayout, and ConstraintLayout. It also discusses the SQLite database in Android, detailing its functionality for data storage and operations like CRUD. Key methods and classes related to SQLite database management, such as SQLiteOpenHelper and SQLiteDatabase, are highlighted for their importance in application development.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Android Layout

The document provides an overview of Android Layout, explaining its role in defining user interfaces through various layout types such as LinearLayout, RelativeLayout, and ConstraintLayout. It also discusses the SQLite database in Android, detailing its functionality for data storage and operations like CRUD. Key methods and classes related to SQLite database management, such as SQLiteOpenHelper and SQLiteDatabase, are highlighted for their importance in application development.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction

to
Android
WHAT IS ANDROID
LAYOUT?Android Layout is used to
define the user interface that
holds the UI controls or widgets
that will appear on the screen
of an android application or
activity screen. Generally,
every application is a
combination of View and
ViewGroup.
WHAT IS ANDROID
As we know, an android
LAYOUT?
application contains a large number of activities
and we can say each activity is one page of the
application. So, each activity contains multiple
user interface components and those components
are the instances of the View and ViewGroup. All
the elements in a layout are built using a
hierarchy of View and ViewGroup objects.
TYPES OF ANDROID
LAYOUT
• Android Linear Layout: LinearLayout is a ViewGroup subclass,
used to provide child View elements one by one either in a
particular direction either horizontally or vertically based on the
orientation property.
• Android Relative Layout: RelativeLayout is a ViewGroup
subclass, used to specify the position of child View elements
relative to each other like (A to the right of B) or relative to the
parent (fix to the top of the parent).
• Android Constraint Layout: ConstraintLayout is a ViewGroup
subclass, used to specify the position of layout constraints for
TYPES OF ANDROID
LAYOUT
• Android Frame Layout: FrameLayout is a ViewGroup
subclass, used to specify the position of View elements it
contains on the top of each other to display only a single View
inside the FrameLayout.
• Android Table Layout: TableLayout is a ViewGroup subclass,
used to display the child View elements in rows and columns.
• Android Web View: WebView is a browser that is used to
display the web pages in our activity layout.
• Android ListView: ListView is a ViewGroup, used to display
scrollable lists of items in a single column.
• Android Grid View: GridView is a ViewGroup that is used to
Linear
Layout
Linear Layout is the most basic layout
in android studio, that aligns all the children
sequentially either in a horizontal manner or
a vertical manner by specifying the
android:orientation attribute. If one applies
android:orientation=”vertical” then elements
will be arranged one after another in a
vertical manner and If you apply
android:orientation=”horizontal” then
elements will be arranged one after another
in a horizontal manner.
Attributes Description
It is defined individually to the child’s views to specify how
android:layout_weight LinearLayout divides the remaining space amongst the views it
contains

android:weightSum Defines the total weight sum


How the elements should be arranged in the layout. It can be
android:orientation
horizontal or vertical.

It specifies how an object should position its content on its X and


android:gravity Y axes.
Possible values are – center_vertical, fill, center, bottom, end, etc.

Sets the gravity of the View or Layout relative to its parent.


android:layout_gravity
Possible values are – center_vertical, fill, center, bottom, end, etc.

This must be a boolean value, either “true” or “false” and


android:baselineAligned
prevents the layout from aligning its children’s baselines.

android:id This gives a unique id to the layout.


Relative
Layout
The relative layout is used to arrange
the child views in a proper order which means arranging
the child objects relative to each other. Generally, if we
create an application using a linear layout that consists of
5 buttons. Even if we specify weight and gravity
properties they will not be relatively arranged.
To arrange them in a proper order we need
to use the relative layout. To arrange them we need some
advanced properties. Basically, we use layout_width,
layout_height, layout_text properties while creating an
application using the linear layout. But we need some
more advanced properties which are supported by
relative layout.
There are so many properties that are
supported by relative layout. some of the
most used properties are listed below
• layout_alignParentTop
• layout_alignParentBottom
• layout_alignParentRight
• layout_alignParentLeft
• layout_centerHorozontal
• layout_centerVertical
• layout_above
• layout_below
To execute the below code we need to install
Android studio. After installing follow the
below steps.
1.Create a new project
2.Select the language as java
3.select the API level of 26(android OREO 8.0)
4.Click on the ok button. Wait until UI displays.
5.Then clean the project. Because gradle may not build.
6.After that, we can see two code files. One is
activity_main.xml and MainActivity.java.
7.Go to the activity_main.xml file.
8.In that, we can see two modes. One is design mode and
another one is code mode. Then go to code mode.
9.And Use the below code.
10.We do not need to change the default java code. Because
<?xml version="1.0" encoding="utf-8"?> <Button
<RelativeLayout android:id="@+id/button3"
android:layout_width="fill_parent" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_height="wrap_content"
xmlns:android=" android:text="Bottom Left Button"
http://schemas.android.com/apk/res/android android:layout_alignParentLeft="true"
"> android:layout_alignParentBottom="true"
/>
<Button <Button
android:id="@+id/button1" android:id="@+id/button4"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Top Left Button" android:text="Bottom Right Button"
android:layout_alignParentLeft="true" android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/> android:layout_alignParentBottom="true"
<Button />
android:id="@+id/button2" <Button
android:layout_width="wrap_content" android:id="@+id/button5"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Top Right Button" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:text="Middle Button"
android:layout_alignParentRight="true"/ android:layout_centerVertical="true"
> android:layout_centerHorizontal="true"/
Sr.No. Attribute & Description
android:layout_above
1 Positions the bottom edge of this view above the given anchor view ID and must be
a reference to another resource, in the form "@[+][package:]type:name"
android:layout_alignBottom
2 Makes the bottom edge of this view match the bottom edge of the given anchor view
ID and must be a reference to another resource, in the form "@[+]
[package:]type:name".
android:layout_alignLeft
3 Makes the left edge of this view match the left edge of the given anchor view ID and
must be a reference to another resource, in the form "@[+][package:]type:name".
android:layout_alignParentBottom
4 If true, makes the bottom edge of this view match the bottom edge of the parent.
Must be a boolean value, either "true" or "false".
android:layout_alignParentEnd
5 If true, makes the end edge of this view match the end edge of the parent. Must be a
boolean value, either "true" or "false".
android:layout_alignParentLeft
6 If true, makes the left edge of this view match the left edge of the parent. Must be a
boolean value, either "true" or "false".
Sr.No. Attribute & Description
android:layout_alignParentRight
7 If true, makes the right edge of this view match the right edge of the parent. Must
be a boolean value, either "true" or "false".
android:layout_alignParentStart
8 If true, makes the start edge of this view match the start edge of the parent. Must
be a boolean value, either "true" or "false".
android:layout_alignParentTop
9 If true, makes the top edge of this view match the top edge of the parent. Must be a
boolean value, either "true" or "false".
android:layout_alignRight
10 Makes the right edge of this view match the right edge of the given anchor view ID
and must be a reference to another resource, in the form "@[+]
[package:]type:name".
android:layout_alignStart
11 Makes the start edge of this view match the start edge of the given anchor view ID
and must be a reference to another resource, in the form "@[+]
[package:]type:name".
android:layout_alignTop
Makes the top edge of this view match the top edge of the given anchor view ID
Sr.No. Attribute & Description
android:layout_below
13 Positions the top edge of this view below the given anchor view ID and must be a
reference to another resource, in the form "@[+][package:]type:name".
android:layout_centerInParent
14 If true, centers this child horizontally and vertically within its parent. Must be a
boolean value, either "true" or "false".
android:layout_centerVertical & centerHorizontal
15 If true, centers this child vertically or horizontally within its parent. Must be a
boolean value, either "true" or "false".
android:layout_toEndOf
16 Positions the start edge of this view to the end of the given anchor view ID and
must be a reference to another resource, in the form "@[+][package:]type:name".
android:layout_toLeftOf & toRightOf
17 Positions the right edge of this view to the left or right of the given anchor view ID
and must be a reference to another resource, in the form "@[+]
[package:]type:name".
android:layout_toStartOf
18 Positions the end edge of this view to the start of the given anchor view ID and
must be a reference to another resource, in the form "@[+][package:]type:name".
Table
Layout
How ConstraintLayout differs from Linear
Layout?
• With the help of ConstraintLayout, we can position our
UI components in any sort of order whether it may be
horizontal or vertical. But in the case of Linear Layout,
we can only arrange our UI components either in a
horizontal or in a vertical manner.
• Linear Layout provides usability with which we can
equally divide all the UI components in a horizontal or
vertical manner using weight sum but in Constraint
Layout, we have to arrange this UI component
manually.
• In Linear Layout the UI which is actually seen in the
How ConstraintLayout differs from
Relative Layout?
• In Constraint Layout, we have to add
constraints to the view on all four sides
whereas in Relative Layout we can simply align
our UI component relative to its ID using the ids
of UI components.
• In Relative Layout, the UI which is actually seen
in the Design editor of Android Studio will be
the same as that we will get to see in the app,
but in the case of Constraint layout if the UI
Android
&
SQLite
SQLite
SQLite is another data storage
available in Android where we can store
data in the user’s device and can use it any
time when required. In this article, we will
take a look at creating an SQLite database
in the Android app and adding data to that
database in the Android app. It’s Common
Practice to Proform basic CRUD (Create,
Read, Update, and Delete) operation with
What is SQLite Database?
SQLite Database is an open-
source database provided in Android which is
used to store data inside the user’s device in the
form of a Text file. We can perform so many
operations on this data such as adding new data,
updating, reading, and deleting this data. SQLite
is an offline database that is locally stored in the
user’s device and we do not have to create any
connection to connect to this database.
Android SQLite Connection
• SQLite is an open-source relational database i.e.
used to perform database operations on android
devices such as storing, manipulating or
retrieving persistent data from the database.
• It is embedded in android bydefault. So, there is
no need to perform any database setup or
administration task.
• SQLiteOpenHelper class provides the functionality
to use the SQLite database.
How Data is Being Stored in the
SQLite Database?
Data is stored in the SQLite
database in the form of tables. When
we stored this data in our SQLite
database it is arranged in the form of
tables that are similar to that of an
excel sheet. Below is the representation
of our SQLite database which we are
storing in our SQLite database.
Important Methods in SQLite Database
Below are the several important methods that we will
be using in this SQLite database integration in Android.
Method Description
This method is used to get the Array of column
getColumnNames()
names of our SQLite table.
This method will return the number of rows in the
getCount()
cursor.
This method returns a Boolean value when our cursor
isClosed()
is closed.
This method returns the total number of columns
getColumnCount()
present in our table.
getColumnName(int This method will return the name of the column when
columnIndex) we passed the index of our column in it.
getColumnIndex(String This method will return the index of our column from
columnName) the name of the column.
This method will return the current position of our
getPosition()
cursor in our table.
SQLiteOpenHelper class
The android.database.sqlite.SQLiteOpenHelper
class is used for database creation and version
management. For performing any database operation, you
have to provide the implementation of onCreate() and
onUpgrade() methods of SQLiteOpenHelper class.
Constructors of SQLiteOpenHelper class
There are two constructors of SQLiteOpenHelper
class.
Constructor Description
SQLiteOpenHelper(Context context, String creates an object for creating, opening and
name, SQLiteDatabase.CursorFactory managing the database.
factory, int version)
SQLiteOpenHelper(Context context, String creates an object for creating, opening and
name, SQLiteDatabase.CursorFactory managing the database. It specifies the error
factory, int version, DatabaseErrorHandler handler.
SQLiteDatabase class
It contains methods to be performed on sqlite database such as
create, update, delete, select etc.
Methods of SQLiteDatabase class
There are many methods in SQLiteDatabase class. Some of them are as
follows
Method Description
void execSQL(String sql) executes the sql query not select query.
long insert(String table, String inserts a record on the database. The table
nullColumnHack, ContentValues values) specifies the table name, nullColumnHack
doesn't allow completely null values. If second
argument is null, android will store null values
if values are empty. The third argument
specifies the values to be stored.
int update(String table, ContentValues updates a row.
values, String whereClause, String[]
whereArgs)
Cursor query(String table, String[] returns a cursor over the resultset.
columns, String selection, String[]

You might also like