Your SlideShare is downloading. ×
0
1
Android UI Dialog
Topics
• What is a Dialog?
• AlertDialog
• ProgressDialog
• Creating a custom Dialog
What is a Dialog?
• A dialog is usually a small window that appears
in front of the current Activity.
• The underlying Act...
Types of Dialog
• AlertDialog
• ProgressDialog
• DatePickerDialog
• TimePickerDialog
AlertDialog
What is AlertDialog?
• An AlertDialog is an extension of the Dialog
class.
• It is capable of constructing most dialog use...
How to Create AlertDialog?
• Get a Builder with AlertDialog.Builder(Context)
and then use the class's public methods to
de...
Adding Buttons to AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sur...
Adding a list to AlertDialog
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new Aler...
Adding checkboxes and radio buttons
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = n...
ProgressDialog
What is ProgressDialog?
• A ProgressDialog is an extension of the
AlertDialog class that can display a progress
animation ...
Creating a Progress Bar?
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setP...
Creating a Custom
Dialog
Steps for Creating a Custom Dialog
• Step #1 - Create an XML layout
• Step #2 - Set the above layout as the dialog's
conte...
Step #1: Create XML Layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layou...
Step #2: Create Dialog
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContent...
Thank you
Upcoming SlideShare
Loading in...5
×

Android ui dialog

2,157

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,157
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
51
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Android ui dialog"

  1. 1. 1 Android UI Dialog
  2. 2. Topics • What is a Dialog? • AlertDialog • ProgressDialog • Creating a custom Dialog
  3. 3. What is a Dialog? • A dialog is usually a small window that appears in front of the current Activity. • The underlying Activity loses focus and the dialog accepts all user interaction. • Dialogs are normally used for notifications and short activities that directly relate to the application in progress.
  4. 4. Types of Dialog • AlertDialog • ProgressDialog • DatePickerDialog • TimePickerDialog
  5. 5. AlertDialog
  6. 6. What is AlertDialog? • An AlertDialog is an extension of the Dialog class. • It is capable of constructing most dialog user interfaces and is the suggested dialog type. • You should use it for dialogs that use any of the following features > A title > A text message > One, two, or three buttons > A list of selectable items (with optional checkboxes or radio buttons)
  7. 7. How to Create AlertDialog? • Get a Builder with AlertDialog.Builder(Context) and then use the class's public methods to define all of the AlertDialog properties. • After you're done with the Builder, retrieve the AlertDialog object with create()
  8. 8. Adding Buttons to AlertDialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MyActivity.this.finish(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create();
  9. 9. Adding a list to AlertDialog final CharSequence[] items = {"Red", "Green", "Blue"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Pick a color"); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } }); AlertDialog alert = builder.create();
  10. 10. Adding checkboxes and radio buttons final CharSequence[] items = {"Red", "Green", "Blue"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Pick a color"); builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } }); AlertDialog alert = builder.create();
  11. 11. ProgressDialog
  12. 12. What is ProgressDialog? • A ProgressDialog is an extension of the AlertDialog class that can display a progress animation in the form of > a spinning wheel, for a task with progress that's undefined, or > a progress bar, for a task that has a defined progression. • The dialog can also provide buttons, such as one to cancel a download.
  13. 13. Creating a Progress Bar? ProgressDialog progressDialog; progressDialog = new ProgressDialog(mContext); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage("Loading..."); progressDialog.setCancelable(false);
  14. 14. Creating a Custom Dialog
  15. 15. Steps for Creating a Custom Dialog • Step #1 - Create an XML layout • Step #2 - Set the above layout as the dialog's content view and define the content for the view elements • Step #3 - Show the dialog Suppose we want to create a Custom dialog as shown below
  16. 16. Step #1: Create XML Layout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> </LinearLayout>
  17. 17. Step #2: Create Dialog Context mContext = getApplicationContext(); Dialog dialog = new Dialog(mContext); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Custom Dialog"); TextView text = (TextView) dialog.findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.drawable.android);
  18. 18. Thank you
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×