简体   繁体   中英

android how to change background colour in AlertDialog

i am making an application in which i am using alertdialog builder ,its working properly ,but the problem is i am unable to change the colour of header of that alertbox and the background of cancel button.what i do ,here is my code

 private void SingleChoiceWithRadioButton() {  
builder = new AlertDialog.Builder(this,R.style.myCoolDialog);  
builder = new AlertDialog.Builder(this);  
    builder.setTitle("Select Country Code");  

    builder.setSingleChoiceItems(CountryCodeArray, -1,  
            new DialogInterface.OnClickListener() {  
        @Override  
        public void onClick(DialogInterface dialog, int value) {  
            String country = "";
            String[]  countries = CountryCodeArray[value].split("  ");

            dialog.dismiss();  
            spinner_text.setText( countries[0]);

            System.out.println(""+spinner_text.getText().toString());
        }  
    });  

    builder.setNegativeButton("Cancel",  
            new DialogInterface.OnClickListener() {  
        @Override  
        public void onClick(DialogInterface dialog, int which) {  
            dialog.dismiss();  
        }  
    });  
    alert = builder.create();  
    alert.show();  
}

here is image

在此处输入图片说明

Thanks in advance...:)

finally i did it..created a custom dialog..,and its working

private void showdialog() {


    final Dialog dialog = new Dialog(RegisterScreen.this, R.style.CenterDialog);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog_layout);
    dialog.setCancelable(true);

    final Spinner sp = (Spinner) dialog.findViewById(R.id.spinner);
    ArrayAdapter<String> adapter_chocolate = new ArrayAdapter<String>(RegisterScreen.this,
            android.R.layout.simple_spinner_item, CountryCodeArray);
    sp.setAdapter(adapter_chocolate);
    sp.setOnItemSelectedListener(new MyOnItemSelectedListener());


    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(RegisterScreen.this);
    //alertDialogBuilder.setTitle("Missing data");
    alertDialogBuilder.setCancelable(true); //if you set this false, the user will not be able to cancel the dialog by clicking BACK button
//  alertDialogBuilder.setMessage("You haven't set the quantity!");
    /*alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    alertDialogBuilder.show(); //don't forget to show the dialog! It is a common mistake.
   */


    Button btnCancel = (Button) dialog.findViewById(R.id.Button_Cancel);
    btnCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            dialog.dismiss();
        }
    });

    dialog.show();


}

its working...:)

By default your dialog gets the theme from your application's theme.. So to change the color, you need to change the theme :

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>

</resources>

A detailed example is here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM