开发者

How to change the color of Dialog box

开发者 https://www.devze.com 2023-01-29 16:32 出处:网络
When i installed my application in different devices Color of dialog box changes Devices to device How can i set the color of开发者_开发问答 Dialog box

When i installed my application in different devices Color of dialog box changes Devices to device How can i set the color of开发者_开发问答 Dialog box

Regards, Kariyachan


You have some clues on anddev.org. The basic idea is to extend the default theme and use it in your activity. In particular, you will need to extend the Theme.Dialog style.


Can u name the devices that u r using to test?...Probably they might contain a customized Android build so the dialog color changes. You can leave it as it is since your build would use the default style available for a device else try setting styles that will avoid this behavior.


Use activity as a dialog by setting dialog theme to it. Then you can inflate your own layout with your own background and colors.


Change color of DialogBox and do lot more with AlertDialog.

What you have to do:

When AlertDialog is visible on your screen, OnShowListener is called. So, by adding dialog.setOnShowListener(this) you will be able to customize your AlertDialog.

Code:

// Create AlertDialog
AlertDialog.Builder adb = new AlertDialog.Builder(context1);
    adb.setTitle(context1.getString(R.string.app_name))
    .setMessage(message)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
});
AlertDialog dialog = adb.create();

// Make some UI changes for AlertDialog
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(final DialogInterface dialog) {

        // Add or create your own background drawable for AlertDialog window
        Window view = ((AlertDialog)dialog).getWindow();
        view.setBackgroundDrawableResource(R.drawable.your_drawable);

        // Customize POSITIVE, NEGATIVE and NEUTRAL buttons.
        Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
        positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
        positiveButton.setTypeface(Typeface.DEFAULT_BOLD);
        positiveButton.invalidate();

        Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
        negativeButton.setTypeface(Typeface.DEFAULT_BOLD);
        negativeButton.invalidate();

        Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL);
        neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
        neutralButton.setTypeface(Typeface.DEFAULT_BOLD);
        neutralButton.invalidate();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号