AlertDialog negative button by default highlighted
Show below code just change one line. alert.show().getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();
AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
alert.setTitle("Stop On The Go!");
alert.setMessage(getResources().getString(R.string.tx_confirm_msg_journey));
alert.setPositiveButton("Stop",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
stopTask();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
navigateToHomePage();
dialog.cancel();
}
});
alert.show().getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();
Just use focus on positive
add this line in your positive button
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
@Override
public void onShow(DialogInterface dialog) {
Button positive= alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positive.setFocusable(true);
positive.setFocusableInTouchMode(true);
positive.requestFocus();
}
});
For Kotlin(in Android)
AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you want to cancel the Transaction?")
.setCancelable(false)
.setPositiveButton(
"Yes"
) { dialog: DialogInterface, which: Int ->
dialog.dismiss()
// for sending data to previous activity use
// setResult(response code, data)
finish()
}
.setNegativeButton(
"No"
) { dialog: DialogInterface, which: Int -> dialog.dismiss() }
.show()
.getButton(AlertDialog.BUTTON_NEGATIVE)
.requestFocus()