Skip to content

PopTip_en

Kongzue edited this page May 17, 2024 · 5 revisions

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

🍞 Simple Prompt PopTip

Simple Prompt PopTip

Provides a text prompt function similar to Toast, but with more powerful customization options. You can set text prompts, icons, and a control button, and choose to display continuously or set an automatic disappearance duration. PopTip is a non-blocking prompt, meaning users can still interact with the interface while PopTip is displayed.

Displaying a Simple PopTip

Use the following code to display a PopTip:

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

Display a PopTip with an icon:

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

Display a PopTip with an icon and a button:

PopTip.show(R.mipmap.img_mail_line_white, "Email sent", "Recall");

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

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

Quick Prompt

To quickly build a prompt using commands, you can add an import at the beginning of the class (some IDEs can auto-generate this):

import static com.kongzue.dialogx.dialogs.PopTip.tip;

Then use it directly in the code:

// Create a text prompt
tip(message);

// Create a text prompt with a button
tip(message, okButtonText)
        .setButton(new OnDialogButtonClickListener<PopTip>() {
            @Override
            public boolean onClick(PopTip dialog, View v) {
                //...
                return false;
            }
        });

// Create a text prompt with an icon
tip(R.mipmap.icon, message);

// Create a text prompt with an icon and a button
tip(R.mipmap.icon, message, okButtonText);

Setting Display Duration

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

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

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

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

To display continuously without automatic disappearance:

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

Manually close an already displayed PopTip:

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

Button Click Callback

Button callbacks can be set through methods:

PopTip.show(R.mipmap.img_mail_line_white, "Email sent", "Recall").showLong().setButton(new OnDialogButtonClickListener<PopTip>() {
    @Override
    public boolean onClick(PopTip popTip, View v) {
        // Callback for clicking "Recall" button
        return false;
    }
});

The callback has a return value; if return true, the PopTip 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 only button click callback
.setButton(new OnDialogButtonClickListener<PopTip>() {
    @Override
    public boolean onClick(PopTip popTip, View v) {
        toast("Clicked button");
        return false;
    }
});

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

// Hide button
.setButton(null)

Feel free to use these as you like.

PopTip Click Callback

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

Use the following code to set the PopTip click callback:

PopTip.show("This is a prompt").setOnPopTipClickListener(new OnDialogButtonClickListener<PopTip>() {
    @Override
    public boolean onClick(PopTip baseDialog, View v) {
        // Click on PopTip


        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:

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

PopTip 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 PopTip() {
    @Override
    public void onShow(PopTip dialog) {
        //...
        tip("onShow");
    }
    @Override
    public void onDismiss(PopTip dialog) {
        //...
        tip("onDismiss");
    }
}

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

PopTip.show(...)
        .onShow(new DialogXRunnable<PopTip>() {
            @Override
            public void run(PopTip dialog) {
                //PopTip show!
            }
        })
        .onDismiss(new DialogXRunnable<PopTip>() {
            @Override
            public void run(PopTip dialog) {
                //PopTip 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:

PopTip.build()
        .setCustomView(new OnBindView<PopTip>(R.layout.layout_custom_view) {
            @Override
            public void onBind(PopTip 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 of a single dialog display, use the following method:

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

To customize animation files, you can refer to: Default Dialog Start Animation File and Default Dialog Close Animation File

Additionally, apart from .setAnimResId(enterAnimResId, exitAnimResId), you can also choose .setEnterAnimResId(enterAnimResId) and .setExitAnimResId(enterAnimResId) individually. These methods 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]).

For global PopTip animation modifications:

You can directly modify the global PopTip animation through static properties:

// Set global PopTip enter animation
PopTip.overrideEnterAnimRes = R.anim.anim_dialogx_bottom_enter;
// Set global PopTip exit animation
PopTip.overrideExitAnimRes = R.anim.anim_dialogx_bottom_exit;
// Set global PopTip enter animation duration
PopTip.overrideEnterDuration = 1000;
// Set global PopTip exit animation duration
PopTip.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 more details see Custom DialogX Themes

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

Additional Methods

// Allow multiple PopTips to be popped up simultaneously
DialogX.onlyOnePopTip = false;

// Force refresh the interface
.refreshUI();

// Close the dialog
.dismiss();

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

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

// Set message 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.onlyOnePopTip is set to false, it allows multiple instances of PopTip to pop up simultaneously. The popped-up instances will shift a certain distance to avoid overlapping with the newly popped-up PopTip, as shown in the image below.

onlyOnePopTip = false

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

Additional Components

TextInfo

TextInfo is used to store basic text style settings and contains a series of properties and their corresponding get/set methods. For example, the methods are 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:

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