How to Create a Custom Dialog Box in Android

How to create a Custom Dialog box in android?

Here I have created a simple Dialog, like:

Dialog Box

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#3E80B4"
android:orientation="vertical" >

<TextView
android:id="@+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="@android:color/white"
android:textSize="15dp"
android:textStyle="bold"/>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >

<Button
android:id="@+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#5DBCD2"
android:textStyle="bold" />

<Button
android:id="@+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#5DBCD2"
android:textStyle="bold" />
</LinearLayout>

</LinearLayout>

You have to extends Dialog and implements OnClickListener

public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {

public Activity c;
public Dialog d;
public Button yes, no;

public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
}

How to Call Dialog ?

R.id.TXT_Exit:
CustomDialogClass cdd=new CustomDialogClass(Values.this);
cdd.show();

Updates

After a long time one of my friends asked me to make a curved shape dialog with a transparent background. So, Here I have implemented it.

Sample Image

To Make a curved shape you need to create a separate curve_shap.XML as below,

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#000000" />

<stroke
android:width="2dp"
android:color="#ffffff" />

<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />

</shape>

Now, add this curve_shap.XML in your main view Layout. In my case I have used LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/curve_shap"
android:orientation="vertical" >
...
</LinearLayout>

How to call this ?

CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();

I hope that works for you.

How to make custom dialog with rounded corners in android

Create an XML file in drawable, say dialog_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/white"/>
<corners
android:radius="30dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>

set it as the background in your layout XML:

android:background="@drawable/dialog_bg"

Set the background of the dialog's root view to transparent, because Android puts your dialog layout within a root view that hides the corners in your custom layout.

Java:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Kotlin:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

Kotlin - custom dialog in Android

You can use below code for a custom Dialog. It's my working code.

 private fun showDialog(title: String) {
val dialog = Dialog(activity)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.custom_layout)
val body = dialog.findViewById(R.id.body) as TextView
body.text = title
val yesBtn = dialog.findViewById(R.id.yesBtn) as Button
val noBtn = dialog.findViewById(R.id.noBtn) as TextView
yesBtn.setOnClickListener {
dialog.dismiss()
}
noBtn.setOnClickListener { dialog.dismiss() }
dialog.show()

}

how to create a custom dialog and receive results in android?

I think the best way to create custom dialogs now is the Dialog Fragment, because the simple dialog it's limited. For example it's the way to create a dialogs with material design. And you have a differents ways to take info from dialog fragment, the first and the second for example.


This is basic code to create a dialog fragment:

//Method to call and start dialog fragment class
public void ShowPhotoFilesDialog(Activity context,File photo){
//Declaration of classes
Custom_DialogFragment custom_dialogFragment = new Custom_DialogFragment ();

FragmentManager fragmentManager = context.getFragmentManager();

// The device is using a large layout, so show the fragment as a dialog
custom_dialogFragment.show(fragmentManager, "dialog");
}

And this is the basic dialog fragment class:

public class Custom_DialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
try {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//To hide action bar from layout
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Declaration of controls
View v = getActivity().getLayoutInflater().inflate(R.layout.my_custom_layout);
builder.setView(v);

//My code

return builder.create();
}
catch (Exception ex){
Log.e("-- Custom_DialogFragment.onCreateDialog --","",ex);
return null;
}
}
}

Tell me if I helped you, good programming!

Fully Custom Dialog Box in Android

If you want to make customize dialog with the good look then you need to set Style and Theme of your dialog.

A style is a collection of properties that specify the look and format for a View or window.

I am just posting you some example of style which you can set it to your dialog by below line:

Dialog dialog = new Dialog(YourActivity.this, R.style.MyTheme);

style.xml

You need to create new style in style.xml:

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:bottomBright">@color/white</item>
<item name="android:bottomDark">@color/white</item>
<item name="android:bottomMedium">@color/white</item>
<item name="android:centerBright">@color/white</item>
<item name="android:centerDark">@color/white</item>
<item name="android:centerMedium">@color/white</item>
<item name="android:fullBright">@color/orange</item>
<item name="android:fullDark">@color/orange</item>
<item name="android:topBright">@color/blue</item>
<item name="android:topDark">@color/blue</item>
</style>

<style name="MyTheme">
<item name="android:alertDialogStyle">@style/CustomDialogTheme</item>
</style>

This is just an example. If you want to get more details please follow the below link:
http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

Hope it will help you.

How to create a Custom Dialog box in android with library or class?

Please use the DialogFragment Class in android to build customized dialogs the right way.

A fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

Implementations should override this class and implement onCreateView(LayoutInflater, ViewGroup, Bundle) to supply the content of the dialog. Alternatively, they can override onCreateDialog(Bundle) to create an entirely custom dialog, such as an AlertDialog, with its own content. Source -

How to implement my own custom dialog box

MainActivity.class

     btn1.setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DiscountDialog();
});

private void DiscountDialog() {
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.setContentView(R.layout.dialog_discount);
final EditText etDiscount = (EditText) dialog
.findViewById(R.id.dialog_discount_editDiscount);

dialog.findViewById(R.id.dialog_discount_CANCEL).setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
dialog.findViewById(R.id.dialog_discount_OK).setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
dialog.show();
}

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/subtitle" >

<LinearLayout
android:id="@+id/dialog_pos_editnumber_EDITORCONTAINER"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/layer_list_pos_editnumber_dialogcontainer"
android:orientation="vertical"
android:paddingTop="0dp" >



<com.mobilepaymentspecialists.controls.FEditText
android:id="@+id/dialog_discount_editDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/layerlist_custom_edittext"
android:hint="@string/fragment_pos_DISCOUNT_HINT"
android:inputType="numberDecimal"
android:digits=".0123456789"
android:singleLine="true"
android:minHeight="40dp"
android:padding="10dp"
android:textColorHint="@color/subtitle" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >

<com.mobilepaymentspecialists.controls.FButton
android:id="@+id/dialog_discount_OK"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/selector_green_button"
android:text="@android:string/ok"
android:textColor="@color/white"
android:textStyle="bold" />

<com.mobilepaymentspecialists.controls.FButton
android:id="@+id/dialog_discount_CANCEL"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/selector_green_button"
android:text="@android:string/cancel"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

</RelativeLayout>

Custom Dialog Box not displaying properly

 final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.enter_quantity_dialog);
Window window = dialog.getWindow();
window.setLayout(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));


Related Topics



Leave a reply



Submit