1 / 131

Mobile Programming Lecture 7

Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences. Agenda. Dialogs Menus SharedPreferences. Android Application Components. Activity 2. Broadcast Receiver 3. Content Provider 4. Service. Dialogs.

deon
Download Presentation

Mobile Programming Lecture 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences

  2. Agenda • Dialogs • Menus • SharedPreferences

  3. Android Application Components • Activity 2. Broadcast Receiver 3. Content Provider 4. Service

  4. 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

  5. Dialogs • onCreateDialog() is called the first time. • onPrepareDialog is called every time its opened. • without this, it will remain the same as the first time it was opened

  6. 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

  7. Dialogs - AlertDialog • you should use it for dialogs that use any of the following features • a title • a text message • one, two, or three buttons • a list of selectable items (with optional checkboxes or radio buttons)

  8. 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; } }

  9. Dialogs - Creating an AlertDialog Nothing special here, just an 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; } }

  10. Dialogs - Creating an AlertDialog Override this method of Activity, which is called when you want to show any dialog of the Activity 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; } }

  11. 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; } } Switch because we can have more than one dialog, meaning we need to check the dialog id

  12. 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; } } If you want an AlertDialog, you need to build one first

  13. 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; } } Give the user a message

  14. 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; } } If true, then the user can press the back button to dismiss the dialog. false would force the user to make an action on the dialog.

  15. 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; } } Add a button, AND set a listener for when the button is pressed. This is should be the "positive" button, e.g. "Yes", "Absolutely!"

  16. 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; } } This is the listener for then the "positive" button is pressed, so you should take some kind of action.

  17. 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; } } The Dialog isn't created until you call create()

  18. 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; } } The type of this method is Dialog, so here we return ... the Dialog!

  19. Dialogs - AlertDialog You can add up to 3 buttons on the AlertDialog by calling • dialog.setPositiveButton() • we just used this one in the previous slides • dialog.setNegativeButton() • "No", "Cancel" • dialog.setNeutralButton() • "Remind me Later"

  20. Dialogs - Showing a Dialog • To show a Dialog on the screen, simply call • 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!

  21. Dialogs - Showing a Dialog See AlertDialogExample

  22. 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 • dismissDialog(int) from within the controlling Activity where the argument is the Dialog ID • dismiss() on the Dialog object • e.g. dialog.dismiss()

  23. Dialogs - Showing a Dialog See DismissDialogExample

  24. Dialogs - AlertDialog with a List • Not only buttons! • You can also add a list to your AlertDialog

  25. 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(); }

  26. Dialogs - AlertDialog with a List We will use this 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(); }

  27. Dialogs - AlertDialog with a List This is a method in an Activity class, as in the previous example 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(); }

  28. Dialogs - AlertDialog with a List We're still using an AlertDialog Builder 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(); }

  29. 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(); } This time we call setItems()!

  30. 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(); } First argument should be your list of items

  31. 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(); } There is more than one OnClickListener class!

  32. 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(); } If you want to use both View.OnclickListener (for buttons) and DialogInterface.OnClickListener (for Dialogs), then you need to be more specific here

  33. 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(); } When an item in the list is clicked, Android kindly provides you with the Dialog object itself, as well as the index of the clicked item

  34. 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(); } Let's not forget to create the Dialog and return it

  35. Dialogs - AlertDialog with a List See AlertDialogListExample

  36. Dialogs - Date/TimePicker Dialogs See DatePickerDialogExample

  37. 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 • See CustomDialogExample

  38. Dialogs - DialogFragment • creating Dialogs by using the onCreateDialog() method of an Activity is old school • creating Dialogs by using a DialogFragment is new school

  39. Dialogs - DialogFragment • DialogFragment also has an onCreateDialog() callback method! • i.e., both an Activity and a DialogFragment have an onCreateDialog() callback method

  40. 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

  41. 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(); } }

  42. 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(); } }

  43. 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(); } } Let's show the Dialog when this Button is clicked!

  44. 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(); } } The Button was just clicked at this point, so let's create a new instance of MyDialogFragment, which we will see in a few slides

  45. 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(); } } Just call .show() on the Fragment! This time we didn't need to use the FragmentTransaction to add the Fragment

  46. 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(); } } Just pass the FragmentManager and it will take care of adding and removing the Fragment for you

  47. 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(); } } Second argument is the tag that you want to assign to the Fragment

  48. 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(); } } We will get back to this!

  49. Dialogs - DialogFragment Let's take a look at our DialogFragment

  50. 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(); } }

More Related