You are on page 1of 44

Unit-5 Toast, Menu, Dialog, List

& Adapters

5/1/2023
2 Menu

 In android, Menu is an important part of the UI component which is used to


provide some common functionality around the application.
 Users can feel a smooth and consistent experience throughout the
application with the help of menu.
 In order to use menu, we should define it in a separate XML file and use
that file in our application based on our requirements.
 There are basically three types of menu context(custom) and
options(system) menu and popup menu.

5/1/2023
3 Difference between context menu
and option menu
Context Menu Option Menu
Menu is activated by long press on a view. Menu is activated by tapping on the
overflow icon or pressing the device menu
button.
It appears near the long-pressed view It appears at the top of the screen
It can have many items It has limited number of items.
onCreateContextMenu() method is used onCreateOptionsMenu() method is used to
to create context menu create context menu
We need to register android widget for No need to register android Activity for
context menu using options menu
registerForContextMenu() method.
When the menu item is selected When the menu item is selected
onContextItemSelected()method is called. onOptionsItemSelected() method is called.

5/1/2023
4 Context Menu

 Context menu is a type of menu that appears when a user performs a long
press on a view in an Android app.
 It usually contains a list of actions that are specific to the selected view,
such as copy, paste, delete, or edit.
 Context menus are used to provide users with quick access to relevant
actions, and can be customized programmatically to add or remove items
based on the context of the selected view.

5/1/2023
5 Steps to create Context Menu
 Step1:create some menu item in res->menu->.xml file

5/1/2023
6 Steps to create Context Menu
 Step2: Implement onCreateContextMenu() method of Activity class in .java
file

5/1/2023
7 Steps to create Context Menu
 Step3: Register the widget (here EditText)for context menu item using
registerForContextMenu()

5/1/2023
8 Steps to create Context Menu
 Step4: Use onContextItemSelected() method to control menu item’s
selection event

5/1/2023
9 Complete Program of Set value in EditText
using Menu Item-java file

5/1/2023
10 Complete Program of Set value in EditText
using Menu Item-java file

5/1/2023
11 Option Menu

 In android, Options Menu is a primary collection of menu items for


an activity and it is useful to implement actions that have a global impact
on the application , such as Settings, Search, etc.
 By using Options Menu, we can combine multiple actions and other options
that are relevant to our current activity.

5/1/2023
12 Steps to create Option Menu
Step1:create some menu item in res->menu->.xml file

5/1/2023
13 Steps to create Option Menu
Step1:create some menu item in res->menu->.xml file

5/1/2023
14 Steps to create Context Menu
 Step2:Implement onCreateOptionsMenu() method of Activity class in .java
file

5/1/2023
15 Steps to create Context Menu
 Step3: Use onOptionsItemSelected() method to control menu item’s
selection event

5/1/2023
16 Complete Program of Options Menu-
MainActivity.java

5/1/2023
17 Complete Program of Options Menu-
MainActivity.java

5/1/2023
18 Alert Dialog
 A dialog is a small window that prompt the user and user have to select
one of the option from dialog box.
 In android one can use one, two , or three buttons or none at all.
 Dialog can also have a list of selectable items that consists of checkboxes
or radio buttons.
 Dialog box may contain progress bar, known as progressbar dialog box.

5/1/2023
19 Dialog box

5/1/2023
20 Dialog box

5/1/2023
21 Dialog box

5/1/2023
22 Dialog box

5/1/2023
23 Custom Dialog Program

5/1/2023
24 Custom Dialog Program

5/1/2023
25 Custom Dialog Program

5/1/2023
26 SQLite Database

 SQLite is heart of Android’s database support.


 It is an open-source relational database i.e. used to perform database
operations on android devices such as storing, manipulating or retrieving
data from the database.
 It is embedded in android by default. So, there is no need to perform any
database setup or administration task.
 SQLite does not require a separate server process or system to operate.

5/1/2023
27 Operations on SQLite Database
 Creating a database: A SQLite database can be created by creating an
instance of the SQLiteOpenHelper class and overriding the onCreate()
method.
 Syntax: CREATE DATABASE database_name;
 Creating a table: A table can be created in a SQLite database by
executing an SQL create table statement.
 Syntax: CREATE TABLE table_name ( column1 datatype, column2 datatype,
column3 datatype, ... );
 Inserting data: Data can be inserted into a SQLite database by executing
an SQL insert statement.
 Syntax: INSERT INTO table_name (column1, column2, column3, ...) VALUES
(value1, value2, value3, ...);

5/1/2023
28 Operations on SQLite Database
 Updating data: Data in a SQLite database can be updated by executing
an SQL update statement.
 Syntax: UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;
 Deleting data: Data in a SQLite database can be deleted by executing an
SQL delete statement.
 Syntax: DELETE FROM table_name WHERE condition;
 Querying data: Data in a SQLite database can be queried by executing an
SQL select statement.
 SELECT column1, column2, ... FROM table_name WHERE condition;
 Closing the database: It is important to close the database connection
after performing database operations to release resources.
 Note: datatype in the above syntax can be INT, VARCHAR, TEXT, REAL,
BLOB, etc.
5/1/2023
29 SQLiteOpenHelper
 SQLiteOpenHelper class provides the functionality to use the SQLite
database.
 The android.database.sqlite.SQLiteOpenHelper class is used for database
creation and version management.
 For performing any database operation, we have to provide the
implementation of onCreate() and onUpgrade() methods of
SQLiteOpenHelper class.

5/1/2023
30 Constructors of SQLiteOpenHelper
Constructor Description

SQLiteOpenHelper(Context creates an object for creating, opening and


context, String name, managing the database.
SQLiteDatabase.CursorFactory
factory, int version)

SQLiteOpenHelper(Context creates an object for creating, opening and


context, String name, managing the database. It specifies the error
SQLiteDatabase.CursorFactory handler.
factory, int version,
DatabaseErrorHandler
errorHandler)

5/1/2023
31 Methods of SQLiteOpenHelper
Method Description

public abstract void onCreate(SQLiteDatabase called only once when database is created for
db) the first time.

public abstract void called when database needs to be upgraded.


onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)

public synchronized void close () closes the database object.

public void onDowngrade(SQLiteDatabase db, called when database needs to be


int oldVersion, int newVersion) downgraded.

5/1/2023
32 Complete Database Program

5/1/2023
33 Complete Database Program

5/1/2023
34 Complete Database Program

5/1/2023
35 Complete Database Program

5/1/2023
36 Complete Database Program

5/1/2023
37 Output

5/1/2023
38 Android Application Priorities
 “The order in which processes are killed to reclaim resources is determined
by the priority of the hosted applications. An application’s priority is equal
to its highest-priority component”.
 Types of Processes and Priorities:

5/1/2023
39 Android Application Priorities

 “All Android applications will remain running and in memory until the system
needs resources for other applications”.
 Active Process: They have application components interacting with the
user. These processes have the highest critical priority. To keep them alive
other processes may be killed.
 Visible Process: They are killed in extreme conditions to run foreground
processes. These processes, host visible activities. They might not need any
user interaction as they are visible only.
 Ongoing Services: They have lesser user interaction so they have slightly low
priority than a foreground service or visible activity respectively. They are
generally not killed unless system runs out of resources.

5/1/2023
40 Android Application Priorities
 Background Process: These processes hold invisible activities. They don’t
have any running service. These processes are generally killed with the
pattern of killing least recent used activity.
 Blank or Empty Process: This is a cache which is maintained by the Android
system. There are applications which are kept in memory even after their
life is finished. This is done to reduce the start-up time if they are to be
launched again. They are removed from memory from time to time. These
processes have least priority.

5/1/2023
41 Android Manifest.xml

5/1/2023
42 AndroidManifest.xml
 The AndroidManifest.xml file is a required file in every Android application.
It contains essential information about the application that the Android
operating system needs to know before it can run the app.
 This manifest file includes the following components:
1. package: Specifies the package name of the application.
2. application: Contains information about the application, such as its icon, label,
and theme.
3. activity: Specifies the main activity of the application, which is the first screen
that the user sees when launching the app.
4. intent-filter: Declares that the activity can be launched by an intent with the
action android.intent.action.MAIN and the category
android.intent.category.LAUNCHER
 The AndroidManifest.xml file plays a crucial role in the Android app
development process. It allows the operating system to know how the app
should behave, what it can do, and what resources it needs to function
properly.

5/1/2023
43 GTU Questions
 What is Context Menu in Android? Which methods are used to implement
Context Menu?(2 M)
 Explain Alert Dialog with example. (4 M)
 Write Short note on SQLite database.(3M)
 Explain different android application priorities.(3M)
 Create an application which will print “ANDROID” in center of screen of
android device. Text should be bold and having color blue. (4M)
 Explain Option Menu.(4M)
 Explain Context Menu.(4M)
 Explain Basic Operations of SQLite Database.(4M)

5/1/2023
44 GTU Questions
 Write difference between option menu and context menu .(3 M)
 Create an Android application to demonstrate User Defined Toast.(4 M)
 Explain Android manifest file (4M)
 Create an Android application to demonstrate User Defined Toast (4M)
 Explain SQLiteOpenHelper class in SQLite Database.(4M)
 Explain Custom Menu in detail(3M)

5/1/2023

You might also like