Beruflich Dokumente
Kultur Dokumente
Mobile Computing
Adapters, Dialogs
Lecture#08
2
Lecture Contents
vAdapter
vSimpleCursorAdapter
vArrayAdapter
vDialog
vDialog Class
vDialog-themed Activity
vToasts
v
v
3
Adapters
Adapters are bridging classes that bind data to
Views (such as List Views) used in the user
interface.
ArrayAdapter
vThe Array Adapter uses generics to bind an
Adapter View to an array of objects of the
specified class
vBy default the Array Adapter uses the toString()
value of each object in the array to create and
populate Text Views
vAlternative constructors enable you to use more
complex layouts
vYou can even extend the class to use alternatives
to Text Views (to display data)
8
ArrayAdapter Example
Steps Involved::::
1.Layout Definitions
2.Adapter Class
3.Activity to show elements
9
1.Layout Definitions
2.View Data Class
3.Adapter Class
4.Activity to show elements
15
Layouts
16
Data Class
17
Student Adapter
18
Main
19
Dialogs
vDialog boxes are a common UI metaphor in
desktop, web, and mobile applications
vThey’re used to help users answer questions,
make selections, and confirm actions, and to
display warning or error messages
vDialog 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:
1.Using Dialog class
2.Dialog-themed Activity
3.Toasts
21
Dialog Creation
Dialog d = new Dialog(MyActivity.this);
// Have the new window tint and blur the window it obscures.
// Find the TextView used in the layout and set its text value
d.show();
23
Dialog output
Dialog with Buttons 24
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
vAndroid allows us to display a message without any
button like 'OK' or 'Cancel'
vIts 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
vToast Syntax
Toast.makeText(Context, Message, Duration).show()
ToastExample:::::
Toast.makeText(MyActivity.this, “You have been
Custom Toast
Toast toast = new Toast(MyActivity.this);
TextView toastView = new TextView(MyActivity.this);
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