Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

can i change color of button in alert dialog when touch in that button in android. How? Thanks for your help!

share|improve this question

3 Answers

Here is how I did.

AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));

customBuilder.setTitle(R.string.popup_error_title);
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    MyActivity.this.finish();
                }  
        });
AlertDialog dialog = customBuilder.create();
dialog.show();

Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null)
        b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));

I find the drawable here

share|improve this answer
1  
hi, i am also applying the Button's BG Image like this only. but if i apply Image i am getting some unwanted Margin at the bottom of the button. if i use BG color i's working fine. i checked the button image. it fine. so can you suggest any solution for this. – Raj Oct 9 '12 at 7:49
@Raj did you find any solution for this unwanted margins? – Deepak Oct 10 '12 at 19:41
@Deepak : not yet found any solution. if you found any please let me know. – Raj Oct 11 '12 at 5:25
it worked for me thank you – NARESH REDDY Dec 13 '12 at 11:27
setBackgroundDrawable(Drawable) is deprecated in API level 16; better to use setBackground(Drawable) if you are not targeting below API level 16. – Engin Yapici Mar 17 at 21:54

Are you referring to the neutral, positive and negative buttons? Or to buttons you included in the layout?

If you are referring to the former, then yes you can. Check out the Custom Button section in this tutorial. You basically need an XML file that will tell your button which drawable/color to use for each state change. You can then set this XML file as your button's background.

share|improve this answer
No budy, I have already done that one. But i have to change the Background of the positive, neutral and the negative Button. . . – user644458 Apr 30 '11 at 7:29
Hi, did u manage to fix this issue? – jonney Jul 20 '11 at 9:55

no you cant change the color or images or background of the default buttons of alert boxes. For customization you will need to make you on custom dialog box like this.

public class TryAgainAlert extends Dialog implements OnClickListener
{
    @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
  if (keyCode == KeyEvent.KEYCODE_BACK)
  {   

   Intent i = new Intent(getApplicationContext(), MainMenu.class);
   finish();
   startActivity(i);

   return true;
  }
  return super.onKeyDown(keyCode, event);
 }


    TextView scores;
    Button tryagain,mainmenu,submit;


     public TryAgainAlert(Context context) {
        super(context);

        setContentView(R.layout.tryagainalert);

        scores=(TextView)findViewById(R.id.text);



        tryagain= (Button) findViewById(R.id.trya);
        mainmenu= (Button) findViewById(R.id.submitscore);
        submit= (Button) findViewById(R.id.mainmenu);

    }


    @Override
    public void onClick(View v) {
        if(v == tryagain)
        {

        else if (v==mainmenu)
        {


        }
        else if (v == submit)
        {

        }
    }

}

you can do what ever you want with the XML file. I hope it will help. Thanks

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.