Mobile Application Development Course Code: 4350703
Unit– IV
Dialog, Menu and Database with RecyclerView
And CardView
❖ Menu in Android
➢ There are 3 types of menus in Android:
• Option Menu
• Context Menu
• Pop-up Menu
❖ Option Menu
➢ The options menu is the primary collection of menu items for an activity.
➢ It's where you should place actions that have a overall impact on the app,
such as Search, Compose Email and Settings.
VPMP Polytechnic, Computer Department. 7
Mobile Application Development Course Code: 4350703
❖ Context Menu
➢ A context menu is a floating menu that appears when the user performs a
long-click on an element.
➢ It provides actions that affect the selected content or context frame.
❖ PopUp Menu
VPMP Polytechnic, Computer Department. 8
Mobile Application Development Course Code: 4350703
➢ A popup menu displays a list of items in a vertical list that is
anchored(sticked) to the view that invoked the menu.
➢ It's good for providing an overflow of actions that relate to specific content
or to provide options for a second part of a command.
❖ How to create a Menu?
➢ For all menu types mentioned above, Android provides a standard XML
format to define menu items.
➢ Instead of building a menu in your activity's code, you should define a menu
and all its items in an XML menu resource.
➢ You can then inflate the menu resource i.e load the XML files as a Menu
object in your activity.
Menu_example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
<item android:id="@+id/upload"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
Attributes Description
android: id It is used to uniquely identify an element in the application.
android:icon It is used to set the item's icon from drawable folder.
android: title It is used to set the item's title
VPMP Polytechnic, Computer Department. 9
Mobile Application Development Course Code: 4350703
❖ Load Android Menu from an Activity
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_example, menu);
}
➢ If you observe above code we are calling our menu using
MenuInflater.inflate() method in the form of R.menu.menu_file_name.
➢ Here our xml file name is menu_example.xml so we used file
name menu_example.
❖ Handle Android Menu Click Events
➢ In android, we can handle a menu item click event using
ItemSelected() event based on the menu type.
➢ To handle a context menu item click event, use onContextItemSelected()
method.
❖ How to Create an Alert Dialog Box in Android?
➢ Alert Dialog shows the Alert message and gives the answer in the form of
yes or no. Alert Dialog displays the message to warn you and then according
to your response, the next step is processed. Android Alert Dialog is built
with the use of three fields: Title, Message area, and Action Button.
➢ In order to make an alert dialog, you need to make an object of
AlertDialogBuilder which an inner class of AlertDialog.
➢ Its syntax is given below
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
➢ Now you have to set the positive (yes) or negative (no) button using the
object of the AlertDialogBuilder class.
➢ Its syntax is,
alertDialogBuilder.setPositiveButton(CharSequence text,
DialogInterface.OnClickListener listener);
alertDialogBuilder.setNegativeButton(CharSequence text,
DialogInterface.OnClickListener listener);
VPMP Polytechnic, Computer Department. 10
Mobile Application Development Course Code: 4350703
➢ Alert Dialog code has three methods:
• setTitle() method for displaying the Alert Dialog box Title
• setMessage() method for displaying the message
• setIcon() method is used to set the icon on the Alert dialog box.
❖ How to make custom dialog in android?
➢ The custom dialog uses DIALOG to create custom alert in android studio.
➢ Dialog display a small window i.e a popup which draws the user attention
over the activity before they continue moving forward.
➢ The dialog appears over the current window and display the content defined
in it.
AlertDialog Vs Custom AlertDialog:
➢ The Alert Dialog and Custom Alert Dialog both prompt a small window to
make decision.
➢ The AlertDialog make use of the defined components or methods like
setIcon, setTitle, setmessage etc but with Custom AlertDialog we can have
the dialog customized and can define the layout of dialog as required.
Example:
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
Button dialogButton = (Button)
dialog.findViewById(R.id.dialogButtonOK);
VPMP Polytechnic, Computer Department. 11
Mobile Application Development Course Code: 4350703
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
dialog.dismiss();
Toast.makeText(getApplicationContext(),"Dismissed..!!",Toas
t.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
❖ Android - SQLite Database
➢ SQLite is a opensource SQL database that stores data to a text file on a
device. Android comes in with built in SQLite database implementation.
➢ SQLite supports all the relational database features.
➢ In order to access this database, you don't need to establish any kind of
connections for it like JDBC,ODBC etc.
❖ Database - Package
➢ The main package is android.database.sqlite that contains the classes to
manage your own databases
❖ Database - Creation
➢ In order to create a database you just need to call this method
openOrCreateDatabase with your database name and mode as a parameter.
➢ It returns an instance of SQLite database which you have to receive in your
own object.
➢ Its syntax is given below
SQLiteDatabase mydatabase = openOrCreateDatabase("your database
name",MODE_PRIVATE,null);
VPMP Polytechnic, Computer Department. 12
Mobile Application Development Course Code: 4350703
❖ Database - Insertion
➢ We can create table or insert data into table using execSQL method defined
in SQLiteDatabase class.
➢ Its syntax is given below
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS student(Username
VARCHAR, Password VARCHAR);");
mydatabase.execSQL("INSERT INTO student VALUES('vpmp','654');");
❖ Database - Fetching
➢ We can retrieve anything from database using an object of the Cursor class.
➢ We will call a method of this class called rawQuery and it will return a
resultset with the cursor pointing to the table.
➢ We can move the cursor forward and retrieve the data.
Cursor resultSet = mydatbase.rawQuery("Select * from student",null);
resultSet.moveToFirst();
String username = resultSet.getString(0);
String password = resultSet.getString(1);
❖ Database - Helper class
➢ For managing all the operations related to the database, a helper class has
been given and is called SQLiteOpenHelper.
➢ It automatically manages the creation and update of the database.
➢ Its syntax is given below
public class DBHelper extends SQLiteOpenHelper
{
public DBHelper()
{
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db) { }
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { }
}
VPMP Polytechnic, Computer Department. 13
Mobile Application Development Course Code: 4350703
❖ Android RecyclerView, Android CardView
CardView: CardView is an extended version of Framelayout which can be used to
show items inside the card format.
➢ With the help of CardView, we can add radius and elevation to our items of
RecyclerView. CardView gives a rich look and feels to our list of data.
RecyclerView: RecyclerView is an extended version of ListView.
➢ In RecyclerView we can load a large amount of data and items of
RecyclerView can have a custom design.
➢ RecyclerView works on the ViewHolder design pattern so we have to create
a Data class that holds data for RecyclerView and a ViewHolder class which
will set data to each item of RecyclerView.
➢ RecyclerView is divided into 3 sections:
• Card Layout.
• Modal Class.
• ViewHolder class.
VPMP Polytechnic, Computer Department. 14