Simple custom dialog android
In this tutorial, we show you how to create a custom dialog in Android. Following steps :
- Create a custom dialog layout (XML file).
- Attach the layout to
Dialog
. - Display the
Dialog
. - Done.
1 : Customedialog.xml
//Create a custom dialog layout (XML file)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:paddingLeft="50dp"
- android:paddingRight="50dp">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:gravity="center"
- android:background="@drawable/header"
- android:orientation="vertical">
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Custome dialog"
- android:textColor="#ffffff"
- android:textSize="18sp"
- android:textStyle="bold" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#fffccc"
- android:orientation="vertical"
- android:padding="10dp">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/content_add"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="Your message"
- android:paddingTop="10dp"
- android:textColor="#000000"
- android:paddingBottom="10dp"
- android:textSize="18sp" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/btn_yes"
- android:layout_width="70dip"
- android:layout_height="wrap_content"
- android:background="#006789"
- android:gravity="center"
- android:text="Yes"
- android:padding="5dp"
- android:textColor="#ffffff"
- android:textSize="18sp" />
- <TextView
- android:id="@+id/btn_no"
- android:layout_width="70dip"
- android:layout_height="wrap_content"
- android:background="#800000"
- android:gravity="center"
- android:text="No"
- android:padding="5dp"
- android:layout_marginLeft="30dp"
- android:textColor="#006789"
- android:textSize="18sp" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
2: Dialog clas
// Attach the layout to Dialog
- public Dialog showDialog(final Context context ,String tittle , String content) {
- final Dialog dialog = new Dialog(context,
- android.R.style.Theme_DeviceDefault_Dialog);
- dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
- dialog.getWindow().setBackgroundDrawable(
- new ColorDrawable(android.graphics.Color.TRANSPARENT));
- dialog.setContentView(R.layout.custome_dialog);
- dialog.setCanceledOnTouchOutside(true);
- TextView tvTitle = (TextView)dialog.findViewById(R.id.textView2);
- TextView tvContent = (TextView)dialog.findViewById(R.id.content_add);
- tvTitle.setText(tittle);
- tvContent.setText(content);
- final TextView btn_yes = (TextView) dialog.findViewById(R.id.btn_yes);
- final TextView btn_no = (TextView) dialog.findViewById(R.id.btn_no);
- btn_yes.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(context,"Yes , you're awesome !", Toast.LENGTH_LONG).show();
- dialog.dismiss();
- }
- }
- );
- btn_no.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(context,"no !", Toast.LENGTH_LONG).show();
- dialog.dismiss();
- }
- });
- dialog.show();
- return dialog;
- }
3. Display the Dialog by click to button.
//
- rootView.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- Dialog mdialog = showDialog(getActivity(), "Look at me !", "I'm awesome!");
- }
- });
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)
No comments: