Android Studio AlertDialog Customize 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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