Giter Site home page Giter Site logo

patilshreyas / materialdialog-android Goto Github PK

View Code? Open in Web Editor NEW
935.0 13.0 142.0 10.16 MB

📱Android Library to implement animated, 😍beautiful, 🎨stylish Material Dialog in android apps easily.

License: Apache License 2.0

Java 100.00%
material-design dialog material-ui android-library android-ui android material-dialog animation-library lottie hacktoberfest

materialdialog-android's Introduction

Maven Central API Android Weekly

Github Followers GitHub stars GitHub forks GitHub watchers Twitter Follow

Material Dialogs for Android 📱

📱Android Library to implement animated, 😍beautiful, 🎨stylish Material Dialog in android apps easily.

1. Material Dialog 2. Animated Material Dialog 3. Bottom Sheet Material Dialog 4. Animated Bottom Sheet Material Dialog

Table of Contents:

Introduction

MaterialDialog library is built upon Google's Material Design library. This API will be useful to create rich, animated, beautiful dialogs in Android app easily. This library implements Airbnb's Lottie library to render After Effects animation in app. Refer this for Lottie documentation.

Types of Dialog

MaterialDialog library provides two types of dialog i.e.

1. Material Dialog 2. Bottom Sheet Material Dialog
This is basic material dialog which has two material buttons (Same as Android's AlertDialog) as you can see below. This is Bottom Sheet material dialog which has two material buttons which is showed from bottom of device as you can see below.

Implementation

Implementation of Material Dialog library is so easy. You can check /app directory for demo. Let's have look on basic steps of implementation.

Prerequisite

i. Gradle

In Build.gradle of app module, include these dependencies. If you want to show animations, include Lottie animation library.

This library is available on MavenCentreal

repositories {
    mavenCentral()
}

dependencies {

    // Material Dialog Library
    implementation 'dev.shreyaspatil.MaterialDialog:MaterialDialog:2.2.3'

    // Material Design Library
    implementation 'com.google.android.material:material:1.0.0'

    // Lottie Animation Library
    implementation 'com.airbnb.android:lottie:3.3.6'
}

ii. Set up Material Theme

Setting Material Theme to app is necessary before implementing Material Dialog library. To set it up, update styles.xml of values directory in app.

<resources>
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <!-- Customize your theme here. -->
        ...
    </style>
</resources>

These are required prerequisites to implement Material Dialog library.

iii. Customize Dialog Theme (Optional)

If you want to customize dialog view, you can override style in styles.xml as below.

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:fontFamily">@font/montserrat</item>

        <!-- Customize your theme here. -->
        <item name="material_dialog_background">#FFFFFF</item>
        <item name="material_dialog_title_text_color">#000000</item>
        <item name="material_dialog_message_text_color">#5F5F5F</item>
        <item name="material_dialog_positive_button_color">@color/colorAccent</item>
        <item name="material_dialog_positive_button_text_color">#FFFFFF</item>
        <item name="material_dialog_negative_button_text_color">@color/colorAccent</item>
    </style>

Create Dialog Instance

As there are two types of dialogs in library. Material Dialogs are instantiated as follows.

i. Material Dialog

MaterialDialog class is used to create Material Dialog. Its static Builder class is used to instantiate it. After building, to show the dialog, show() method of MaterialDialog is used.

        MaterialDialog mDialog = new MaterialDialog.Builder(this)
                .setTitle("Delete?")
                .setMessage("Are you sure want to delete this file?")
                .setCancelable(false)
                .setPositiveButton("Delete", R.drawable.ic_delete, new MaterialDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        // Delete Operation
                    }
                })
                .setNegativeButton("Cancel", R.drawable.ic_close, new MaterialDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        dialogInterface.dismiss();
                    }
                })
                .build();

        // Show Dialog
        mDialog.show();

ii. Bottom Sheet Material Dialog

BottomSheetMaterialDialog class is used to create Bottom Sheet Material Dialog. Its static Builder class is used to instantiate it. After building, to show the dialog, show() method of BottomSheetMaterialDialog is used.

        BottomSheetMaterialDialog mBottomSheetDialog = new BottomSheetMaterialDialog.Builder(this)
                .setTitle("Delete?")
                .setMessage("Are you sure want to delete this file?")
                .setCancelable(false)
                .setPositiveButton("Delete", R.drawable.ic_delete, new BottomSheetMaterialDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_SHORT).show();
                        dialogInterface.dismiss();
                    }
                })
                .setNegativeButton("Cancel", R.drawable.ic_close, new BottomSheetMaterialDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        Toast.makeText(getApplicationContext(), "Cancelled!", Toast.LENGTH_SHORT).show();
                        dialogInterface.dismiss();
                    }
                })
                .build();

        // Show Dialog
        mBottomSheetDialog.show();

Text Alignment

Text alignment is supported for title and message of a dialog. It's configurable while building a dialog. If it's not provided in builder, TextAlignment.CENTER is considered by default i.e. it'll be always aligned in a center.

Following Alignment Enum Values are allowed to be used for Dialog text alignment:

  • TextAlignment.START: Aligns text at start / left.
  • TextAlignment.CENTER: Aligns text at center.
  • TextAlignment.END: Aligns text at end / right.

Example usage:

        MaterialDialog mDialog = new MaterialDialog.Builder(this)
                .setTitle("Lorem Ipsum Title", TextAlignment.START)
                .setMessage("Lorem Ipsum Message", TextAlignment.START)

HTML formatting for Message

HTML spanned text is supported only for dialog's message. While setting a message, you can directly provide Spanned instance as shown in following example.

        MaterialDialog mDialog = new MaterialDialog.Builder(this)
                .setMessage(Html.fromText("<b>Lorem <i>Ipsum</i></b>. <br> Click <a href=\"https://example.com\">here</a> for more information"))

Show Animations

Material Dialog Bottom Sheet Material Dialog

Animations in this library are implemented using Lottie animation library. You can get free animations files here. You can find varieties of animation files on https://lottiefiles.com. *.json file downloaded from LottieFiles should be placed in android project. There are two ways to place animation file (*.json).

For example, here delete_anim.json animation file is used to show file delete animation.

i. Using Resource File

Downloaded json file should placed in raw directory of res.

In code, setAnimation() method of Builder is used to set Animation to the dialog.

Prototype :

setAnimation(int resourceId)

Resource file should be passed to method. e.g. R.raw.delete_anim.

        MaterialButton mDialog = new MaterialDialog.Builder(this)
                .setAnimation(R.raw.delete_anim)

ii. Using Asset File

Downloaded json file should placed in asset directory.

In code, setAnimation() method of Builder is used to set Animation to the dialog.

Prototype:

setAnimation(String fileName)

Only file name with extensions should passed to method.

        MaterialButton mDialog = new MaterialDialog.Builder(this)
                // Other Methods to create Dialog........               
                .setAnimation("delete_anim.json")               
                //...

iii. Getting LottieAnimationView

To get View of Animation for any operations, there is a method in Material Dialogs which returns LottieAnimationView of dialog.

        // Get Animation View
        LottieAnimationView animationView = mDialog.getAnimationView();
        // Do operations on animationView

Dialog State Listeners

There are three callback events and listeners for Dialog.

Following are interfaces for implementations:

  • OnShowListener() - Listens for dialog Show event. Its onShow() is invoked when dialog is displayed.
  • OnCancelListener() - Listens for dialog Cancel event. Its onCancel() is invoked when dialog is cancelled.
  • OnDismissListener() - Listens for dialog Dismiss event. Its onDismiss() is dismiss when dialog is dismissed.
       ... 
       mDialog.setOnShowListener(this);
       mDialog.setOnCancelListener(this);
       mDialog.setOnDismissListener(this);
    }
    
    @Override
    public void onShow(DialogInterface dialogInterface) {
        // Dialog is Displayed
    }

    @Override
    public void onCancel(DialogInterface dialogInterface) {
        // Dialog is Cancelled
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        // Dialog is Dismissed
    }
}

Contribute

Let's develop with collaborations. We would love to have contributions by raising issues and opening PRs. Filing an issue before PR is must. See Contributing Guidelines.

Credits

This library is built using following open-source libraries.

License

Project is published under the Apache 2.0 license. Feel free to clone and modify repo as you want, but don't forget to add reference to authors :)

materialdialog-android's People

Contributors

imgbot[bot] avatar imgbotapp avatar jatsqi avatar kevingermainbusiness avatar okelloenos avatar patilshreyas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

materialdialog-android's Issues

Changes in Orientation

If device orientation is horizontal then the animation shouldn't be displayed.
This should be done in both the dialogs.

[BUG/FEATURE REQUEST] Lottie very big

When choosing a very large lottie, it looks disproportionate in the dialogue.

It can be solved by changing the property in the xml of the lottie:

android: scaleType = "centerCrop"
by
android: scaleType = "centerInside"

[ISSUE] Theme is not changed in api 32

I have two themes.xml, day and night. And I tried to add custom material dialog items to themes.xml but style did not change

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.DANISHBETON" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        <!-- MD. -->
        <item name="material_dialog_background">#FFFFFF</item>
        <item name="material_dialog_title_text_color">#000000</item>
        <item name="material_dialog_message_text_color">#5F5F5F</item>
        <item name="material_dialog_positive_button_color">@color/danisred</item>
        <item name="material_dialog_positive_button_text_color">#FFFFFF</item>
        <item name="material_dialog_negative_button_text_color">@color/danisred</item>
    </style>
<resources>

[BUG] Button alignment with multi-line-text

Version: 2.2.2
API-Level: 30
Problem: If only one of the two buttons (positive, negative) has a multi-line-text, then this button is no longer aligned with the other one.
Screenshot:
See here

Is this behavior intended or is there a configuration option somewhere that I missed?
I could eventually provide a PR for this if the fix is easy. 👍

Keeps crashing on Android7.0

On Nougat, it was keep crashing with .setAnimation("delete_anim.json") method and there is not a single Log, can you please test

NETWORK PERMISSION

After adding 3 implementations in my Gradle network permission is missing.
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();

Button Color is different setted in style.xml

I use theme that used in sample project
and color change colorPrimary, colorPrimaryDark. colorAccent to my style

but totally different color is shown to Application
that color be found anywhere of project.

please help me,

HTML text not added

i want to show some html text in dialog but it is not happening please do some changes

Generic Message support: Support for separate plain text and Spanned text

IMO using the html markup <br> is an hotfix, because if you want the MaterialDialog to be easy to use for users switching from classic AlertDialog, you can't ask them to change all of their translations, and use another language (even if HTML is pretty simple) to make it work.

Maybe keep the way things were before and add an dialog.enableHtml(boolean) ?

Originally posted by @Awsom3D in #44 (comment)

Support for HTML text in message

Hi,

is there a way to use html text, i want for example this text:

Hi, my name is John.
But I want John to be bold text.

Can I do this ? I tried with strings file "" but not.

Some issue while implementing

I tried this, but i am facing some issue with this. I called it in the on click listener of my button, but it is not taking this, getApplicationContext etc in the builder argument. I have attached a screenshot of the same. I also tried to call in activity fragment but the same issue there. if i pass getApplicationContext, it shows the class has been called in Protected state. Please help!

BottomSheetMaterialDialog dialog=new BottomSheetMaterialDialog.Builder(getActivity().getApplicationContext());

Screenshot (8)

onDismissed funcion when user cancel dialog

Write information about Bug/Feature Request here
When user cancel dialog from outside, it should have onDismissed() function in some of rare use case.

Edited..
Careless of me. i just found out dismiss listener.

Update Needed to Improve UI When Buttons Are Not Required

Hello @PatilShreyas,

I am using your library in my recent project and found that if I remove buttons, it creates a blank space which looks odd. Upon further investigation, I found that the buttons are set to "INVISIBLE" mode when they are null, which is causing the blank space to appear.

To resolve this issue for my current project, I modified the library code to set the buttons to "GONE" when they are null.

I believe this is a minor change that can easily be implemented, and I wanted to suggest it to the author. I cloned the library's repo, made the necessary modifications, and tested them in my project, which worked perfectly.

If you agree with my solution, I would be happy to contribute by submitting a pull request with the change

Thank you for creating this useful library, and I hope my feedback can help improve it for the entire community. If you need any further information or clarification, please let me know.

[Feature Request] Implement separate themes for each Bottom Sheet Material Dialog called

I don't see any information regarding this but if we have 2 Bottom Sheet Material Dialog in one activity and one Bottom Sheet Material Dialog needs to have a red button while the other Bottom Sheet Material Dialog needs to have a green button, this can't be done as you can't set a separate theme for each Bottom Sheet Material Dialog.

Setting a theme in styles for the whole activity forces one set of button colors (ex. red)

This component requires that you specify a valid TextAppearance attribute.

I had an error when I tried to run the project.

The error:

Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).

My styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:fontFamily">@font/montserrat</item>

    <!-- Customize your theme here. -->
    <item name="material_dialog_background">#FFFFFF</item>
    <item name="material_dialog_title_text_color">#000000</item>
    <item name="material_dialog_message_text_color">#5F5F5F</item>
    <item name="material_dialog_positive_button_color">@color/colorAccent</item>
    <item name="material_dialog_positive_button_text_color">#FFFFFF</item>
    <item name="material_dialog_negative_button_text_color">@color/colorAccent</item>
</style>

Body list of items

Add a feature in body to add multiple items like a list. Check Material Design Alert Dialog function .setItems for more what i'm talking about.

Migrating project to the Kotlin

Proposed change

  • Migrating Material Dialog Library to the Kotlin as primary programming language.
  • Support Kotlin DSLs for making dialogs.
  • This change is encouraged during "Hacktoberfest" for the participants of this event.
  • Milestone for v3.0.0

[HELP/FEATURE REQUEST]

I'm trying to create a dialog with a numberPicker normally i would use the dialog builder and inflate a layout then pass it to the builder using the setView method but after reading the source of this library I don't see any method for this neither a method for using the android dialog builders singlechoice list and multiple choice list dialog methods

Basically
Is there anyone way to set a custom view, single choice and multiple choice list dialog?
Thanks.

Change Lottie animation width and height and center it

Hello, I used material dialog in my project, and I added a lottie animation to it, but it's so big, so I tried to change its width and height but it becomes at the top left of the dialog.

Any solution to center it ?🙂

IMG_20221113_164525

[BUG] Button not accessible on small screens

Hi, thanks for your work on this lib, it's super useful !

Unfortunately, some of my users are complaining that they can't click on the dialog button because it's simply out of the window, in portrait or paysage.
I found out that they have relatively small screens (720p or less).

On my larger smartphone, the animation is effectively disabled in paysage to make some more room, but on smaller phones it doesn't work either way.

Is there a way to fix this properly in the lib so users can scroll to the dialog button ?

Thanks for your input,

[FEATURE REQUEST] Message text not centered

Hi,
Now the dialog message text is forced to be aligned centred. It would be nice to choose the alignment (left, right,...).
Or at least by default left and not center.
Thanks

Migrate project to Kotlin

I would like to contribute to this project by helping to migrate all existing Java code to Kotlin

add edittext

Write information about Bug/Feature Request here
Hi is it possible to add an editext to materialdialog ?

thanks.

[Tech Debt] Migrate Java to Kotlin

With Kotlin being the recommended language for Android Development, it would be great to start migrating to avoid making the library and repository obsolete in future.

Opening BottomSheetMaterialDialog in Adapter from Fragment

Hello,
so basically the question is always in the title...
I wanted to ask how can I build the BottomSheetMaterialDialog in my Adapter which is used in a fragment.
The situation is, that I've got a app, which has got 2 fragments and of the fragments has got a recyclerview with some items and i made a onlongclicklistener onto the layout that works so far. But how can I open the BottomSheetMaterialDialog when I longclick the item. I passed my main activity to the adapter but when I try to open it, the app crashes with the error message
java.lang.IllegalStateException: System services not available to Activities before onCreate()

Seeing forward to your answer
Thank you
Philipp

Lottie animation won't display

After following implementation of the dialog, the animation won't be displayed like in the examples provided.

Here is what i did:
// Material Design
implementation 'com.google.android.material:material:1.2.0-alpha06'
// Material Dialog Library
implementation 'com.shreyaspatil:MaterialDialog:2.1'
// Lottie Animation Library
implementation 'com.airbnb.android:lottie:3.4.0'

Added the animation used in your examples to the raw folder

image

Called dialog
val mDialog = MaterialDialog.Builder(activity)
.setTitle("Remove?")
.setMessage("Are you sure want to remove this book?")
.setCancelable(false)
.setPositiveButton("Remove", R.drawable.ic_delete_white_24dp) { _, _ ->
remove(book, initialBooks.indexOf(book))
}
.setNegativeButton("Cancel", R.drawable.ic_close_24dp) { dialog, _ ->
dialog.dismiss()
}
.setAnimation(R.raw.deletion_one_file)
.build()

mDialog.show()

This is the result
image

This is inside of a Activity()

I even tried measuring the mDialog.animationView and returns height: 350 but width: -1

update 1:
I get this error when build() function is called
W/System.err: java.lang.UnsupportedOperationException: Can't convert to ComplexColor: type=0x2 W/System.err: at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:1152) at android.content.res.ResourcesImpl.loadComplexColorFromName(ResourcesImpl.java:1028) at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1107) at android.content.res.Resources.getColorStateList(Resources.java:1126) at android.content.Context.getColorStateList(Context.java:709) at androidx.core.content.ContextCompat.getColorStateList(ContextCompat.java:492) at com.shreyaspatil.MaterialDialog.AbstractDialog.createView(AbstractDialog.java:180) at com.shreyaspatil.MaterialDialog.MaterialDialog.<init>(MaterialDialog.java:36) at com.shreyaspatil.MaterialDialog.MaterialDialog$Builder.build(MaterialDialog.java:166) at com.guia.app.activities.MainActivity.setViews(MainActivity.kt:56) at com.guia.app.activities.MainActivity.onCreate(MainActivity.kt:35) at android.app.Activity.performCreate(Activity.java:7989) at android.app.Activity.performCreate(Activity.java:7978) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3316) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3485) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2045) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7478) W/System.err: at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.