0

I have been struggling with this for a while now. I have added a neutral button to my Alert Dialog which is suppose to basically insert todays date into the input area of the dialog without closing the dialog. Giving the user the chance to customize the input.

The neutral button appears in the dialog but when it is clicked it cancels out of the dialog like the cancel button.

enter image description here

I have gone through a number of different posts on this site and other websites. But I was not able to find anything relevant to what I am trying to do. I know its not rocket science but I don't know how to resolve this. Any help would be appreciated. Here is the code for my Alert Dialog Creation

//Show Alert Dialog With EditText Options to Enter/Edit A Series
    //When shouldUpdate = true, It Will Automatically Display Old Series Name And Change The Button Text To UPDATE
    private void showSeriesDialog(final boolean shouldUpdate, final Series series, final int position) {
        LayoutInflater layoutInflaterAndroid = LayoutInflater.from( getApplicationContext() );
        final View view = View.inflate( this, R.layout.dialog_series, null );

        AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder( SeriesActivity.this );
        alertDialogBuilderUserInput.setView( view );

        final EditText inputSeries = view.findViewById( R.id.etSeriesNameInput );
        leagueId.setText( savedLeagueId );
        bowlerId.setText( savedBowlerId );
        TextView dialogTitle = view.findViewById( R.id.dialog_title );
        dialogTitle.setText( !shouldUpdate ? getString( R.string.lbl_new_series_title ) : getString( R.string.lbl_edit_series_title ) );

        if (shouldUpdate && series != null) {
            inputSeries.setText( series.getName() );
            leagueId.setText( series.getLeagueId() );
            bowlerId.setText( series.getBowlerId() );

        }
        alertDialogBuilderUserInput.setCancelable( false ).setPositiveButton( shouldUpdate ? "update" : "save", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogBox, int id) {

            }
        } ).setNegativeButton( "cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogBox, int id) {
                dialogBox.cancel();
            }
        } ) .setNeutralButton("use date",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogBox, int id) {
                        String dateNow = getDateNow();
                        inputSeries.setText(dateNow);

                    }
                });

        final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
        alertDialog.show();

        alertDialog.getButton( AlertDialog.BUTTON_POSITIVE ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Show Toast Message When No Text Is Entered
                if (TextUtils.isEmpty( inputSeries.getText().toString() )) {
                    Toast.makeText( SeriesActivity.this, "Enter Series!", Toast.LENGTH_SHORT ).show();
                    return;
                } else {
                    alertDialog.dismiss();
                }


                //Check If User Is Updating Series
                if (shouldUpdate && series != null) {

                    //Updating Series By Its Id
                    updateSeries( inputSeries.getText().toString(), position );

                } else {
                    //Creating New Series
                    createSeries( leagueId.getText().toString(), bowlerId.getText().toString(), inputSeries.getText().toString() );
                }
            }
        } );
    }



    private String getDateNow() {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("d-MM-yyyy", Locale.ENGLISH);
            return sdf.format(cal.getTime());
        }

This issue is not about the alert dialog closing on the button click but on how to get the date value from getDateNow to appear in the input field "Enter Series Name", instead of manually having to type the date each time you want to add a series.

I was able to get the "USE DATE" button to populate the EDITTEXT field by using the following code:

alertDialog.getButton( AlertDialog.BUTTON_NEUTRAL ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Show Toast Message When No Text Is Entered
                if (TextUtils.isEmpty( inputSeries.getText().toString() )) {
                    String dateNow = getDateNow();
                    inputSeries.setText(dateNow);
                    return;
                } else {
                    alertDialog.dismiss();
                }


                //Check If User Is Updating Series
                if (shouldUpdate && series != null) {

                    //Updating Series By Its Id
                    updateSeries( inputSeries.getText().toString(), position );

                } else {
                    //Creating New Series
                    createSeries( leagueId.getText().toString(), bowlerId.getText().toString(), inputSeries.getText().toString() );
                }
            }
        } );


        alertDialog.getButton( AlertDialog.BUTTON_POSITIVE ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Show Toast Message When No Text Is Entered
                if (TextUtils.isEmpty( inputSeries.getText().toString() )) {
                    Toast.makeText( SeriesActivity.this, "Enter Series!", Toast.LENGTH_SHORT ).show();
                    return;
                } else {
                    alertDialog.dismiss();
                }


                //Check If User Is Updating Series
                if (shouldUpdate && series != null) {

                    //Updating Series By Its Id
                    updateSeries( inputSeries.getText().toString(), position );

                } else {
                    //Creating New Series
                    createSeries( leagueId.getText().toString(), bowlerId.getText().toString(), inputSeries.getText().toString() );
                }
            }
        } );
    }

enter image description here

The problem is that the following code must be empty in order for this to work. Is there another way to write this so that it will wok whether or not the EDITTEXT field is empty or not?

if (TextUtils.isEmpty( inputSeries.getText().toString() )) {
                        String dateNow = getDateNow();
                        inputSeries.setText(dateNow);
                        return;
                    } else {
                        alertDialog.dismiss();
                    }
Robert Vogl
  • 250
  • 5
  • 19
  • Possible duplicate of [How to prevent a dialog from closing when a button is clicked](https://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) – Sani Huttunen Jul 12 '18 at 19:27
  • I think AlertDialogs always work like that: if you click a Button in their ButtonBar then they get dismissed automatically. So I think you have to implement onCreateView to show your own layout including a ButtonBar. This will give you a lot of freedom to handle things. But keep in mind that components should behave like users expect them to - so if you're doing your own layout anyway maybe do not place the "Date" button in the ButtonBar but somewhere else – Bö macht Blau Jul 12 '18 at 19:27
  • I have edited my question to reflect that this is not about the Alert Dialog closing on button click. – Robert Vogl Jul 12 '18 at 20:00
  • @RobertVogl: You're wrong about that. Your problem is exactly that the dialog is "closing" on the click of the NeutralButton. You can have exactly one of each type in the Alert Dialog. Positive, Neutral and Negative. Poisitve is like "yes", Negative is like "no" and Neutral is like "i dont know, ask me later". All three buttons will close the dialog so check out the answers in the question I marked as duplicate for a solution. – Sani Huttunen Jul 12 '18 at 20:19
  • @SaniSinghHuttunen You are wrong see my revision above this issue was not about the dialog closing but rather getting the value of a function populate the EditText field. – Robert Vogl Jul 12 '18 at 20:28
  • @RobertVogl: Now that you've changed the whole question it's no longer about your first question. – Sani Huttunen Jul 12 '18 at 20:43
  • My question was never about the dialog closing it was about passing a value to the EditText on a button click in an AlertDialog. Just like the title indicates. You just became fixated on the fact that I mention that the Alert Dialog was closing. – Robert Vogl Jul 12 '18 at 20:50
  • From revision 1: `I have added a neutral button to my Alert Dialog which is suppose to basically insert todays date into the input area of the dialog without closing the dialog. Giving the user the chance to customize the input. The neutral button appears in the dialog but when it is clicked it cancels out of the dialog like the cancel button.` Good night. – Sani Huttunen Jul 12 '18 at 22:36

2 Answers2

0

According to docs, the neutral button is as -

Neutral - You should use this when the user may not want to proceed with the action, but doesn't necessarily want to cancel. It appears between the positive and negative buttons. For example, the action might be "Remind me later."

You can add only one of each button type to an AlertDialog. That is, you cannot have more than one "positive" button.

So, you can't directly use it for your work as it will dismiss the dialog when clicked. You need to customize that or you can create your own dialog.

Raj
  • 2,997
  • 2
  • 12
  • 30
-1

In case someone else is trying to accomplish something similar to this in the future.

The Neutral button can be used for other purposes other than what was described above. It can act as a positive button in order to capture some input from a function, in my case it was to get todays date and by adding a return rather than dismissing the dialog I was able to keep it open for the user to modify the date and then click "save".

Here is the code that I used to accomplish this:

alertDialog.getButton( AlertDialog.BUTTON_NEUTRAL ).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Show Toast Message When No Text Is Entered
                if (inputSeries.getText().toString() !=null && !inputSeries.getText().toString().isEmpty() ) {
                    String dateNow = getDateNow();
                    inputSeries.setText(dateNow);
                    return;
                } else {
                    alertDialog.dismiss();
                }


                //Check If User Is Updating Series
                if (shouldUpdate && series != null) {

                    //Updating Series By Its Id
                    updateSeries( inputSeries.getText().toString(), position );

                } else {
                    //Creating New Series
                    createSeries( leagueId.getText().toString(), bowlerId.getText().toString(), inputSeries.getText().toString() );
                }
            }
        } );

See the last image that I posted above in my question to see the results.

Robert Vogl
  • 250
  • 5
  • 19