Skip to content

PopNotification_en

Kongzue edited this page May 17, 2024 · 6 revisions

🌐 View 简体中文文档 | 繁體中文文檔

☁ PopNotification

Simple Prompt PopNotification

Provides a notification style prompt function similar to Notification. Please note that this component cannot replace Notification, and by default, it does not support display across interfaces (but can be allowed with floating window permissions). It is only used for in-app notification prompts and has more powerful customization properties. You can set text prompts, icons, and a control button, and choose to display continuously or set an automatic disappearance duration. PopNotification is a non-blocking prompt, meaning users can still interact with the interface while PopNotification is displayed.

Displaying a Simple PopNotification

Use the following code to display a PopNotification:

PopNotification.show("This is a prompt");

Display a PopNotification with an icon:

PopNotification.show(R.mipmap.img_mail_line_white, "Received an email");

Display a PopNotification with an icon and a message:

PopNotification.show(R.mipmap.img_mail_line_white, "This is the title", "Message content text");

The displayed icon can be tinted according to light/dark theme effects:

// Disable icon tinting:
PopNotification.show(R.mipmap.img_mail_line_white,"This is a prompt")
      .setAutoTintIconInLightOrDarkMode(false);

The usage of PopNotification is basically consistent with PopTip.

Setting Display Duration

PopNotification automatically disappears after 2 seconds by default. To manually adjust, use the following methods to set the display duration:

// Display for 2 seconds
PopNotification.show("This is a prompt").showShort();

// Display for 3.5 seconds
PopNotification.show("This is a prompt").showLong();

// Display for a custom duration of 1 second (in milliseconds)
PopNotification.show("This is a prompt").autoDismiss(1000);

To display continuously without automatic disappearance:

PopNotification.show("This is a prompt").noAutoDismiss();

Manually close an already displayed PopNotification:

PopNotification popNotification = PopNotification.show("This is a prompt").noAutoDismiss();
// Manually close:
popNotification.dismiss();

Button Click Callback

Button callbacks can be set through methods:

PopNotification.show(R.mipmap.img_mail_line_white, "This is the message title", "This is the message content.")
    .showLong()
    .setButton("Recall", new OnDialogButtonClickListener<PopNotification>() {
        @Override
        public boolean onClick(PopNotification popNotification, View v) {
            // Callback for clicking "Recall" button
            return false;
        }
	});

The callback has a return value; if return true, the PopNotification will not close automatically after clicking.

Additionally, DialogX provides various methods to set callbacks and button text:

// Set only button text
.setButton("Button Text")

// Set button text and callback
.setButton("Confirm", new OnDialogButtonClickListener<PopNotification>() {
    @Override
    public boolean onClick(PopNotification popNotification, View v) {
        toast("Clicked button");
        return false;
    }
});

// Hide button
.setButton(null)

Feel free to use these as you like.

PopNotification Click Callback

In addition to setting click callbacks for buttons, you can also set click callbacks for PopNotification prompts. However, once the prompt click is enabled, the text part of the PopNotification prompt can no longer be click-through.

Use the following code to set the PopNotification click callback:

PopNotification.show("This is a prompt").setOnPopNotificationClickListener(new OnDialogButtonClickListener<PopNotification>() {
    @Override
    public boolean onClick(PopNotification baseDialog, View v) {
        // Click on PopNotification
        return false;
    }
});

Lifecycle Callbacks

To monitor the lifecycle of a dialog, you can implement the .setDialogLifecycleCallback(...) interface. It's recommended to use the build() method to construct the dialog:

PopNotification.build()
        .setDialogLifecycleCallback(new DialogLifecycleCallback<PopNotification>() {
            @Override
            public void onShow(PopNotification dialog) {
                // Callback when PopNotification is shown
            }
            @Override
            public void onDismiss(PopNotification dialog) {
                // Callback when PopNotification is dismissed
            }
        })
        .show();

PopNotification also supports Lifecycle. You can get the Lifecycle object using .getLifecycle().

You can also handle lifecycle events by overriding them when creating an instance using new, for example:

// Example of overriding events
new PopNotification() {
    @Override
    public void onShow(PopNotification dialog) {


        //...
        tip("onShow");
    }
    @Override
    public void onDismiss(PopNotification dialog) {
        //...
        tip("onDismiss");
    }
}

You can also use the methods .onShow(DialogXRunnable) and .onDismiss(DialogXRunnable) to handle lifecycle transactions, for example:

PopNotification.show(...)
        .onShow(new DialogXRunnable<PopNotification>() {
            @Override
            public void run(PopNotification dialog) {
                //PopNotification show!
            }
        })
        .onDismiss(new DialogXRunnable<PopNotification>() {
            @Override
            public void run(PopNotification dialog) {
                //PopNotification dismiss!
            }
        });

Custom Layout

To add a custom layout in the dialog, first prepare your custom layout file and then build it using the following method:

PopNotification.build()
        .setCustomView(new OnBindView<PopNotification>(R.layout.layout_custom_view) {
            @Override
            public void onBind(PopNotification dialog, View v) {
                //v.findViewById(...)
            }
        })
        .show();

In the callback parameters, v is the instantiated component of your given layout file. You can instantiate other sub-layout components using v.findViewById(resId) and set their functionality and event callbacks in the onBind method.

Custom Enter and Exit Animations

For customizing animations for a single dialog display, use the following method:

PopNotification.show("This is the body content.")
        // Set enter and exit animation resources
        .setAnimResId(R.anim.anim_dialogx_bottom_enter, R.anim.anim_dialogx_bottom_exit);

To create custom animation files, refer to: Default Dialog Start Animation File and Default Dialog Close Animation File

In addition to .setAnimResId(enterAnimResId, exitAnimResId), there are also .setEnterAnimResId(enterAnimResId) and .setExitAnimResId(enterAnimResId) methods available, which are only effective for a single dialog display.

You can also set the duration of the enter animation with setEnterAnimDuration([long]) and the exit animation with .setExitAnimDuration([long]).

Global Animation Modifications for PopNotification:

You can modify the global animations of PopNotification directly through static properties:

// Set global PopNotification enter animation
PopNotification.overrideEnterAnimRes = R.anim.anim_dialogx_notification_enter;
// Set global PopNotification exit animation
PopNotification.overrideExitAnimRes = R.anim.anim_dialogx_notification_exit;
// Set global PopNotification enter animation duration
PopNotification.overrideEnterDuration = 1000;
// Set global PopNotification exit animation duration
PopNotification.overrideExitDuration = 1000;

Modification through Custom Themes

You can also completely customize the default animation effects of the global dialog box by referring to the custom theme interface. For details, please consult Custom DialogX Themes

Note that these three settings have different priorities: Customization for a single dialog display > Global PopNotification animation modification > Custom theme modification.

Additional Methods

// Allow multiple PopNotifications to be popped up simultaneously
DialogX.onlyOnePopNotification = false;

// Set title
.setTitle(resId/String)

// Set body content
.setMessage(resId/String)

// Force refresh the interface
.refreshUI();

// Close the dialog
.dismiss();

// Set title text style
.setTitleTextInfo(TextInfo);

// Set button text style
.setButtonTextInfo(TextInfo);

// Set prompt text style
.setMessageTextInfo(TextInfo);

// Get the instantiated dialog object, you can use this method for deeper customization of the Dialog's functionality
.getDialogImpl()

// Get custom layout instance
.getCustomView()
    
// Set background color, forcibly tint the dialog background, note that the parameter is an int type color value, not an R.color index
.setBackgroundColor(ColorInt);

// Set dialog corner radius (will clip the content display)
.setRadius(float px)

// Hide the dialog (without animation), to restore display execute the non-static method .show()
.hide();

// Check if it is currently showing
.isShow()

// Front dialog box display hierarchy
.bringToFront()

// Specify the level of dialog display
.setThisOrderIndex(int)

When DialogX.onlyOnePopNotification is set to false, it allows multiple instances of PopNotification to pop up simultaneously. The popped-up instances will shift a certain distance to avoid overlapping with the newly popped-up PopNotification, as shown in the image below.

onlyOnePopNotification = false

After enabling this switch, PopNotification will only be able to display one instance at a time, automatically closing the old instance when a new one pops up.

Cross-Interface Display (Experimental)

Cross-Interface Display

Cross-interface display is an experimental feature that requires additional floating window permissions (android.permission.SYSTEM_ALERT_WINDOW) and activation of floating window permissions to use. However, due to theme extensibility considerations and limitations in newer versions of Android, floating window Windows cannot achieve click-through when displayed over other applications. We are still looking for better solutions.

Activation method:

DialogX.globalHoverWindow = true;

Check and apply for permissions:

  1. First, configure the permission in AndroidManifest.xml:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  2. Use code to check and apply for floating window permissions:

    // Floating window permission check
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(context

)) { Toast.makeText(me, "Using DialogX.globalHoverWindow requires floating window permission", Toast.LENGTH_LONG).show(); Intent intent = new Intent(); intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); startActivity(intent); return; } }


3. Start PopNotification:

```java
PopNotification.build()
        .setDialogImplMode(DialogX.IMPL_MODE.WINDOW)	// Important, no need to set if global use of Window implementation mode
        .setTitle("This is a message " + notificationIndex)
        .setIcon(icon)
        .setButton("Reply", new OnDialogButtonClickListener<PopNotification>() {
            @Override
            public boolean onClick(PopNotification baseDialog, View v) {
                Intent intent = new Intent(me, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
                return false;
            }
        })
        .showLong();

When this method is enabled, all dialog components can be displayed across interfaces when using the Window implementation mode.

Additional Components

TextInfo

TextInfo is used to store basic text style settings, containing a series of properties and their corresponding get/set methods, explained as follows:

Property Explanation Default Value
fontSize Font size, use default style when value is -1, unit: dp -1
gravity Alignment, use default style when value is -1, values like Gravity.CENTER can be used -1
fontColor Text color, use default style when value is 1, values can be obtained with Color.rgb(r,g,b) 1
bold Bold style false

Please note, fontColor is a ColorInt value, you can set a HEX color with Color.parseColor("#4D000000"), or set a color resource with getResources().getColor(R.color.black30). Do not directly pass in a resource ID as it may be ineffective.

Specifying Style Individually

If your App incorporates multiple themes and you need a dialog to display in a specific non-global theme style in certain scenarios, you can use .build() to construct the dialog. Then, apply .setStyle(style) to specify the theme style, and finally execute .show() to display the dialog. For example:

PopNotification.build()
        // or directly use .build(IOSStyle.style())
        .setStyle(IOSStyle.style())
        .setMessage("Message content.")
        .setButton("Button")
        .show();
Clone this wiki locally