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

Post on 02-Jan-2016

217 views 1 download

transcript

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.

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

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.

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.

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.

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();

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"};

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();

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.

Examples

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

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.

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.

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.

EditNameDialogFrag example (2)

• So we have the dialog

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.

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

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

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

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.

QA&