1.39k likes | 1.79k Views
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.
E N D
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 • 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. • 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 • a title • a text message • one, two, or three buttons • 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; } }
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; } }
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; } }
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
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
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
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.
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!"
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.
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()
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!
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"
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!
Dialogs - Showing a Dialog See AlertDialogExample
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()
Dialogs - Showing a Dialog See DismissDialogExample
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(); }
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(); }
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(); }
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(); }
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()!
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
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!
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
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
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
Dialogs - AlertDialog with a List See AlertDialogListExample
Dialogs - Date/TimePicker Dialogs See DatePickerDialogExample
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
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! • 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); 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!
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
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
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
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
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!
Dialogs - DialogFragment Let's take a look at our DialogFragment
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(); } }