I've created a class extending DialogFragment
which returns an AlertDialog
in it's onCreateDialog method, like here.
The problem is, that I want to increase the standard (positive) button's height, but I cannot get a hold of it to change it's height.
When I do the following in the onCreateDialog method of the DialogFragment
mAlertDialog = new AlertDialog.Builder(getActivity())
.setView(messageView)
.setCustomTitle(titleView)
.setPositiveButton(R.string.dialog_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((ContextSwapUserInterface) getActivity()).instructionsAccepted();
}
}
)
.create();
mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setHeight(60);
I get an exception, which says "...unable to instantiate ComponentInfo..."
I guess this is because the Button is not instantiated properly at this point of time.
So I tried getting the button in my main activity, after I created the DialogFragment
and called it's .show method:
// Create and show a new InstructionsDialogFragment
DialogFragment instructionsDialogFragment = InstructionsDialogFragment.newInstance(mInstructions);
instructionsDialogFragment.show(getFragmentManager(), "Instructions Dialog");
((Button) instructionsDialogFragment.getDialog().findViewById(android.R.id.button1)).setHeight(60);
I also tried the following, instead of the last line above:
((AlertDialog) instructionsDialogFragment.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setHeight(60);
Both versions result in a NullPointerException. Is there any easy way to customize the AlertDialog
's Button while using a DialogFragment
?