📜 ⬆️ ⬇️

Dialog boxes in Android. Part 1

Good afternoon / evening / morning, dear habravchane and you,%% username!
I develop Android applications, adore this operating system and want to share my experience using dialog boxes in my projects. First of all, this article is very useful for beginners in the development field for Android.
Also for beginners in this industry, I recommend first reading this post of a respected Hoorsh .
And now we will start consideration of this issue (which has several pitfalls) under the cut.

Dialog

Dialog is a class that belongs to the Android SDK and helps us mortal programmers to work with dialog boxes in Android. The Dialog class has 4 subclasses:

On the first of this list, AlertDialog we will stop in more detail.

Alertdialog

To create the AlertDialog dialog box, we will need an instance of the Builder class:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Now that we have a builder, we can “build” our own dialog box. In the parameters of the dialog box, you can write many different parameters, but the main ones are these:

An example of creating an AlertDialog:
 AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle(R.string.dialog_about_title); builder.setMessage(R.string.dialog_about_message); builder.setCancelable(true); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { //   @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //    } }); AlertDialog dialog = builder.create(); 

In this example, I used setCancelable (true) - this allows the user to close the dialog box using the hardware Back button. I also created a listener for the affirmative button and used the create () method. The create () method returns the finished dialog box with your parameters as an instance of the AlertDialog class.
So we created a dialog box! The next task is to show it to the user. Here are some options:

The second method suits us better, but writing a bunch of different Buider and AlertDialog in the main activity spoils the readability of the code. Therefore, it is more expedient to bring the creation and processing of dialog boxes to a new class, for example:
 public class DialogScreen { public static final int IDD_ABOUT = 1; //    public static final int IDD_SETTINGS = 2; public static final int IDD_RATE = 3; public static AlertDialog getDialog(Activity activity, int ID) { AlertDialog.Builder builder = new AlertDialog.Builder(activity); switch(ID) { case IDD_ABOUT: //   About builder.setTitle(R.string.dialog_about_title); builder.setMessage(R.string.dialog_about_message); builder.setCancelable(true); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { //   @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //    } }); return builder.create(); case IDD_RATE: //   Rate the app builder.setTitle(R.string.dialog_rate_title); builder.setMessage(R.string.dialog_rate_message); builder.setCancelable(true); builder.setPositiveButton(R.string.dialog_rate_ok, new DialogInterface.OnClickListener() { //     @Override public void onClick(DialogInterface dialog, int which) { //  dialog.dismiss(); } }); builder.setNeutralButton(R.string.dialog_rate_cancel, new DialogInterface.OnClickListener() { //    @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //    } }); builder.setNegativeButton(R.string.dialog_rate_buy, new DialogInterface.OnClickListener() { //    AdFree  @Override public void onClick(DialogInterface dialog, int which) { //  dialog.dismiss(); } }); return builder.create(); case IDD_SETTINGS: //   View view = activity.getLayoutInflater().inflate(R.layout.settings, null); //  layout   ID builder.setView(view); builder.setTitle(R.string.dialog_settings_title); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { //   public void onClick(DialogInterface dialog, int whichButton) { MainActivity.doSaveSettings(); //     MainActivity dialog.dismiss(); } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { //   public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setCancelable(true); return builder.create(); default: return null; } } } 

The only note about this code will be about using the setView (View view) method in the IDD_SETTINGS dialog. AlertDialog, like all other dialog boxes, has one nice feature - you can set your own Layout, which allows you to completely modify the dialog boxes and their contents. There is one pitfall here: you will have to perform the processing of the elements of this Layout exactly in the Activity where you call this dialog box. For example, I show the IDD_SETTINGS dialog in MainActivity with a Layout named settings:
 <?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" > <SeekBar android:id="@+id/seekVol" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/seekSense" android:layout_marginTop="42dp" /> <TextView android:id="@+id/textVol" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/seekSense" android:layout_marginTop="20dp" android:text="@string/dialog_settings_vol" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textSense" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/dialog_settings_sense" android:textAppearance="?android:attr/textAppearanceMedium" /> <SeekBar android:id="@+id/seekSense" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textSense" /> </RelativeLayout> 

Accordingly, I load this window into MainActivity:
  AlertDialog dialog = DialogScreen.getDialog(this, DialogScreen.IDD_SETTINGS); dialog.show(); initSettings(dialog); } 

The initSettings method of the MainActivity class in this case will look like this:
  //  SeekBar       SeekBar sb_sense = (SeekBar)dialog.findViewById(R.id.seekSense); SeekBar sb_vol = (SeekBar)dialog.findViewById(R.id.seekVol); //   SeekBar    sb_sense.setProgress(sense); sb_vol.setProgress(volume); 

Well, then process your objects as you like.

Small total

1) AlertDialog is suitable for most purposes of dialog boxes.
2) AlertDialog can take the form of a layout with any objects
3) It is much better to use a separate class for dialog boxes and use AlertDialog.show ()
4) Processing custom layout objects is done in the activity that caused the dialog
')
The next part of this article will talk about DialogFragment, which has been included in the Android SDK since 3.0 Honeycomb. I hope this article will help beginners and not very much in their quests on the conquest of Google Play.

PS

This is my first article for Habr, so I accept any constructive criticism. Thank you in advance.

Source: https://habr.com/ru/post/166469/


All Articles