Summary

This document covers Dialogs in Android application development, including types, creation, and usage . The examples include AlertDialog, creating dialogs, and using dialog fragments. Various options and methodologies are presented for developers.

Full Transcript

12/1/2024 Dialogs 1 Dialog A dialog is a small window that prompts the user to provide additional information without taking up the entire screen. Such as : Prompt users for decisions (e.g., Save, Cancel, Exit). Display...

12/1/2024 Dialogs 1 Dialog A dialog is a small window that prompts the user to provide additional information without taking up the entire screen. Such as : Prompt users for decisions (e.g., Save, Cancel, Exit). Display additional information (e.g., Progress, Date/Time). 2 1 12/1/2024 Dialog Type AlertDialog: Standard dialog with up to three buttons (e.g., Positive, Negative, Cancel). ProgressDialog: Displays progress (deprecated for general use in newer APIs). DatePickerDialog and TimePickerDialog: Predefined dialogs for selecting dates or times. Custom Dialogs: User-defined layouts incorporated into a dialog. 3 Creating a Dialog 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. 4 2 12/1/2024 Code: with two listeners, one for each button. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Winner!").setCancelable(false).setPositiveButton("Next Level", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss();}.setNegativeButton("Quit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { finish(); } builder.show(); 5 AlertDialog 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"}; 6 3 12/1/2024 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) {} Builder.show(); 7 DialogFragment Instead of using an the alertdialog builder, you can use dialogFragment. you implement onCreateDialog(Bundle) Instead of (or in addition to ) onCreateView DialogFragments can be added to a framelayout just like fragments OR we can use the show method All the demos are going to use show, instead putting in the framelayout, since you already saw how to do it. 8 4 12/1/2024 Why Use DialogFragments? Lifecycle-aware. Better alignment with fragments in modern Android. 9 DialogFragment To show the fragment, use the following as an example: FragmentManager fm = getSupportFragmentManager(); EditNameDialogFrag editNameDialog = new EditNameDialogFrag(); editNameDialog.show(fm, "fragment_edit_name"); Show function, causes the dialogFragment to popup on the screen. 10 5 12/1/2024 extends DialogFragment and usesAlertDialog @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(requireActivity()); FragmentEditNameBinding binding = FragmentEditNameBinding.inflate(inflater); AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(requireActivity(), R.style.ThemeOverlay_AppCompat_Dialog)); builder.setView(binding.getRoot()).setTitle("Hello"); builder.setPositiveButton("Done", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { mListener.onFinishEditDialog(binding.txtYourName.getText().toString()); dismiss(); } }).setCancelable(false); Dialog dialog = builder.create(); return dialog; 11 Input Keyboard, touch, controllers, and device sensors 12 6 12/1/2024 View For most input, you add a custom listener to a View class EditView has a keyboard listener, since it accepts input. You add a listener to a View (widgets) and then it only work for that widget You can’t have a listener for a “screen” Unless the view takes up the entire screen The exception is the Sensor, which is for the device. 13 View There is a View class that you can extend and use to create custom Views We’ll use an ImageView widget and add listeners 14 7 12/1/2024 A Note View will need several attributed in order to work with these listeners In the xml you will need to add android:focusable="true" android:focusableInTouchMode="true" android:clickable="true" 15 Keyboard input 16 8 12/1/2024 View.OnKeyListener For "keyboard" input Implement the View.OnKeyListener And override public boolean onKey(View v, int keyCode, KeyEvent event) Note, that there is not a char field here. You get the KeyCode as a parameter or event.getKeyCode() 17 View.OnKeyListener What key was pressed? Use event.getMatch(char[] chars) Pass it an array of characters you want to test against. If it matches, then returns that character Else returns ‘\0’ Use keycode == KeyEvent. Constants Example: KeyEvent.KEYCODE_0 for Zero Gives you access to any key that was pushed: KEYCODE_CAMERA, KEYCODE_DPAD_LEFT KEYCODE_ENDCALL, KEYCODE_VOLUME_DOWN, etc 18 9 12/1/2024 View.OnKeyListener Add the listener to the view ImageView in our case iv.setOnKeyListener(new myKeyListener()); When the ImageView has focus, then the keylistener will be called for any key events. 19 View Overrides When extending a View class you also can override several more You also override them in an activity as well. onKeyDown(int, KeyEvent) Called when a new key event occurs. onKeyUp(int, KeyEvent) Called when a key up event occurs. onTrackballEvent(MotionEvent) Called when a trackball motion event occurs. There is also a touchEvent 20 10 12/1/2024 Touch input 21 Touch Events. There is an OnTouchListener But you can also use the OnClickListener and OnLongClickListener as well. Normally associated with buttons. 22 11 12/1/2024 View.OnTouchListener Implement the View.OnTouchListener And override public boolean onTouch(View v, MotionEvent event) return true if event consumed, false otherwise. the event has all the information about the touch event. 23 View.OnTouchListener MotionEvent getX(), getY() returns the X, Y location of the touch in the Widget Not the position on the screen. getRawX(), getRawY() returns the original raw X and Y coordinate, which is the position on the screen. getAction() Return the kind of action being performed one of either ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_CANCEL. 24 12 12/1/2024 View.OnTouchListener MotionEvent getX(), getY() returns the X, Y location of the touch in the Widget Not the position on the screen. getRawX(), getRawY() returns the original raw X and Y coordinate, which is the position on the screen. getAction() Return the kind of action being performed one of either ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_CANCEL. 25 13

Use Quizgecko on...
Browser
Browser