AlertDialogはちょっとしたwarningなどの情報を知らせるのに適しています。
そのDialogはFragmentなので説明用の画像やテキストの設定もカスタマイズして追加できます。
 
![[Android] AlertDialogに画像を入れてカスタマイズする 1x1.trans - [Android] AlertDialogに画像を入れてカスタマイズする](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Android Studio
2021.2.1
2021.2.1
AlertDialog
で基本的なAlertDialogを作成しました。更にボタンの代わりに画像や追加テキストを入れたいという場合を考えてみます。
AlertDialogを生成するFragmentの中で、
| 1 | alert.setView(int layoutRsId); | 
のようにレイアウトをViewとして指定して画像を追加するとできます。
MainActivity.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | //package your.app.name; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.app.AlertDialog; import android.app.Dialog; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.FragmentManager; import androidx.annotation.NonNull; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import java.util.Objects; public class MainActivity extends AppCompatActivity {     private ImageView imageView;     private TextView textView;     private DialogFragment dialogFragment;     private FragmentManager flagmentManager;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         imageView = findViewById(R.id.image_view);         textView = findViewById(R.id.text_view);         Button button = findViewById(R.id.button);         button.setOnClickListener(v -> {             flagmentManager = getSupportFragmentManager();             dialogFragment = new AlertDialogFragment();             dialogFragment.show(flagmentManager, "test alert dialog");         });     }     public void setSelection(String str){         textView.setText(str);         if(str.equals("bag1 clicked")){             imageView.setImageResource(R.drawable.bag01);         }         else if(str.equals("bag2 clicked")){             imageView.setImageResource(R.drawable.bag02);         }     }     public static class AlertDialogFragment extends DialogFragment {         AlertDialog dialog ;         AlertDialog.Builder alert;         View alertView;         @SuppressLint("InflateParams")         @Override         @NonNull         public Dialog onCreateDialog(Bundle savedInstanceState) {             alert = new AlertDialog.Builder(getActivity());             alert.setTitle("Custom AlertDialog");             // カスタムレイアウトの生成             if(getActivity() != null) {                 alertView = getActivity().getLayoutInflater()                         .inflate(R.layout.alert_layout, null);             }             // alert_layout.xmlにあるボタンIDを使う             ImageView bag1 = alertView.findViewById(R.id.bag_1);             bag1.setOnClickListener(v -> {                 Log.d("debug","bag1 clicked");                 setMassage("bag1 clicked");                 // Dialogを消す                 Objects.requireNonNull(getDialog()).dismiss();             });             ImageView bag2 = alertView.findViewById(R.id.bag_2);             bag2.setOnClickListener(v -> {                 Log.d("debug","bag2 clicked");                 setMassage("bag2 clicked");                 // Dialogを消す                 Objects.requireNonNull(getDialog()).dismiss();             });             // ViewをAlertDialog.Builderに追加             alert.setView(alertView);             // Dialogを生成             dialog = alert.create();             dialog.show();             return dialog;         }         private void setMassage(String message) {             MainActivity mainActivity = (MainActivity) getActivity();             if(mainActivity != null) {                 mainActivity.setSelection(message);             }         }     } } | 
このレイアウトはAlertDialogに追加する画面で、画像を2つdrawableに入れておきます。
alert_lyout.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:gravity="center"     android:background="#8af"     android:layout_width="match_parent"     android:layout_height="match_parent">     <TextView         android:text="@string/question"         android:textSize="20sp"         android:textColor="#00f"         android:layout_margin="10dp"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />     <LinearLayout         android:orientation="horizontal"         android:gravity="center"         android:layout_width="match_parent"         android:layout_height="wrap_content">         <ImageView             android:id="@+id/bag_1"             android:src="@drawable/bag01"             android:layout_margin="10dp"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:contentDescription="@string/description2"/>         <ImageView             android:id="@+id/bag_2"             android:src="@drawable/bag02"             android:layout_margin="10dp"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:contentDescription="@string/description3"/>     </LinearLayout> </LinearLayout> | 
 
メインのレイアウトです。
activity_main.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#8af"     tools:context=".MainActivity">     <ImageView         android:id="@+id/image_view"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:contentDescription="@string/description1"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.5"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.3" />     <TextView         android:id="@+id/text_view"         android:text="@string/text"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.5"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.5" />     <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margin="20dp"         android:text="@string/button"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.5"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.6" /> </androidx.constraintlayout.widget.ConstraintLayout> | 
 
strings.xml
| 1 2 3 4 5 6 7 8 9 | <resources>     <string name="app_name">YourAppName</string>     <string name="text">Test AlertDialog2</string>     <string name="description1">selected bag</string>     <string name="button">Alert Dialog</string>     <string name="question">どのバッグにしますか?</string>     <string name="description2">bag1</string>     <string name="description3">bag2</string> </resources> | 
もちろんレイアウトはコードで記述することもできます。レイアウトファイルを使わずに画像表示
 
関連ページ:
簡単なAlertDialog 
AlertDialog:FragmentDialogを使って設定
AlertDialogに画像を設定
 
References:
ダイアログ | Android Developers
AlertDialog | Android Developers
