1 / 29

Mobile Computing

Mobile Computing. Lecture#11 Adapters and Dialogs. Lecture Contents. Adapter SimpleCursorAdapter ArrayAdapter Dialog Dialog Class Dialog-themed Activity Toasts. Adapters. Adapters are bridging classes that bind data to Views (such as List Views) used in the user interface.

Télécharger la présentation

Mobile Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mobile Computing Lecture#11 Adapters and Dialogs

  2. Lecture Contents • Adapter • SimpleCursorAdapter • ArrayAdapter • Dialog • Dialog Class • Dialog-themed Activity • Toasts

  3. Adapters Adapters are bridging classes that bind data to Views (such as List Views) used in the user interface. The adapter is responsible for • Creating the child Views used to represent each item within the parent View (say a ListView) • Providing access to the underlying data (say an ArrayList)

  4. Simple Cursor Adapter • An easy adapter to map columns from a cursor to TextViews or ImageViews • You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views.

  5. Simple Cursor Adapte Example

  6. Simple Cursor Adapte Example

  7. ArrayAdapter • The Array Adapter uses generics to bind an Adapter View to an array of objects of the specified class • By default the Array Adapter uses the toString() value of each object in the array to create and populate Text Views • Alternative constructors enable you to use more complex layouts • You can even extend the class to use alternatives to Text Views (to display data)

  8. ArrayAdapter Example Steps Involved:::: • Layout Definitions • Adapter Class • Activity to show elements

  9. ArrayAdapter Example (Adapter Class)

  10. ArrayAdapter Example (Main Class)

  11. ArrayAdapter Example (row.xml)

  12. ArrayAdapter Example (main.xml)

  13. ArrayAdapter Example (Output)

  14. ArrayAdapter Complex Example Steps Involved:::: • Layout Definitions • View Data Class • Adapter Class • Activity to show elements

  15. Layouts

  16. Data Class

  17. Student Adapter

  18. Main

  19. Dialogs • Dialog boxes are a common UI metaphor in desktop, web, and mobile applications • They’re used to help users answer questions, make selections, and confirm actions, and to display warning or error messages • Dialog boxes in Android are partially transparent, floating Activities that appear upon activities that launch them

  20. Implementing Dialogs There are three ways to implement a dialog in Android: • Using Dialog class • Dialog-themed Activity • Toasts

  21. Using Dialog Class or Its Extensions • Android includes a number of specialist classes that extend Dialog • Each is designed to provide specific dialog-box functionality • Dialog-class-based screen is constructed entirely within its calling Activity, so it doesn’t need to be registered in the manifest • Dialog’s life cycle is controlled entirely by the calling Activity

  22. Dialog Creation Dialog d = new Dialog(MyActivity.this); // Have the new window tint and blur the window it obscures. Window window = d.getWindow(); int flag = WindowManager.LayoutParams.FLAG_BLUR_BEHIND; window.setFlags(flag, flag); d.setTitle("Dialog Title"); // Set title d.setContentView(R.layout.dialog_view); // Inflate layout // Find the TextView used in the layout and set its text value TextView text = (TextView)d.findViewById(R.id.dialog); text.setText("This is the text in my dialog"); d.show();

  23. Dialog output

  24. Dialog with Buttons final Context context = Main.this; String title = "It is Pitch Black"; String message = "You are likely to be eaten by a ghost"; String button1String = "Go Back"; String button2String = "Move Forward"; AlertDialog.Builder ad = new AlertDialog.Builder(context); ad.setTitle(title); d.setMessage(message); ad.setPositiveButton(button1String, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { //Do something positive } }); ad.setNegativeButton(button2String, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int arg1) { //Do something negative } }); ad.show();

  25. Dialog with Buttons

  26. Toast • Android allows us to display a message without any button like 'OK' or 'Cancel' • Its helps us to display a short message in short time • Call Toast.makeText() • Provide necessary arguments (Context, Message, Duration) • Call show() message to display toast

  27. Syntax/Example • Toast Syntax Toast.makeText(Context, Message, Duration).show() ToastExample::::: Toast.makeText(MyActivity.this, “You have been assigned a project”, Toast.LENGTH_LONG).show();

  28. Custom Toast Toast toast = new Toast(MyActivity.this); TextViewtoastView = new TextView(MyActivity.this); toastView.setText("Here goes your message"); toastView.setBackgroundColor(Color.WHITE); toastView.setTextColor(Color.RED); toastView.setPadding(10,10,10,10); toast.setView(toastView); toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();

  29. Regular/Custom Toast

More Related