Sunday, October 23, 2016

android - Show an icon on AlertDialog

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="#453d63"
    >
    <Button
        android:id="@+id/btn_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:layout_gravity="top|center_horizontal"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.ContextCompat;
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 the alert dialog title
                builder.setTitle("Say Hello!");

                /*
                    AlertDialog.Builder setIcon (Drawable icon)

                        Set the Drawable to be used in the title.

                        Note: To ensure consistent styling, the drawable should be
                              inflated or constructed using the alert dialog's themed
                              context obtained via getContext().

                        Parameters
                            icon : Drawable

                        Returns
                            AlertDialog.Builder this Builder object to allow for
                                                chaining of calls to set methods
                */

                // Display an icon on alert dialog
                builder.setIcon(
                        ContextCompat.getDrawable(
                                mContext,R.drawable.ic_volume_up_black_48dp
                        )
                );

                // Set the alert dialog message
                builder.setMessage("Are you want to say it?");

                // Set a positive button for alert dialog
                builder.setPositiveButton("Say",null);

                // Set a negative button for alert dialog
                builder.setNegativeButton("No",null);

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

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