Wednesday, October 19, 2016

android - Change AlertDialog background color

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#a3baa5"
    >
    <Button
        android:id="@+id/btn_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:layout_gravity="center"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private Button mButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mButton = (Button) findViewById(R.id.btn_alert);

        // Set a click listener for the button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Initializing a new alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

                // Set a title for alert dialog
                builder.setTitle("Say Hello!");

                // Show a message on alert dialog
                builder.setMessage("Are you want to do this?");

                // Set the positive button
                builder.setPositiveButton("Say",null);

                // Set the negative button
                builder.setNegativeButton("Not now", null);

                // Create the alert dialog
                AlertDialog dialog = builder.create();

                // Finally, display the alert dialog
                dialog.show();

                /*
                    setBackgroundDrawable
                        void setBackgroundDrawable (Drawable drawable)

                        Change the background of this window to a custom Drawable. Setting the
                        background to null will make the window be opaque. To make the window
                        transparent, you can use an empty drawable (for instance a ColorDrawable
                        with the color 0 or the system drawable android:drawable/empty.)

                        Parameters
                            drawable Drawable: The new Drawable to use for this window's background.
                */

                // Change the alert dialog background color
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));

                /*
                    setBackgroundDrawableResource
                        void setBackgroundDrawableResource (int resId)

                        Change the background of this window to a Drawable resource. Setting the
                        background to null will make the window be opaque. To make the window
                        transparent, you can use an empty drawable (for instance a ColorDrawable
                        with the color 0 or the system drawable android:drawable/empty.)

                        Parameters
                            resId int: The resource identifier of a drawable resource which
                                         will be installed as the new background.
                */

                // Another way to change alert dialog background color
                //dialog.getWindow().setBackgroundDrawableResource(android.R.color.holo_red_dark);
            }
        });
    }
}