Full project with source code to create Do Not Dismiss AlertDialog After Clicking a Button in Android
MainActivity.java
package tech.codingpoint.overridedialogbuttonexample;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Example Message")
.setPositiveButton("Ok", null)
.setNegativeButton("Cancel", null)
.show();
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Not closing", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
}