How to create android dialog with a custom layout Download

Android Alert Dialog This is a small window that prompts the user to enter additional information. The dialog does not fill the screen and is usually used for modal events that require the user to enter some information or perform actions. Today we are going to learn how to create Android dialogs with a custom layout

How to display the Android dialog in a custom layout

To create an Android dialog in a custom layout is too simple, you just need to create a class that extends DialogFragmentthen pump up your own layout and that’s it.

Go ahead and create a new android studio project from FILE> New project, Fill in all the required data, in the activity section select Primary activity and continue.

Create a custom layout for the dialogue. Create your own layout; right click on location and select create new layout and the following code:


Create a Java dialog class that extends DialogFragment, name it, Add the following code to your class dialog box.

the hack.patchversion.patchversiontutorials package; import; import android.content.DialogInterface; import android.os.Bundle; import; import; import; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class CustomLoginDialog extends DialogFragment { @NonNull @Override public Dialog onCreateDialog (BundlevedInstanceState) { View view = getActivity (). GetLayoutInflater (). Inflate (R.layout.custom_login_layout, null); AlertDialog.Builder builder = new AlertDialog.Builder (getActivity ()); builder.setTitle (“Login”); builder.setView (view); final EditText emailText = view.findViewById (; final EditText passwordText = view.findViewById (; builder.setNegativeButton (Cancel, zero); builder.setPositiveButton (“Login”, new DialogInterface.OnClickListener () { @Override public void onClick (DialogInterface dialogInterface, int i) { Toast.makeText (getActivity (), “Email is” + emailText.getText (). ToString () + “and password” + passwordText.getText (). ToString (), Toast.LENGTH_SHORT) .show (); } }); return builder.create (); } @Override public void onCancel (DialogInterface dialog) { super.onCancel (dialog); Toast.makeText (getActivity (), “Dialogue Canceled”, Toast.LENGTH_SHORT) .show (); } }

Now it’s time to show the dialogue in the main activity. We will create a button in the MainActivity layout that will display a dialog when clicked. Go ahead and add the following code to content_main.xml

As you noted above, we included the XML attribute OnClick = “Login» This method will be called each time the button is clicked, so add the following code to your,

the hack.patchversion.patchversiontutorials package;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

protected void onCreate (BundlevedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById (;
setSupportActionBar (toolbar);


public void login (view presentation) {
CustomLoginDialog customLoginDialog = new CustomLoginDialog (); (getSupportFragmentManager (), "login_dialog");


Showing the dialog is too easy, you just call the following code:

CustomLoginDialog customLoginDialog = new CustomLoginDialog (); (getSupportFragmentManager (), "login_dialog");
// fragment manager and dialog tab, the tag can be any or any name
// (fragment Manager, tag [this is arbitrary string])


Download: Link-1 |Link-2 |Link-3


Leave a Comment

Your email address will not be published. Required fields are marked *