Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created January 29, 2021 06:07
Embed
What would you like to do?
Android Studio AlertDialog Customize 5
public class MyDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
TextView titleView = new TextView(getActivity());
titleView.setText("タイトル");
titleView.setTextSize(24);
titleView.setTextColor(Color.WHITE);
titleView.setBackgroundColor(getResources().getColor(R.color.alertBlue));
titleView.setPadding(20, 20, 20, 20);
titleView.setGravity(Gravity.CENTER);
TextView msgView = new TextView(getActivity());
msgView.setText("ここにメッセージを入力します。ここにメッセージを入力します。ここにメッセージを入力します。");
msgView.setTextSize(16);
msgView.setTextColor(Color.BLACK);
msgView.setPadding(20, 20, 20, 40);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCustomTitle(titleView)
.setView(msgView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// このボタンを押した時の処理を書きます。
}
});
return builder.create();
}
@Override
public void onStart() {
super.onStart();
AlertDialog alertDialog = (AlertDialog) getDialog();
if (alertDialog != null) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.WHITE);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundColor(getResources().getColor(R.color.alertBlue));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment