-2

I have a created a alert dialog for exit the app, when back button is pressed. i want to apply the Button Focused and Button Pressed for Yes or No buttons in Alertdialog. i have already applied Button Focused and Button Pressed for other buttons in my app.

public void onBackPressed() {
    // TODO Auto-generated method stub
    createDialog();
}

private void createDialog() {
    // TODO Auto-generated method stub
    AlertDialog.Builder alertdlg = new AlertDialog.Builder(this);
    alertdlg.setMessage("Are you sure you want to exit?");
    alertdlg.setCancelable(false);
    alertdlg.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            Myactivity.super.onBackPressed();

        }
    });
    alertdlg.setNegativeButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    }); 

    alertdlg.create().show();

 }
daas12
  • 25
  • 6

1 Answers1

0

you can use following code:

// your code

AlertDialog dialog = alertdlg.create();
dialog.show();

Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null)
{

 int sdk = android.os.Build.VERSION.SDK_INT;
 if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
       b.setBackgroundDrawable(getResources().getDrawable(R.drawable.your_selector));
 } else {
       b.setBackground(getResources().getDrawable(R.drawable.your_selector));
   }
}

or you can create custom view and show that

for more info you can see This question

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • Thanks. It worked, but it gives warning "The method setBackgroundDrawable(Drawable) from the type view is deprecated" how to solve this. – daas12 Oct 04 '14 at 12:15
  • yes that method has been deprecated, because of that I use if-else method to check version of sdk, if your minSdkVersion is greater that `JELLY_BEAN`s version you can remove that, but don't worry about that, – Shayan Pourvatan Oct 04 '14 at 12:18