Header Ads

Simple custom dialog android

In this tutorial, we show you how to create a custom dialog in Android. Following steps :
  1. Create a custom dialog layout (XML file).
  2. Attach the layout to Dialog.
  3. Display the Dialog.
  4. Done.


1 : Customedialog.xml 
//Create a custom dialog layout (XML file)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. android:paddingLeft="50dp"
  7. android:paddingRight="50dp">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="40dp"
  11. android:gravity="center"
  12. android:background="@drawable/header"
  13. android:orientation="vertical">
  14. <TextView
  15. android:id="@+id/textView2"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="Custome dialog"
  19. android:textColor="#ffffff"
  20. android:textSize="18sp"
  21. android:textStyle="bold" />
  22. </LinearLayout>
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:background="#fffccc"
  27. android:orientation="vertical"
  28. android:padding="10dp">
  29. <LinearLayout
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:orientation="vertical" >
  33. <TextView
  34. android:id="@+id/content_add"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_gravity="center"
  38. android:gravity="center"
  39. android:text="Your message"
  40. android:paddingTop="10dp"
  41. android:textColor="#000000"
  42. android:paddingBottom="10dp"
  43. android:textSize="18sp" />
  44. </LinearLayout>
  45. <LinearLayout
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_gravity="center"
  49. android:layout_marginLeft="5dp"
  50. android:layout_marginRight="5dp"
  51. android:orientation="horizontal" >
  52. <TextView
  53. android:id="@+id/btn_yes"
  54. android:layout_width="70dip"
  55. android:layout_height="wrap_content"
  56. android:background="#006789"
  57. android:gravity="center"
  58. android:text="Yes"
  59. android:padding="5dp"
  60. android:textColor="#ffffff"
  61. android:textSize="18sp" />
  62. <TextView
  63. android:id="@+id/btn_no"
  64. android:layout_width="70dip"
  65. android:layout_height="wrap_content"
  66. android:background="#800000"
  67. android:gravity="center"
  68. android:text="No"
  69. android:padding="5dp"
  70. android:layout_marginLeft="30dp"
  71. android:textColor="#006789"
  72. android:textSize="18sp" />
  73. </LinearLayout>
  74. </LinearLayout>
  75. </LinearLayout>


2:  Dialog clas
// Attach the layout to Dialog

  1. public Dialog showDialog(final Context context ,String tittle , String content) {
  2. final Dialog dialog = new Dialog(context,
  3. android.R.style.Theme_DeviceDefault_Dialog);
  4. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  5. dialog.getWindow().setBackgroundDrawable(
  6. new ColorDrawable(android.graphics.Color.TRANSPARENT));
  7. dialog.setContentView(R.layout.custome_dialog);
  8. dialog.setCanceledOnTouchOutside(true);
  9. TextView tvTitle = (TextView)dialog.findViewById(R.id.textView2);
  10. TextView tvContent = (TextView)dialog.findViewById(R.id.content_add);
  11. tvTitle.setText(tittle);
  12. tvContent.setText(content);
  13. final TextView btn_yes = (TextView) dialog.findViewById(R.id.btn_yes);
  14. final TextView btn_no = (TextView) dialog.findViewById(R.id.btn_no);
  15. btn_yes.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18.     Toast.makeText(context,"Yes , you're awesome !", Toast.LENGTH_LONG).show();
  19. dialog.dismiss();
  20. }
  21. }
  22. );
  23. btn_no.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26.     Toast.makeText(context,"no !", Toast.LENGTH_LONG).show();
  27. dialog.dismiss();
  28. }
  29. });
  30. dialog.show();
  31. return dialog;
  32. }

3. Display the Dialog by click to button.
//
  1. rootView.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {        
  2.   @Override
  3.    public void onClick(View arg0) {
  4.    Dialog mdialog = showDialog(getActivity(), "Look at me !", "I'm awesome!");
  5.         }
  6.  });

Finish , that's it , so simple and you got a beautiful dialog ,it just being limited by your creative.
Anyway , thanks for reading , enjoy.
Here is full source code  for some lazy guys: (kidding)

down load Source Code

No comments:

Powered by Blogger.