I am using alert dialog with two buttons.Issue here is whenever my alert dialog gets displayed its negative button is shown highlighted.And this is happening in this dialog only rest other are working fine.Please suggest some solution.
Here is the code:
AlertDialog.Builder alert = new AlertDialog.Builder(context);
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();
Screenshot:
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();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With