3 - Menus and Dialog Box

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

APPLICATION DEVELOPMENT AND EMERGING

TECHNOLOGIES

Lecture 3
Android Menus,
Using Dialog Box

Engr. Racquel A. Cortez, MIT


Navigation

Navigation refers to the interactions that allow users to


navigate across, into, and back out from the different pieces of
content within your app. 
• Button Clicks
• Application Bars
• Menus
• Navigation Components
UI Patterns

• UI Patterns help users to navigate through every app in the same way.

• Android UI is divided in three different areas:


 Home screen – the “landing” area when phone is turn on.
 All apps – the interface where the app installed are displayed
 Recent screen – the list of last used apps.
Action Bar

A well-known pattern that plays an important role. An action bar is a piece


of the screen, usually at the top, that is persistent across multiple views.

It provides some key functions:


• App branding: icon area
• Title area
• Key action area
• Menu Area
Menus

• Option menus
 onCreateOptionsMenu()
 onOptionItemsSelected()

• Context menus
 onCreateContextMenu()
 onContextItemsSelected()
Using an Option Menu
1. Create the menu XML file
2. Add Menu Resources
3. Add Menu items

<menu xmlns:android="http://schemas.android.com/apk/res/android" >


<item
android:id="@+id/menu_item1"
android:icon="@android:drawable/ menu_img1"
android:title="@string/menu_settings">
</item>

</menu>
Using an Option Menu

4. Inflate the menu layout resource into the options menu


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Using an Option Menu
5. Process the menu action in onMenuItemSelected of Activity

@Override
public boolean onOptionItemSelected(int featureId, MenuItem item) {
if (item.id == R.id.menu_item1){

}
return true;
}
• menu.xml

<item
android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:orderInCategory="0"
android:showAsAction="always"/>

• java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.findItem(R.id.item1).setIntent(new Intent(this,
FullscreenActivity.class));

return true;
}
Using a Context Menu

1. Create the menu XML file


2. Register object for Context Menu

registerForContextMenu(View v)

3. Override onCreateContextMenu
4. Inflate (load) the menu layout resource into the context
menu
Using a Context Menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate( R.menu.context_menu, menu );
}
Using a Context Menu
5. Process the menu action in onContextItemSelected of Activity

@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_item1){

}
return super.onContextItemSelected(item);
}
Dialogs

The dialog box (also known as a dialog) is an onscreen element that offers
choices to the user.
 Dialog
 AlertDialog
 CharacterPickerDialog
 ProgressDialog
 TimePickerDialog
 DatePickerDialog

 
Dialogs
Key methods that an activity use
to manage a Dialog Instance
 onCreateDialog
A callback when a dialog is being created for the first time.
 onPrepareDialog
A callback for updating a dialog on-the fly
 showDialog()
Used to display a Dialog instance.
 dismissDialog()
Used to stop showing a Dialog instance
 removeDialog()
Used to remove a Dialog instance from the Activity object’s Dialog pool.
Working with Custom Dialog
1. Design a custom layout resource to display in AlertDialog
2. Define the custom dialog identifier in the activity
3. Update the Activity class’ onCreateDialog() method to build and
return the appropriate custom AlertDialog
4. Launch the dialog using the showDialog() method
 Example:

AlertDialog.Builder dialog = new AlertDialog.Builder(this);


dialog.setTitle("My Dialog");
  dialog.setMessage("This is Sample Dialog");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
AlertDialog alert = dialog.create();
alert.show();
 Adding a Custom Dialog to the Settings Screen
Begin by inflating(loading) the custom layout you created into a
View control:

LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  final View layout = inflater.inflate(R.layout.custom_dialog, null);

Dialog dialog = new Dialog(myContext);


dialog.setContentView(R.layout.createddialog);
<object>=(objettype)dialog.findViewById(R.id.object1)

dialog.show()

//to close
dialog.dismiss()

You might also like