Monday, October 24, 2016

android - AlertDialog custom layout

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="#2666ba"
    >
    <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>
res/layout/alertdialog_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_view"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    >
    <RelativeLayout
        android:id="@+id/title_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        >
        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_marginRight="10dp"
            android:layout_marginEnd="10dp"
            />
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Alert Title"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_toRightOf="@id/iv_icon"
            android:layout_toEndOf="@+id/iv_icon"
            />
    </RelativeLayout>
    <ImageView
        android:id="@+id/iv_top_divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#b1afaf"
        />
    <RelativeLayout
        android:id="@+id/message_bar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <TextView
            android:id="@+id/tv_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Put your message here."
            android:layout_margin="25dp"
            android:textSize="18sp"
            android:textStyle="italic"
            />
    </RelativeLayout>
    <ImageView
        android:id="@+id/iv_bottom_divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#b1afaf"
        />
    <RelativeLayout
        android:id="@+id/button_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/btn_positive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:text="Yes"
            />
        <Button
            android:id="@+id/btn_negative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/btn_positive"
            android:text="No"
            />
        <Button
            android:id="@+id/btn_neutral"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            />
    </RelativeLayout>
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
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.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


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

    private CoordinatorLayout mCLayout;
    private Button mButton;

    private AlertDialog mDialog;
    private View mCustomView;
    private RelativeLayout mTitleBar;
    private RelativeLayout mMessageBar;
    private RelativeLayout mButtonBar;
    private ImageView mTopDivider;
    private ImageView mBottomDivider;
    private ImageView mIcon;
    private TextView mTitle;
    private TextView mMessage;
    private Button mPositive;
    private Button mNegative;
    private Button mNeutral;


    @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);

        // Get the custom layout
        LayoutInflater layoutInflater = getLayoutInflater();
        mCustomView = layoutInflater.inflate(R.layout.alertdialog_custom_view,null);

        // Get the widgets reference from custom view
        mTitleBar = (RelativeLayout) mCustomView.findViewById(R.id.title_bar);
        mMessageBar = (RelativeLayout) mCustomView.findViewById(R.id.message_bar);
        mButtonBar = (RelativeLayout) mCustomView.findViewById(R.id.button_bar);

        mTopDivider = (ImageView) mCustomView.findViewById(R.id.iv_top_divider);
        mBottomDivider = (ImageView) mCustomView.findViewById(R.id.iv_bottom_divider);
        mIcon = (ImageView) mCustomView.findViewById(R.id.iv_icon);

        mTitle = (TextView) mCustomView.findViewById(R.id.tv_title);
        mMessage = (TextView) mCustomView.findViewById(R.id.tv_message);

        mPositive = (Button) mCustomView.findViewById(R.id.btn_positive);
        mNegative = (Button) mCustomView.findViewById(R.id.btn_negative);
        mNeutral = (Button) mCustomView.findViewById(R.id.btn_neutral);

        // Initializing a new alert dialog builder
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

        // Set the custom layout for alert dialog
        builder.setView(mCustomView);

        // Set a title for alert dialog
        mTitle.setText("Say Hello!");

        // set text for positive button
        mPositive.setText("Say");

        // Set a message for alert dialog
        mMessage.setText("Are you want to say?");

        // Set the icon visibility gone, if dialog have no icon
        //mIcon.setVisibility(View.GONE);

        // Set an icon for alert dialog
        mIcon.setImageDrawable(
                ContextCompat.getDrawable(
                        mContext,R.drawable.ic_volume_up_black_48dp
                )
        );

        // Customize the alert dialog view
        mCustomView.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_red_dark));
        mTitleBar.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_green_dark));
        mMessageBar.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_orange_dark));
        mButtonBar.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_red_dark));
        mTopDivider.setBackgroundColor(Color.WHITE);
        mBottomDivider.setBackgroundColor(Color.WHITE);

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

        // Set a click listener for alert dialog positive button
        mPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Dismiss the alert dialog
                mDialog.dismiss();

                // Show a message on snack bar
                Snackbar.make(mCLayout,"Positive button clicked.",Snackbar.LENGTH_LONG).show();
            }
        });

        // Set a click listener for alert dialog negative button
        mNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Dismiss the alert dialog
                mDialog.dismiss();

                // Show a message on snack bar
                Snackbar.make(mCLayout,"Negative button clicked.",Snackbar.LENGTH_LONG).show();
            }
        });

        // Set a click listener for alert dialog neutral button
        mNeutral.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Dismiss the alert dialog
                mDialog.dismiss();

                // Show a message on snack bar
                Snackbar.make(mCLayout,"Neutral button clicked.",Snackbar.LENGTH_LONG).show();
            }
        });

        // Set a click listener for the main layout button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Finally, display the alert dialog
                mDialog.show();
           }
        });
    }
}