HeaderDialog

This is an android library to display a material-designed dialog with header.

Here are the screenshots:

Get it on Google Play


Features

  • Roboto font
  • LinkMovementMethod support
  • Justify text option
  • Icon, text and both as header
  • Custom header background color
  • Custom header text color
  • Custom header icon color
  • Custom header text gravity
  • Custom message text gravity
  • Show or hide header shadow
  • Multiline header title text option
  • Arabic text support
  • Custom view support (NEW)

Step 1: Installation

Adding the depencency

Add this to your root build.gradle file:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Now add the dependency to your app build.gradle file:

implementation 'com.github.marcoscgdev:HeaderDialog:1.0.6'

Step 2: Creating the dialog

Here is a complete snippet of it usage:

new HeaderDialog(this)
    .setColor(getResources().getColor(R.color.colorAccent)) // Sets the header background color
    .setElevation(false) // Sets the header elevation, true by default
    .setIcon(getResources().getDrawable(R.drawable.ic_info_outline_black_48dp)) // Sets the dialog icon image
    .setTitle(getResources().getString(R.string.library_name)) // Sets the dialog title
    .setMessage("Lorem ipsum dolor sit amet...") // Sets the dialog message
    .justifyContent(true) // Justifies the message text, false by default
    .setTitleColor(Color.parseColor("#212121")) // Sets the header title text color
    .setIconColor(Color.parseColor("#212121")) // Sets the header icon color
    .setTitleGravity(Gravity.CENTER_HORIZONTAL) // Sets the header title text gravity
    .setMessageGravity(Gravity.CENTER_HORIZONTAL) // Sets the message text gravity
    .setTitleMultiline(false) // Multiline header title text option, true by default
    .setView(R.layout.custom); // Set custom view to the dialog (only possible via layout resource)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
    // Your action
        }
    })
    .setNegativeButton("Close", null)
    .show();

Important note: Do not enable the justified text if you are using html content!

Step 3: Accessing inflated custom view

HeaderDialog headerDialog = new HeaderDialog(this);
...
headerDialog.show();
headerDialog.getInflatedView().findViewById(R.id.checkbox).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Checkbox clicked!", Toast.LENGTH_SHORT).show();
    }
});

Full Example

Here is a full example:

Layouts

(a). custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom layout"
        android:layout_gravity="center"/>

</LinearLayout>

(b). activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/colorPrimary"
    tools:context="com.marcoscg.headerdialogsample.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?borderlessButtonStyle"
        android:textColor="@android:color/white"
        android:text="Show headerdialog"
        android:textSize="16sp"
        android:layout_centerInParent="true"
        android:onClick="showHeaderDialog"/>

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/hideIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:textColor="#fff"
        android:text="Hide icon"/>

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/hideTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#fff"
        android:text="Hide title"/>

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/justify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:checked="true"
        android:textColor="#fff"
        android:text="Justify"/>

</RelativeLayout>

Code

MainActivity.java

package com.marcoscg.headerdialogsample;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.marcoscg.headerdialog.HeaderDialog;

public class MainActivity extends AppCompatActivity {

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

        if (Build.VERSION.SDK_INT >= 21 && getSupportActionBar()!=null)
            getSupportActionBar().setElevation(0f);
    }

    public void showHeaderDialog(View v) {
        CheckBox hideIcon = (CheckBox) findViewById(R.id.hideIcon);
        CheckBox hideTitle = (CheckBox) findViewById(R.id.hideTitle);
        CheckBox justify = (CheckBox) findViewById(R.id.justify);

        HeaderDialog headerDialog = new HeaderDialog(this);
        headerDialog.setColor(getResources().getColor(R.color.colorAccent));

        if (!hideIcon.isChecked())
            headerDialog.setIcon(getResources().getDrawable(R.drawable.ic_info_outline_black_48dp));

        if (!hideTitle.isChecked())
            headerDialog.setTitle(getResources().getString(R.string.app_name));

        headerDialog.setMessage(getResources().getString(R.string.lorem));

        if (justify.isChecked())
            headerDialog.justifyContent(true);

        headerDialog.setTitleColor(Color.parseColor("#212121"));
        headerDialog.setTitleGravity(Gravity.CENTER_HORIZONTAL);
        headerDialog.setView(R.layout.custom);
        headerDialog.setPositiveButton(android.R.string.ok, null);
        headerDialog.show();

        ((CheckBox)headerDialog.getInflatedView().findViewById(R.id.checkbox))
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this, "Checkbox checked: " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            showAboutDialog();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void showAboutDialog() {
        String msg = getResources().getString(R.string.about_text);
        new HeaderDialog(this)
                .setColor(getResources().getColor(R.color.colorAccent))
                .setIcon(getResources().getDrawable(R.mipmap.ic_launcher))
                .setTitle(getResources().getString(R.string.library_name))
                .setMessage(Html.fromHtml(msg))
                .setElevation(true)
                .justifyContent(false) // Default false
                .setTitleColor(getResources().getColor(R.color.colorPrimary))
                .setTitleGravity(Gravity.CENTER_HORIZONTAL)
                .setMessageGravity(Gravity.CENTER_HORIZONTAL)
                .setPositiveButton("Close", null)
                .setNeutralButton("View on GitHub", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/marcoscgdev/HeaderDialog/")));
                    }
                })
                .show();
    }
}

Reference

Download code here.
Read more here.
Follow code author here.