An Android library that provides a simple implementation of a DialogFragment

Overview

SimpleDialogFragment

An Android library that provides a simple implementation of a DialogFragment.

Are you tired of creating a new DialogFragment for each Dialog that you want to display?

The current recommended way of displaying Dialogs in an Android application involves the creation of a subclass of a DialogFragment (official documentation can be found here). Creating a new DialogFragment subclass for each Dialog we need to display in an application is tedious and we all know that an application often contains a lot of these dialogs. This leads to lots of DialogFragment classes that provide no value to the project. What if we could eliminate those?

This library is a simple implementation of a DialogFragment that allows you to specify the content of the Dialog, while still using the recommended DialogFragment and all its lifecycle benefits.

Example of use :

SimpleDialogFragment.newInstance(
            SimpleDialogContent.builder()
                    .setTitle("My Title")
                    .setMessage("My Dialog Message")
                    .build())
            .show(this.getSupportFragmentManager(), SimpleDialogFragment.TAG);

Getting started

dependencies {
    compile 'com.julienarzul:simpledialogfragment:1.1.1'
}

How to use

Basic Use

The simplest way to use this library is to show a Dialog with a title, message and a positive button.

In order to do that, we simply need to create and show a new instance of the SimpleDialogFragment class. The instance created must be given a SimpleDialogContent object that will define the content of the Dialog shown.

The following example shows the code to use to display a dialog with a custom title, custom message and custom positive button text (this is the enclosing Activity or Fragment):

SimpleDialogFragment.newInstance(
            SimpleDialogContent.builder()
                    .setTitle("My Title")
                    .setMessage("My Dialog Message")
                    .setPositiveButtonText("Got it")
                    .build())
            .show(this.getSupportFragmentManager(), SimpleDialogFragment.TAG);

Supported AlertDialog content

The SimpleDialogContent object given to the SimpleDialogFragment supports defining:

  • a title
  • a message
  • a positive button
  • a negative button
  • a neutral button
  • whether the Dialog should be cancelable or not

Any combination of these attributes can easily be created via the SimpleDialogContent.Builder.

It is mandatory to specify at least a non-null message to the dialog.

Specify button listeners

More often than not, we need to implement some behaviour when the user has agreed (or disagreed) to the content of the Dialog. For that purpose, we need to add listeners to the SimpleDialogFragment. The Android documentation shows an example where the Activity containing the DialogFragment is the dialog's buttons listener (here). Implementing it that way allows the listener to be kept on activity lifecycle events (such as activity destruction/recreation).

This library uses the exact same principle. The enclosing activity must implement an interface if it want to listen to the dialog's buttons click events. The below example shows how to listen to the positive and negative buttons click events:

public class MyActivity extends AppCompatActivity implements SimpleDialogFragment.OnPositiveButtonClickListener,
    SimpleDialogFragment.OnNegativeButtonClickListener

There are three interfaces that the activity can implement:

  • SimpleDialogFragment.OnPositiveButtonClickListener
  • SimpleDialogFragment.OnNegativeButtonClickListener
  • SimpleDialogFragment.OnNeutralButtonClickListener

Particular case: Dialogs inside Fragments

When displaying a DialogFragment inside a Fragment, we probably would like to perform our Dialog click behavior directly in our Fragment and completely bypass the Activity.
SimpleDialogFragment allows you to do that easily.

Implement the listener interfaces directly in your Fragment:

public class NestedFragment extends Fragment implements SimpleDialogFragment.OnPositiveButtonClickListener,
    SimpleDialogFragment.OnNegativeButtonClickListener

and display the SimpleDialogFragment:

SimpleDialogFragment.newInstance(dialogContent)
        .show(this.getChildFragmentManager(), SimpleDialogFragment.TAG);

Warning: Since we're nesting a fragment into another one, you must use the getChildFragmentManager() method to show the dialog.

Particular case: Displaying several SimpleDialogFragments

When displaying several SimpleDialogFragment in the same Activity (or Fragment), we need a way to know which dialog triggered a click on the buttons.
To fix that problem, SimpleDialogFragment uses a Request Code, in the same manner than with onActivityResult.

When creating a SimpleDialogContent object, you can give it a request code:

SimpleDialogContent dialogContent = SimpleDialogContent.builder()
            .setMessage("Fire missiles?")
            .setPositiveButtonText("Fire")
            .setNegativeButtonText("Cancel")
            .setRequestCode(SIMPLE_DIALOG_FRAGMENT_MY_REQUEST_CODE)
            .build();

That request code is then passed to you in each of the click events triggered by the listeners:

@Override
public void onDialogPositiveButtonClicked(DialogInterface dialog, Integer requestCode) {
    if (requestCode != null) {
        switch (requestCode) {
            case SIMPLE_DIALOG_FRAGMENT_MY_REQUEST_CODE:
                // TODO: Implement positive button behaviour
                break;
        }
    }
}

Contributing

Any contributions is welcome through Pull Requests. Please take the time to clearly explain the feature you wish to add or the bug you're trying to fix.

Don't hesitate to raise an issue before opening a pull request so that we can discuss it before-hand.

License

Copyright 2017 Julien Arzul

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
Android library to show
Android library to show "Rate this app" dialog

Android-RateThisApp Android-RateThisApp is an library to show "Rate this app" dialog. The library monitors the following status How many times is the

A small library replicating the new dialogs in android L.
A small library replicating the new dialogs in android L.

L-Dialogs A small library replicating the new dialogs in android L. Set Up (Android Studio): Download the aar here: https://www.dropbox.com/s/276bhapr

📱 An Android Library for 💫fluid, 😍beautiful, 🎨custom Dialogs.
📱 An Android Library for 💫fluid, 😍beautiful, 🎨custom Dialogs.

Aesthetic Dialogs for Android 📱 📱 Android Library for 💫 fluid, 😍 beautiful, 🎨 custom Dialogs. Table of Contents: Introduction Types of Dialog Dar

Android library that allows applications to add dialog-based slider widgets to their settings
Android library that allows applications to add dialog-based slider widgets to their settings

Android Slider Preference Library Overview Slider represents a float between 0.0 and 1.0 Access with SliderPreference.getValue() or SharedPreferences.

An Android library for displaying a dialog where it presents new features in the app.
An Android library for displaying a dialog where it presents new features in the app.

WhatIsNewDialog What is new dialog for Android is used for presenting new features in the the app. It can be used in the activity starts, from menu or

Android library to show
Android library to show "Rate this app" dialog

Android-RateThisApp Android-RateThisApp is an library to show "Rate this app" dialog. The library monitors the following status How many times is the

An easy-to-use Android library that will help you to take screenshots of specif views of your app and save them to external storage (Including API 29 Q+ with Scope Storage)

🇺🇸 English | 🇧🇷 Português (pt-br) 🇺🇸 English: An easy to use Library that will help you to take screenshots 📸 of the views in your app Step 1.

An beautiful and easy to use dialog library for Android
An beautiful and easy to use dialog library for Android

An beautiful and easy to use dialog library for Android

CuteDialog- Android Custom Material Dialog Library
CuteDialog- Android Custom Material Dialog Library

A Custom Material Design Dialog Library for Android Purpose CuteDialog is a Highly Customizable Material Design Android Library. CuteDialog allows dev

Comments
  • Crash - AndroidStringResource not parcelable?

    Crash - AndroidStringResource not parcelable?

    Fatal Exception: java.lang.RuntimeException: Parcel: unable to marshal value AndroidStringResource{string=Do you want rate this app ?, resId=0}
       at android.os.Parcel.writeValue(Parcel.java:1455)
       at com.julienarzul.simpledialogfragment.AutoValue_SimpleDialogContent.writeToParcel(AutoValue_SimpleDialogContent.java:39)
       at android.os.Parcel.writeParcelable(Parcel.java:1474)
       at android.os.Parcel.writeValue(Parcel.java:1380)
       at android.os.Parcel.writeArrayMapInternal(Parcel.java:702)
       at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1412)
       at android.os.Bundle.writeToParcel(Bundle.java:1133)
       at android.os.Parcel.writeBundle(Parcel.java:742)
       at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:148)
       at android.os.Parcel.writeTypedArray(Parcel.java:1285)
       at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:564)
       at android.os.Parcel.writeParcelable(Parcel.java:1474)
       at android.os.Parcel.writeValue(Parcel.java:1380)
       at android.os.Parcel.writeArrayMapInternal(Parcel.java:702)
       at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1412)
       at android.os.Bundle.writeToParcel(Bundle.java:1133)
       at android.os.Parcel.writeBundle(Parcel.java:742)
       at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:150)
       at android.os.Parcel.writeTypedArray(Parcel.java:1285)
       at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:564)
       at android.os.Parcel.writeParcelable(Parcel.java:1474)
       at android.os.Parcel.writeValue(Parcel.java:1380)
       at android.os.Parcel.writeArrayMapInternal(Parcel.java:702)
       at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1412)
       at android.os.Bundle.writeToParcel(Bundle.java:1133)
       at android.os.Parcel.writeBundle(Parcel.java:742)
       at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3622)
       at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3974)
       at android.os.Handler.handleCallback(Handler.java:761)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:156)
       at android.app.ActivityThread.main(ActivityThread.java:6523)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
    
    opened by yuraj11 1
Owner
Julien Arzul
Julien Arzul
Backport of Material dialogs with easy-to-use API based on DialogFragment

StyledDialogs for Android Demo app: Features: Compatible with Material Design Guidelines Same look for Android 2.2+ Built on top of standard DialogFra

Avast 2.2k Dec 29, 2022
Backport of Material dialogs with easy-to-use API based on DialogFragment

StyledDialogs for Android Demo app: Features: Compatible with Material Design Guidelines Same look for Android 2.2+ Built on top of standard DialogFra

Avast 2.2k Nov 29, 2022
[Deprecated] This project can make it easy to theme and custom Android's dialog. Also provides Holo and Material themes for old devices.

Deprecated Please use android.support.v7.app.AlertDialog of support-v7. AlertDialogPro Why AlertDialogPro? Theming Android's AlertDialog is not an eas

Feng Dai 468 Nov 10, 2022
A simple library for creating animated warnings/dialogs/alerts for Android.

Noty A simple library for creating animated warnings/notifications for Android. Examples Show me code Show me code Show me code Show me code Show me c

Emre 144 Nov 29, 2022
A simple library to show custom dialog with animation in android

SmartDialog A simple library to show custom dialog in android Step 1. Add the JitPack repository to your build file allprojects { repositories {

claudysoft 9 Aug 18, 2022
Extremely useful library to validate EditText inputs whether by using just the validator for your custom view or using library's extremely resizable & customisable dialog

Extremely useful library for validating EditText inputs whether by using just the validator (OtpinVerification) for your custom view or using library's extremely resizable & customisable dialog (OtpinDialogCreator)

Ehma Ugbogo 17 Oct 25, 2022
A simple file/ directory picker dialog for android

FileListerDialog FileListerDialog helps you to list and pick file/directory. Library is built for Android Getting Started Installing To use this libra

Yogesh S 446 Jan 7, 2023
A highlight lib and also it can be a simple popup window lib for android

HighlightPro 中文 HighlightPro is a highlight library for android and also it can be a simple popup window library for android. Features: One or more hi

heyangyang 192 Jan 2, 2023
Make your native android Dialog Fancy and Gify. A library that takes the standard Android Dialog to the next level with a variety of styling options and Gif's. Style your dialog from code.

FancyGifDialog-Android Prerequisites Add this in your root build.gradle file (not your module build.gradle file): allprojects { repositories { ...

Shashank Singhal 522 Jan 2, 2023
LicensesDialog is an open source library to display licenses of third-party libraries in an Android app.

LicensesDialog LicensesDialog is an open source library to display licenses of third-party libraries in an Android app. Download Download the latest R

PSDev 817 Dec 30, 2022