0% found this document useful (0 votes)
8 views92 pages

FU Lecture 11

The document provides an overview of designing user interfaces using dialogs and menus in Android. It explains the creation and usage of AlertDialogs, including how to add buttons and lists, and how to manage dialog visibility. Code examples illustrate how to implement these features in an Activity class.

Uploaded by

Chị Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views92 pages

FU Lecture 11

The document provides an overview of designing user interfaces using dialogs and menus in Android. It explains the creation and usage of AlertDialogs, including how to add buttons and lists, and how to manage dialog visibility. Code examples illustrate how to implement these features in an Activity class.

Uploaded by

Chị Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 92

Designing Your User

Interface Using Views

User Interface
Agenda
 Dialogs

 Menus
Dialogs
• A dialog is a small window that appears in
front of the current Activity

• It causes the Activity to lose focus

• Used for ProgressBars, Alerts, etc


Dialogs
• onCreateDialog() is called the first time.

• onPrepareDialog is called every time its


opened.
o without this, it will remain the same as the first time it
was opened
Dialogs - AlertDialog

• an AlertDialog is an extension of the Dialog


class

• it is capable of constructing most dialog user


interfaces and is the suggested dialog type
Dialogs - AlertDialog
• you should use it for dialogs that use any of
the following features

o a title
o a text message
o one, two, or three buttons
o a list of selectable items (with optional checkboxes
or radio buttons)
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;

switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Nothing special here, just an
Dialogs - Creating an AlertDialog
int I use to identify the dialog,
because we can have more
than one
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;

switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
Override this method of
public class MyDialogsActivity,
extendswhich is{called
Activity
when
static final you want to show
int DIALOG_EXIT_ID = 1;
any dialog of the Activity
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;

switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;
Switch because we can have more
than one dialog, meaning we need
@Override
to check the dialog id
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;

switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
If you
protected Dialog onCreateDialog(int id)want
{ an
Dialog dialog = null; AlertDialog, you need to
build one first
switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
Give the user a message
switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
If true,
Dialog dialog then the user can press the
= null;
back button to dismiss the dialog. false
switch(id) {would force the user to make an action
on the dialog.
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
Add a button, AND set a listener for
switch(id) { when the button is pressed. This is
should
case beDIALOG_EXIT_ID:
the "positive" button, e.g.
"Yes",AlertDialog.Builder
"Absolutely!" builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;

This is the listener for then the


switch(id) { "positive" button is pressed, so you
case DIALOG_EXIT_ID:
should take some kind of action.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;

switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
Thebuilder.setPositiveButton("Yes",
Dialog isn't created until new DialogInterface.OnClickListener() {
you call create()public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
});
dialog = builder.create();
break;
}

return dialog;
}
}
Dialogs - Creating an AlertDialog
public class MyDialogs extends Activity {
static final int DIALOG_EXIT_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;

switch(id) {
case DIALOG_EXIT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogExample.this.finish();
}
The type of this method is
});
Dialog, so here we return ...
dialog = builder.create();
the Dialog!
break;
}

return dialog;
}
}
Dialogs - AlertDialog
You can add up to 3 buttons on the AlertDialog
by calling

1. dialog.setPositiveButton()
o we just used this one in the previous slides
2. dialog.setNegativeButton()

o "No", "Cancel"
3. dialog.setNeutralButton()

o
"Remind me Later"
Dialogs - Showing a Dialog

• To show a Dialog on the screen, simply call


o showDialog(int)
from within your Activity.

• It takes as parameter the ID of the dialog.

• You can also call it from within an anonymous inner


class

• Make sure you override the onCreateDialog() method


in that Activity!
Dialogs - Dismissing a Dialog

• You don't want to show the Dialog to the user forever!

• You have to allow the user to close the dialog somehow,


even if it's not cancelable

• You can dismiss the Dialog by calling


o dismissDialog(int) from within the controlling
Activity where the argument is the Dialog ID
o dismiss() on the Dialog object
 e.g. dialog.dismiss()
Dialogs - AlertDialog with a List

• Not only buttons!

• You can also add a list to your AlertDialog


Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};

@Override public Dialog onCreateDialog(int id) {


Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
We will use this
Dialogs - AlertDialog with a List
String array for our
list

String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};

@Override public Dialog onCreateDialog(int id) {


Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
This is a method in
an Activity class, as
in the previous
String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};
example

@Override public Dialog onCreateDialog(int id) {


Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands",
We're still using an "USA", "St. Martin", "Curacao"};
AlertDialog Builder
@Override public Dialog onCreateDialog(int id) {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};

@Override This
public Dialog
time onCreateDialog(int id) {
we call
setItems()!
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};

FirstonCreateDialog(int
@Override public Dialog argument id) {
should be your list of
Builder builder = new AlertDialog.Builder(this);
items
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};

@Override public Dialog onCreateDialog(int id) { There is more than


one OnClickListener
Builder builder = new AlertDialog.Builder(this);
class!
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands",
If you"USA",
want to"St.
useMartin", "Curacao"};
both View.OnclickListener
(for buttons) and
DialogInterface.OnClickListener (for
@Override public Dialog onCreateDialog(int id) {
Dialogs), then you need to be more specific
here
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};

@Override public Dialog onCreateDialog(int id) {


When an item in the list is clicked,
Builder builder
Android = new
kindly AlertDialog.Builder(this);
provides you with
the Dialog object
builder.setTitle("Select itself, as well as
a Country");
the index of the clicked item
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Toast.LENGTH_LONG).show(); }
});

return builder.create();
}
Dialogs - AlertDialog with a List
String[] countries = new String[]{"Netherlands", "USA", "St. Martin", "Curacao"};

@Override public Dialog onCreateDialog(int id) {


Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Country");
builder.setItems(countries, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int index) {

Toast.makeText(getApplicationContext(),
"You selected " + countries[index],
Let's not forget to create
Toast.LENGTH_LONG).show();
the Dialog and return it }
});

return builder.create();
}
Dialogs - Custom Dialogs - SeekBar

• You can create your own Dialog if the standard Android


Dialogs are not suitable for your needs

• For example, there is no SeekBar Dialog, but you can


create your own
Dialogs - DialogFragment

• creating Dialogs by using the onCreateDialog() method


of an Activity is old school

• creating Dialogs by using a DialogFragment is new


school
Dialogs - DialogFragment

• DialogFragment also has an onCreateDialog() callback


method!
o i.e., both an Activity and a DialogFragment have an
onCreateDialog() callback method
Dialogs - DialogFragment

DialogFragment is slightly different than the other


Fragments we've seen so far

• We don't need to add it to the XML

• Nor do we need to add it to the UI using a


FragmentTransaction
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment Let's create our
Activity first ...

public class MyActivity extends Activity {


@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
Let's show the Dialog
when this Button is
setContentView(R.layout.main);
Buttonclicked!
button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
The Button was just clicked at this
point, so let's create a new
instance of MyDialogFragment,
button.setOnClickListener(new OnClickListener() {
which we will see in a few slides
@Override public void onClick(View v) {

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
Just call .show() on the Fragment!
This time we didn't need to use the
button.setOnClickListener(new OnClickListener() {
FragmentTransaction to add the
@Override public void onClick(View v) {
Fragment

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);

Just pass the FragmentManager and it


will take care
button.setOnClickListener(new of adding and{removing
OnClickListener()
@Overridethe Fragment
public for you v) {
void onClick(View

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);

Second argument
button.setOnClickListener(new OnClickListener() { is the tag
that you want to assign to the
@Override public void onClick(View v) {
Fragment

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
We will get back to this!
});
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment DialogFragment!

public class MyDialogFragment extends DialogFragment {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment
Dialog also has an
onCreateDialog(),
the proof is in
public class MyDialogFragment the
extends DialogFragment {
@Override
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment
This is how we get the
public class MyDialogFragment extends DialogFragment {
Context from within a
@Override Fragment, remember!?
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
We need to return a act.doPositiveClick();
Dialog!
}
});
return builder.create();
}
}
Dialogs - DialogFragment
You've
public class MyDialogFragment extends DialogFragment { seen this other
stuff before!
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {
The button in the Dialog
has now been clicked!
What do we do next?
@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {
Let's get a handle on the
Activity containing this
Fragment
@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("This is a DialogFragment!");
builder.setPositiveButton("OK", new OnClickListener() {

Let's call our own custom


@Override
method,
doPositiveClick() on the
public void onClick(DialogInterface dialog, int which) {
Activity
MyActivity act= (MyActivity) getActivity();
act.doPositiveClick();
}
});
return builder.create();
}
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
Which takes us back here,
}); back to our Activity.
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment
public class MyActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {

MyDialogFragment f = new MyDialogFragment();


f.show(getFragmentManager(), "dialog");
}
Simply make a Toast as
evidence});that we were
successful
}

public void doPositiveClick() {


Toast.makeText(this, "doPositiveClick()", Toast.LENGTH_LONG).show();
}
Dialogs - DialogFragment

• Note that we don't need to override onCreateView() or


onActivityCreated() methods of a DialogFragment

• You may choose to override onCreateView() and return


a View if you want to create some custom Dialog
without using onCreateDialog()

• I'll leave it up to the Android developer's website to


explain it if anyone is interested in doing so
Menu Options

• In Android 2.3.x and below, clicking on the dedicated


Menu button allows the user to reveal menu options

• In Android 3.0 and above, the options menu is


presented by way of an action bar
o the dedicated Menu button is deprecated and some
devices just do not have one
Menu Options - Creating one <=
2.3.x

• Right click on your project > New > Other ...

• Select Android XML File

• In the Resource Type drop down list, select Menu

• Enter a File name and click Finish

• Click Add ... > Item

• Edit the id as appropriate, and enter the Title

• Repeat to add additional menu options


Menu Options - Creating one <=
2.3.x

We will use this Menu XML file, main_menu.xml, for our


example

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/set_text"
android:title="@string/set_text_opt"/>
<item android:id="@+id/close"
android:title="@string/close_opt" />
</menu>
Menu Options - Creating one <=
2.3.x
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Menu Options - Creating one <=
2.3.x
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} Override this Activity method

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Menu Options - Creating one <=
2.3.x
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Similar to a LayoutInflater,
but for Menus instead
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Menu Options - Creating one <=
2.3.x
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
Show the Menu now

public boolean onCreateOptionsMenu(Menu menu) {


MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Menu Options - Creating one <=
2.3.x
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override This is the Menu XML file


that we created previously
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Menu Options - Creating one <=
2.3.x
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

This is given as argument to


onCreateOptionsMenu, so
@Override use it as argument to inflate
the menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Menu Options - Creating one <=
2.3.x
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
Return true if you want the
public boolean
menu to beonCreateOptionsMenu(Menu
displayed, false if menu) {
you don't
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Menu Options - Creating one <=
2.3.x

• This is enough for the Menu to be displayed on the


screen when the user presses the Menu button

• If you want to take action after an option is selected,


then you need to override the onOptionsItemSelected()
method of Activity
Menu Options - Creating one <=
2.3.x
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.set_text:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("First Option Selected!");
break;
case R.id.close:
Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show();
finish();
break;
}

return true;
}
Menu Options - Creating one <=
2.3.x We override this
method of Activity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.set_text:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("First Option Selected!");
break;
case R.id.close:
Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show();
finish();
break;
}

return true;
}
Menu Options - Creating one <=
2.3.x
Which menu item was
selected?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.set_text:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("First Option Selected!");
break;
case R.id.close:
Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show();
finish();
break;
}

return true;
}
Menu Options - Creating one <=
2.3.x
@Override
Here we just changed the text of a
TextView item)
public boolean onOptionsItemSelected(MenuItem when{the item
R.id.set_text is selected
switch (item.getItemId()) {
case R.id.set_text:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("First Option Selected!");
break;
case R.id.close:
Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show();
finish();
break;
}

return true;
}
Menu Options - Creating one <=
2.3.x
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.set_text:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("First Option Selected!");
Here we close ourbreak;
app when item
R.id.close is selected
case R.id.close:
Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show();
finish();
break;
}

return true;
}
Menu Options - Creating one <=
2.3.x

You can change the menu options that show up at runtime


Menu Options - Creating one >= 3.0

For Android 3.0 and higher ...

http://developer.android.com/guide/topics/ui/actionbar.html
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.

• You can provide a context menu for any view, but they
are most often used for items in a
o ListView
o other view collections in which the user can perform
direct actions on each item.
Context Menu - Creating one

When creating a Context Menu, you can create


a Menu XML file in the same way you do for an
Options Menu
Context Menu - Creating one

We will use the following XML file for our example

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/edit_option" android:title="Edit"></item>
<item android:id="@+id/share_option" android:title="Share"></item>
<item android:id="@+id/delete_option" android:title="Delete"></item>
</menu>
Context Menu - Creating one

We will use the following XML file for our example

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/edit_option" android:title="Edit"></item>
<item android:id="@+id/share_option" android:title="Share"></item>
<item android:id="@+id/delete_option" android:title="Delete"></item>
</menu>

All of the callback methods in this example are declared within our ListActivity
Context Menu - Creating one

As a reminder,
• a ListActivity extends Activity
• it already has a ListView, so you don't need to add one
the the Layout XML file
o you don't even need to use a Layout XML File
o which means you don't need to call
setContentView()
• You can get a handle on the ListView by simply calling
getListView().
Context Menu - Creating one
public class ContextMenuExample extends ListActivity {
String[] entries = new String[]{"Martin","Anderson","Junior","George","Dan"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, entries));

registerForContextMenu(getListView());
}

...
Context Menu - Creating one
We will use this String
array to populate our
List
public class ContextMenuExample extends ListActivity {
String[] entries = new String[]
{"Martin","Anderson","Junior","George","Dan"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, entries));

registerForContextMenu(getListView());
}

...
Context Menu - Creating one
public class ContextMenuExample extends ListActivity {
String[] entries = new String[]{"Martin","Anderson","Junior","George","Dan"};

Populate the ListView


@Override
here
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, entries));

registerForContextMenu(getListView());
}

...
Context Menu - Creating one
public class ContextMenuExample extends ListActivity {
String[] entries = new String[]{"Martin","Anderson","Junior","George","Dan"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Register the ListView with a
Context Menu, now the menu
this.setListAdapter(new
will show up when you long ArrayAdapter<String>(this,
press on the ListView
android.R.layout.simple_list_item_1, entries));

registerForContextMenu(getListView());
}

...
Context Menu - Creating one
public class
onCreate() ContextMenuExample extends ListActivity {
from
previous slide is here
String[] entries = new String[]{"Martin","Anderson","Junior","George","Dan"};

...

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}

...
Context Menu - Creating one
public class ContextMenuExample extends ListActivity {
String[] entries = new String[]{"Martin","Anderson","Junior","George","Dan"};
Override this method.
It is called when the
... Context Menu for View
v is being built

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}

...
Context Menu - Creating one
public class ContextMenuExample extends ListActivity {
String[] entries = new String[]{"Martin","Anderson","Junior","George","Dan"};

...

@Override
public void onCreateContextMenu(ContextMenu
You've seen this before menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}

...
Context Menu - Creating one
public class ContextMenuExample extends ListActivity {
String[] entries = new String[]{"Martin","Anderson","Junior","George","Dan"};

...

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
onContextItemSelected() on
inflater.inflate(R.menu.context_menu,
the next slide goes here menu);
}

...
Context Menu - Creating one
Override this method of
Activity

@Override public boolean onContextItemSelected(MenuItem item) {


AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
TextView tv = (TextView) getListView().getAdapter().getView(info.position, null, null);
switch(item.getItemId()) {
case R.id.edit_option:
/* Edit option selected */
break;
case R.id.share_option:
/* Share option selected */
break;
case R.id.delete_option:
/* Edit option selected */
break;
}
return true;
}
This Object has

Context Menu - Creating one information about the


Context Menu, NOT
the item in the List

@Override public boolean onContextItemSelected(MenuItem item) {


AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
TextView tv = (TextView) getListView().getAdapter().getView(info.position, null, null);
switch(item.getItemId()) {
case R.id.edit_option:
/* Edit option selected */
break;
case R.id.share_option:
/* Share option selected */
break;
case R.id.delete_option:
/* Edit option selected */
break;
}
return true;
}
Context Menu - Creating one This Object will have
information about the
item pressed
@Override public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
TextView tv = (TextView) getListView().getAdapter().getView(info.position, null, null);
switch(item.getItemId()) {
case R.id.edit_option:
/* Edit option selected */
break;
case R.id.share_option:
/* Share option selected */
break;
case R.id.delete_option:
/* Edit option selected */
break;
}
return true;
}
Context Menu - Creating one We can get a handle
on the item in the
ListView by using
@Override public boolean onContextItemSelected(MenuItem item) {
info.position
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
TextView tv = (TextView) getListView().getAdapter().getView(info.position, null, null);
switch(item.getItemId()) {
case R.id.edit_option:
/* Edit option selected */
break;
case R.id.share_option:
/* Share option selected */
break;
case R.id.delete_option:
/* Edit option selected */
break;
}
return true;
}
Context Menu - Creating one
info.position tells us which item was
@Override public boolean
pressed, onContextItemSelected(MenuItem
this is how we tell which item) {
Menu Item was pressed
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
TextView tv = (TextView) getListView().getAdapter().getView(info.position, null, null);
switch(item.getItemId()) {
case R.id.edit_option:
/* Edit option selected */
break;
case R.id.share_option:
/* Share option selected */
break;
case R.id.delete_option:
/* Edit option selected */
break;
}
return true;
}
Context Menu - Creating one
@Override public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
TextView tv = (TextView) getListView().getAdapter().getView(info.position, null, null);
switch(item.getItemId()) {
case R.id.edit_option:
/* Edit option selected */
break;
By combining info.position and
case R.id.share_option:
item.getItemId(), we know the
/* Share option selected */ action needs to be performed on
the item
break;
case R.id.delete_option:
/* Edit option selected */
break;
}
return true;
}

You might also like