Android Developer Fundamentals V2
Lesson 4
1
1
1
User Interaction
Android Developer Fundamentals V2
Menus and pickers
This work is licensed under a Creative Commons Attribution 4.0 International License.
Android Developer Fundamentals V2
Menus and pickers
This work is licensed under a Creative Commons Attribution 4.0 International License.
4.3 Menus and pickers
2
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Contents
3
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Overview
4
Android Developer Fundamentals V2
Types of Menus
5
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Dialogs and pickers
6
1
2
3
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
App Bar with Options Menu
7
Android Developer Fundamentals V2
What is the App Bar?
Bar at top of each screen—same for all devices (usually)
8
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
What is the options menu?
9
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Adding Options Menu
10
Android Developer Fundamentals V2
Steps to implement options menu
11
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Create menu resource
<item android:id="@+id/option_settings"
android:title="Settings" />
<item android:id="@+id/option_favorites"
android:title="Favorites" />
12
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Inflate options menu
Override onCreateOptionsMenu() in Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
13
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Add icons for menu items
14
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Add menu item attributes
<item
android:id="@+id/action_favorites"
android:icon="@drawable/ic_favorite"
android:orderInCategory="30"
android:title="@string/action_favorites"
app:showAsAction="ifRoom" />
15
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Override onOptionsItemSelected()
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
showSettings();
return true;
case R.id.action_favorites:
showFavorites();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
16
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Contextual Menus
17
Android Developer Fundamentals V2
What are contextual menus?
18
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Types of contextual menus
19
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Floating Context Menu
20
Android Developer Fundamentals V2
Steps
21
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Create menu resource
<item
android:id="@+id/context_edit"
android:title="Edit"
android:orderInCategory="10"/>
<item
android:id="@+id/context_share"
android:title="Share"
android:orderInCategory="20"/>
22
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Register a view to a context menu
In onCreate() of the Activity:
TextView article_text = findViewById(R.id.article);
registerForContextMenu(article_text);
23
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onCreateContextMenu() onCreateContextMenu() method
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_context, menu);
}
24
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onContextItemSelected() onCreateContextMenu() method
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_edit:
editNote();
return true;
case R.id.context_share:
shareNote();
return true;
default:
return super.onContextItemSelected(item);
}
}
25
The view
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Contextual Action Bar
26
Android Developer Fundamentals V2
What is Action Mode?
27
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Action mode has a lifecycle
28
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
What is a contextual action bar?
Long-press on View shows contextual action bar
29
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Steps for contextual action bar
30
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Use setOnLongClickListener
private ActionMode mActionMode;
In onCreate():
View view = findViewById(article);
view.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (mActionMode != null) return false;
mActionMode =
MainActivity.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
31
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement mActionModeCallback
public ActionMode.Callback mActionModeCallback =
new ActionMode.Callback() {
// Implement action mode callbacks here.
};
32
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onCreateActionMode
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_context, menu);
return true;
}
33
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onPrepareActionMode
34
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done.
}
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onActionItemClicked
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
// Perform action for the Share menu item.
mode.finish(); // Action picked, so close the action bar.
return true;
default:
return false;
}
}
35
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onDestroyActionMode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
36
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Popup Menu
37
Android Developer Fundamentals V2
What is a popup menu?
38
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Steps
39
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Add ImageButton
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_popup"
android:src="@drawable/@drawable/ic_action_popup"/>
40
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Assign onClickListener to button
private ImageButton mButton = � (ImageButton) findViewById(R.id.button_popup);
In onCreate():
mButton.setOnClickListener(new View.OnClickListener() {
// define onClick
});
41
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onClick
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, mButton);
popup.getMenuInflater().inflate(
R.menu.menu_popup, popup.getMenu());
popup.setOnMenuItemClickListener(� new PopupMenu.OnMenuItemClickListener() {
// implement click listener.
});
popup.show();
}
42
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Implement onMenuItemClick
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.option_forward:
// Implement code for Forward button.
return true;
default:
return false;
}
}
43
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Dialogs
44
Android Developer Fundamentals V2
Dialogs
45
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
AlertDialog
AlertDialog can show:
46
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Build the AlertDialog
Use AlertDialog.Builder to build alert dialog and set attributes:
public void onClickShowAlert(View view) {
AlertDialog.Builder alertDialog = new
AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Connect to Provider");
alertDialog.setMessage(R.string.alert_message);
// ... Code to set buttons goes here.
47
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Set the button actions
48
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
alertDialog code example
alertDialog.setPositiveButton(
"OK", newDialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User clicked OK button.
}
});
Same pattern for setNegativeButton() and setNeutralButton()
49
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Pickers
50
Android Developer Fundamentals V2
Pickers
51
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Pickers use fragments
52
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Introduction to fragments
53
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Creating a date picker dialog
54
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Creating a time picker dialog
55
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
Learn more
56
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
What's Next?
57
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Menus and pickers
END
58
Android Developer Fundamentals V2