4

Why doesn't dialog dismiss on the first click (but shows Toast) ? On the second click it dismisses (Toast is shown again).

private void networkDialog(){
    final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
    dialog.setContentView(R.layout.custom_dialog);
    Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
    nobutton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
        }
   });
   dialog.show();
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
JohnK
  • 264
  • 3
  • 15

4 Answers4

2

Try this way .Let me inform .I hope it will help you.

     private void networkDialog()
      {

        final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
        dialog.setContentView(R.layout.custom_dialog);
        Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
        nobutton.setOnClickListener(this);
        dialog.show();

        }

Then Use onClick switch Statement

public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.dialogButLeft:
                Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
                dialog.dismiss();
                break;
         }
    }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

A bit late but a colleague had the same issue and referred to this, are you absolutely sure that you're not creating two dialogs by calling networkDialog() twice?

Add some unique text to the dialog that will be visible to you when it's displayed like System.currentTimeMillis(), that way you can see if it's called twice because the text is different.

Or add logging / run in debug

0

Make your Button also final like this:

private void networkDialog(){
    final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
    dialog.setContentView(R.layout.custom_dialog);
    final Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
    nobutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
        }
   });
   dialog.show();
}

it's working for me in my app like this:

// Initialize variables
final Dialog passwordDialog = new Dialog(BPMActivity.this,R.style.CustomDialogStyle);
passwordDialog.setContentView(R.layout.password_view);
final Button btnCancel=(Button) passwordDialog.findViewById(R.id.btn_cancel);


btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passwordDialog.dismiss();
            }
        });

passwordDialog.show();
bendaf
  • 2,981
  • 5
  • 27
  • 62
  • That's interesting, I have a very similar code, which I will add to the post and it's working... Where do you call this function? – bendaf Aug 21 '15 at 14:30
  • It's working, but only after you dismiss it by double click on the very first time. Then it works as it should be - dismiss by one click. – JohnK Aug 21 '15 at 14:37
  • For me, it dismiss by one click, I just tried it :(. – bendaf Aug 21 '15 at 14:41
  • Probably somehow double layer is called in my case on first execution ... But I really don't see how is this possible. – JohnK Aug 21 '15 at 15:07
  • 1
    could you share the code, where you call the `networkDialog()` function? – bendaf Aug 21 '15 at 15:15
  • 1
    something is wrong on dismiss(), because when I add one more button with Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); it works from the first click as it should be. So, function itself is called and works properly, but the dismiss() execution. – JohnK Aug 21 '15 at 15:22
0

I don't know if this is still relevant to the OP. But I've been banging my head against the wall for quite some time trying to figure this one out. It appears to happen in later (5-6+) Android versions and did not occur on a 4.4.2 device which I have. The solution I've found is to setFocusableInTouchMode of the Button to false:

button.setFocusableInTouchMode(false)

This answer gave me the idea:

I have to click the button twice for it to work

Community
  • 1
  • 1
Jimmy Lee Jones
  • 785
  • 4
  • 18