karthik
karthik

Reputation: 524

displaying AlertDialog inside tab view activity consisting of fragments

I am trying to display AlertDialog inside tab views activity consisting of fragments . this is my java code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    switch (item.getItemId()) {
        case R.id.dis:
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); //Read Update
            alertDialog.setTitle("Support us to improve");
            alertDialog.setMessage(R.string.w4);
            alertDialog.show();
            alertDialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

now i can display the dialog box , but the onclick listener was not working for setPositiveButton() , the button was not displaying in the dialog box.

this is my output now , how do i add an button here.

enter image description here

Upvotes: 0

Views: 121

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21043

Add Positive button before showing dialog .

 AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 
        alertDialog.setTitle("Support us to improve");
        alertDialog.setMessage(R.string.w4);
        alertDialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // Do your stuff
            }
        });
       alertDialog.show();

Also you do not need to call dialogInterface.dismiss(); it will automatically dismiss . Its AlertDialog's property for default buttons.

Upvotes: 1

Related Questions