Schnodahipfe
Schnodahipfe

Reputation: 1053

Android 3.2 - Customize AlertDialog Button within DialogFragment

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?

Upvotes: 2

Views: 3259

Answers (2)

Guerneen4
Guerneen4

Reputation: 728

Another way is to use an onShowListener to modify the buttons I had the same problem and i counldn't get my hand on Buttons until the dialog is shown .show(). i think the UI elements of the AlertDialog are not instantiated after builder.create but only after .show().

mAlertDialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setHeight(60);
        }
    });

Upvotes: 2

Davos555
Davos555

Reputation: 2004

Ok - Try gettin the button within the onActivityCreated method of the fragment. I did this for a listfragment, but it should do the same - check you have the right IDs etc.

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Button b = (Button)this.getView().findViewById(R.id.buttonID);

        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Button Clicked",  Toast.LENGTH_SHORT).show();
            }
    });

Upvotes: 4

Related Questions