Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Cosc 5/4730 Dialogs 2.3.3 and below 3.0 and above (fragment)"— Presentation transcript:

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

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

3 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

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

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

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

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

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

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

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

11 Examples The previous examples are all shown in dialogs233Activity in the DialogDemo

12 DialogFragments The showDialog is depreicated in API 13+ – Links and discussion of dialogfragment vs alertdiags: http://stackoverflow.com/questions/13765127/di alogfragment-advantages-over-alertdialog http://stackoverflow.com/questions/13765127/di alogfragment-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.

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

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

15 EditNameDialogFrag example (2) So we have the dialog

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

17 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

18 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

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

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

21 DialogFragment References http://developer.android.com/reference/andr oid/app/DialogFragment.html http://developer.android.com/reference/andr oid/app/DialogFragment.html http://android- developers.blogspot.com/2012/05/using- dialogfragments.html http://android- developers.blogspot.com/2012/05/using- dialogfragments.html http://stackoverflow.com/questions/1291218 1/simplest-yes-no-dialog-fragment http://stackoverflow.com/questions/1291218 1/simplest-yes-no-dialog-fragment

22 Q A &


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

Similar presentations


Ads by Google