codearts
codearts

Reputation: 2956

Can't display alert dialog on button click Android

So I created a multiple choice alert dialog. The problem is that I can't display it. I want to display it when the user clicks on a button in another Activity. Here is the code.

public class FlourishPickerDialog extends DialogFragment {
    public ArrayList<String> flourishSet = new ArrayList<>();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final ArrayList mSelectedItems = new ArrayList();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.flourish_picker_text)
                .setMultiChoiceItems(R.array.dealersGripFlourishArr, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            mSelectedItems.add(which);
                        } else if (mSelectedItems.contains(which)) {
                            mSelectedItems.remove(Integer.valueOf(which));
                        }
                    }
                })

                .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
                    @Override
                    public  void onClick(DialogInterface dialog, int id) {
                        flourishSet = mSelectedItems;
                    }
                })

                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        builder.create();
        return builder.show();
    }

}

So how can I do that? I tried creating a method in the activity and calling it with a button onClick but I couldn't. Thanks in advance. EDIT :

I also tried typing builder.show(); before builder.create(); and reverse. Here is how I try to display it. This is the method in the activity.java of the xml file with the button onClick.

public void showFlourishPicker() {
    DialogFragment alert = new FlourishPickerDialog();
    alert.show(getFragmentManager(), "flourishes");
}

EDIT 2:

Here is the xml onClick

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:onClick="showFlourishPicker"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="86dp" />

Upvotes: 1

Views: 981

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

I've tested your problem and I got the solution, follow those steps.

On your MainActivity import this :

import android.support.v4.app.DialogFragment;

Make your call as follows :

public void showFlourishPicker (View v){
  DialogFragment alert = new FlourishPickerDialog();
  alert.show(getSupportFragmentManager(), "flourishes");
}

And in your FlourishPicker add this imports :

import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

Upvotes: 1

noev
noev

Reputation: 980

Remove the show code from your dialog and place it in the calling activity. And in the onCreateDialog(Bundle savedInstanceState) method return builder.create();

onCreateDialog has to return Dialog but show() returns void.

Upvotes: 1

Related Questions