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:

custom_login_layout.xml

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

CustomLoginDialog.java

the hack.patchversion.patchversiontutorials package; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; 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 (R.id.email); final EditText passwordText = view.findViewById (R.id.password); 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 MainActivity.java,

the hack.patchversion.patchversiontutorials package;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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


}

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

}
}

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


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

VIDEO DEMO

Leave a Comment

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