0

I have a AlertDialog which has a EditText. This EditText takes user input and on "Post" button click a API call is initiated.

The problem is the AlertDialog should dismiss as soon as the "Post" button is clicked. Currently I have to click on outside area to dismiss it.

If I remove the API call, the AlertDialog dismisses properly on button click.

I am not sure what is wrong.

Here is my code:-

    case R.id.btnAddComms:
        scrollNews.fullScroll(v.FOCUS_DOWN);
        btnAddComms.setPressed(true);

        AlertDialog.Builder builder = new AlertDialog.Builder(NDetails.this);
        builder.setTitle("Post Comment");
        builder.setIcon(R.drawable.post_comment_button);


        final EditText input1 = new EditText(NDetails.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        input1.setLayoutParams(lp);
        builder.setView(input1);


        builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
        {
            //private ITextInputDialogCalback callback;

            public void onClick(DialogInterface dialog, int id) 
            {



                 postedComment = input1.getText().toString();
                 if(postedComment.length()>0)
                 {
                     dialog.cancel();
                     PostComments(postedComment);

                 }
                 else
                 {
                     Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
                     input1.findFocus();
                 }





            }

            private void PostComments(String postedComment)
            {
                // TODO Auto-generated method stub

                  String postCommentUrl  = url;
                    try 
                    {
                        String commentResponse = new PostComment().execute(postCommentUrl).get();
                        String getRequestForComments = myurl;
                        String items = new FetchItems().execute(getRequestForComments).get();
                        ArrayList<HashMap<String, String>> updatedCommentList = new GetList().execute(items).get();



                        itemsAdapter = (ListAdapter) new CommentsAdapter(NDetails.this, updatedCommentList);
                        commentsList.invalidate();
                        commentsList.refreshDrawableState();
                        commentsList.setAdapter(itemsAdapter);



                        commentsList.post(new Runnable() 
                        {

                            @Override
                            public void run() 
                            {
                                // TODO Auto-generated method stub
                                commentsList.setSelection(itemsAdapter.getCount()-1);

                            }
                        });


                    } 
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    catch (ExecutionException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

            }
        })
        .setCancelable(false);



      AlertDialog alert = builder.create();
        alert.setCanceledOnTouchOutside(true);

        alert.show();

        break;
user3713706
  • 1,243
  • 4
  • 15
  • 18
  • use `dialog.dismiss()` inside your positive button onclick. – Spring Breaker Jun 06 '14 at 07:17
  • Nope. Doesn't work either. It dismisses after button click but I can still see it again.. – user3713706 Jun 06 '14 at 07:21
  • it would be a better implementation if You create Your Dialog in a seperated method like private Dialog getMyDialog(){...code....return alert}; . And then call Dialog dialog = getMyDialog(); dialog.show(); in Your case – Opiatefuchs Jun 06 '14 at 07:26

4 Answers4

1

instead of this

    AlertDialog alert = builder.create();
    alert.setCanceledOnTouchOutside(true);

    alert.show();

    break;

use

    builder.create().show();

    break;
Milan Maharjan
  • 4,156
  • 1
  • 21
  • 28
0

Do the next:

AlertDialog alert; // Define it in your activity or fragment

... 

case R.id.btnAddComms:

            ...

            builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
            {
                //private ITextInputDialogCalback callback;

                public void onClick(DialogInterface dialog, int id) 
                {

                     postedComment = input1.getText().toString();
                     if(postedComment.length()>0)
                     {
                         alert.dismiss(); // Dismiss dialog here
                         PostComments(postedComment);

                     }
                     else
                     {
                         Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
                         input1.findFocus();
                     }

          ...


          alert = builder.create();
          alert.setCanceledOnTouchOutside(true);

          alert.show();

          break;
adboco
  • 2,840
  • 1
  • 21
  • 21
0

Simply replace your dialog "post" button click code to the below code:

UPDATED:

AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setMessage("your message");     

        builder.setPositiveButton("Post", new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                 postedComment = input1.getText().toString();
                     if(postedComment.length()>0)
                     {

                         PostComments(postedComment);
                         dialog.dismiss();
                     }
                     else
                     {
                         Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
                         input1.findFocus();
                     }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
        {   
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                dialog.dismiss();
            }
        });

        builder.show();
Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47
  • I think the alert box is creating twice. Because when I click post button the alert box goes.But there is still one more i can see which goes only if i click outside – user3713706 Jun 06 '14 at 08:14
0

Try a different way to create a dialog. Something like this can be used:

new AlertDialog.Builder(ACT_NewsListings.this)
            .setTitle("Title")
            .setMessage("Message")
            .setCancelable(false)

            .setPositiveButton("Post", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    dialog.dismiss();
                                        //YOUR STUFF HERE
                }
            })
            .show();
Parth Kapoor
  • 1,494
  • 12
  • 23