cosc 5/4730 dialogs 2.3.3 and below 3.0 and above (fragment)

22
Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Upload: harvey-black

Post on 02-Jan-2016

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Cosc 5/4730

Dialogs 2.3.3 and below

3.0 and above (fragment)

Page 2: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Dialogs

• So most of this is depreciated in API 13+, but it is encapsulated in a fragment, plus if you are writing from below API 13 (honeycomb) it’s worth knowing.

Page 3: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Dialog

• A dialog is always created and displayed as a part of an Activity. – You should normally create dialogs from within

your Activity's onCreateDialog(int) callback method

Page 4: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Dialog

• Using the createDialog() method– All dialog creation is done there.– Only called the first time specfic dialog is created.

• Use the onPrepareDialog(int, Dialog)– Every time the dialog is called, you can modify it

• In you activity:– showDialog(DIALOG_PAUSED_ID);• Where is the ID is the dialog you want to display.

Page 5: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Creating a Dialog

• There are several dialogs you can create– AlertDialog, which can have up to 3 buttons

– ProgressDialog

– DatePickerDialog and TimePickerDialog

– Custom dailog, which you create the layout.

Page 6: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

AlertDialog

• Use the AlertDialog.Builder to create the dialog– You can have up to three buttons, “positive”,

“negative” and cancel– To create the following Dialog• Set the positive and negative, and disable cancel.

Page 7: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

AlertDialog (2)• Code: with two listeners, one for each button.AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("Winner!")

.setCancelable(false)

.setPositiveButton("Next Level", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {dialog.dismiss();//next(); }}).setNegativeButton("Quit", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {finish();}});

dialog = builder.create();

Page 8: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

AlertDialog (3)

• Instead of buttons, you can have it as radio buttons (and more then just three);– Use Builder.setSignleChoiceItems• Where we send a CharSequence[] of items.

• final String[] items = {"Remove Walls", "Add Walls", "Add/Remove Objects", "Add/Remove Score"};

Page 9: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

AlertDialog (4)• code:final String[] items = {"Remove Walls", "Add Walls", "Add/Remove Objects", "Add/Remove Score"};AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Choose Type:");builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) {

dialog.dismiss();if (item == 0) { //remove walls}

}});dialog = builder.create();

Page 10: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Custom Dialogs

• You need to create a layout xml file for the dialog– Then dialog.setContentView(R.layout.youlayout)– Using dialog.findViewByID(R.id.X) to access each

widget to set images and/or listeners as necessary– dialog.setTitle(“your title”);

– Now it’s ready to be shown.

Page 11: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Examples

• The previous examples are all shown in dialogs233Activity in the DialogDemo

Page 12: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

DialogFragments

• The showDialog is depreicated in API 13+– Links and discussion of dialogfragment vs alertdiags:

http://stackoverflow.com/questions/13765127/dialogfragment-advantages-over-alertdialog

• Basically, I’m just going to show how it works.• What it comes down to is that you are creating

custom dialogs. – But we can still use the alertdialog creators in the

dialogfragment as well.

Page 13: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

DialogFragment

• Basically a fragment and everything that we have already done.

• Or you implement onCreateDialog(Bundle)– Instead of (or in addition to ) onCreateView

• DialogFragments can be added to a framelayout just like fragments OR we can show it like a dialog.– All the demos are going to show it, instead putting in

the framelayout, since you already saw how to do it.

Page 14: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

EditNameDialogFrag example

• Looking at the code, it is a DialogFragment (using support library or without is the same)

• Create a callback as we have done before• In onCreateView– Inflate the layout, return the view.

• Just like we have done before.

• The change is in the FragmentDialogActivity.FragmentManager fm = getSupportFragmentManager();EditNameDialogFrag editNameDialog = new EditNameDialogFrag();editNameDialog.show(fm, "fragment_edit_name");

Show function, causes the dialogFragment to popup on the screen.

Page 15: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

EditNameDialogFrag example (2)

• So we have the dialog

Page 16: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

EditNameDialogFrag and keyboard

• Changing the keyboard return button to done.– In the xml for the EditText

<EditText android:id="@+id/txt_your_name"… android:inputType="text" android:imeOptions="actionDone" />

– Changes keyboard – Setup in the listener if (EditorInfo.IME_ACTION_DONE == actionId)– We know when they done editing/typing.– A note, this doesn’t work correctly in the emulators, so I

added done button to the example.

Page 17: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

DialogFragment and AlertDialog

• We can embed the alertDialogs into the FragmentDialog, so you can use the “default” dialogs as well.– Shown in code DialFragActivity and

AlertDialogFrag1 and myDialogFragment

Page 18: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

DialogFragment with AlertDialog

• Two pieces for the Dialog Fragment• newInstance– Creates a new dialogFragment, instead doing this

in the Activity.• onCreateDialog– Creates the AlertDialog via the builder just like

before.• Then use callbacks in the positive/negative

Page 19: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

DialogFragment with AlertDialog (2)

• In the fragment new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mlistener.doPositiveClick(); //using “normal” callbacks. }}

• In the Activity:public void doPositiveClick() { // Do stuff here.}

Page 20: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

Example code

• DialFragActivity – AlertDialogFrag1• Cancel/Ok dialog

– myDialogFragment• Implements the two dialog from the beginning of the

lecture No/Yes, and a ListDialog

– There is a doPositivieClick(), doNegativeClick(), and doItem(String) for these dialog• Implemented via the callbacks.

Page 22: Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)

QA&