how to display ToolTip in android?

后端 未结 11 1181
情书的邮戳
情书的邮戳 2020-12-17 09:04

I want to display the ToolTip(QuickAction View) when I am moving my cursor on the view. Can any one please give me the simple example for it? tooltip will only contains the

相关标签:
11条回答
  • 2020-12-17 09:29

    Based on ban's Answer, I've created this method.

    It does not assume anything about the toast size. Simply places the tool tip gravity based on where the view is relative to the window (i.e. left/right/above/below the center of the window). The toast always starts from center of the view and will stretch towards the right/left/bottom/top respectively.

    See Example

    private static void setToastGravity(View view, Toast toast) {
        final Rect viewRect = new Rect(); // view rect
        final Rect windowRect = new Rect(); // window rect
        view.getGlobalVisibleRect(viewRect);
        view.getWindowVisibleDisplayFrame(windowRect);
    
        int offsetX;
        int offsetY;
        int gravityX;
        int gravityY;
    
        if (viewRect.centerY() > windowRect.centerY()) {
            // above
            offsetY = windowRect.bottom - viewRect.centerY();
            gravityY = Gravity.BOTTOM;
        } else {
            // tooltip below the view
            offsetY = viewRect.centerY() - windowRect.top;
            gravityY = Gravity.TOP;
        }
    
        if (viewRect.centerX() > windowRect.centerX()) {
            // tooltip right of the view
            offsetX = windowRect.right - viewRect.centerX();
            gravityX = Gravity.END;
        } else {
            // tooltip left of the view
            offsetX = viewRect.centerX() - windowRect.left;
            gravityX = Gravity.START;
        }
    
        toast.setGravity(gravityX | gravityY, offsetX, offsetY);
    }
    
    0 讨论(0)
  • 2020-12-17 09:31

    Using AndroidX is the recommended way.

    Android 4.0 (API 14) and higher

    AndroidX support Library added support for tooltips (small popup windows with descriptive text) for views and menu items.

    Use setTooltipText to set the tooltip text which will be displayed in a small popup next to the view.

    See the following example:

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    TooltipCompat.setTooltipText(fab, "Send an email");
    

    The tooltip will be displayed:

    • On long click, unless it is handled otherwise (by OnLongClickListener or a context menu).
    • On hover, after a brief delay since the pointer has stopped moving

    To add the Appcompat library into your project,

    1. Open the build.gradle file for your application.

    2. Add the support library to the dependencies section.

      dependencies {
      
      compile "androidx.appcompat:appcompat:1.1.0"
      }
      

    Android 8.0 (API level 26) and higher

    If your minimum supported version is Android 8.0 (API level 26) and higher, you can specify the tooltip text in a View by calling the setTooltipText() method. You can also set the tooltipText property using the corresponding XML.

    To specify the tooltip text in your XML files, set the android:tooltipText attribute, as shown in the following example:

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:tooltipText="Send an email" />
    

    To specify the tooltip text in your code, use the setTooltipText(CharSequence) method, as shown in the following example:

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setTooltipText("Send an email");
    
    0 讨论(0)
  • 2020-12-17 09:31

    Starting with Android API 14+, there is an event for hovering. You can do,

    view.setOnHoverListener(...)
    

    and listen for MotionEvents such as ACTION_HOVER_ENTER and ACTION_HOVER_EXIT, instead of onLongClick.

    0 讨论(0)
  • 2020-12-17 09:31

    add this to your button

    android:tooltipText="Tooltip text goes here"
    
    0 讨论(0)
  • 2020-12-17 09:38

    Android doesn't have tool tips. It is a touch-based UI. Current touch sensors can't generally detect hovering in a way that tool tips would be useful.

    There's no concept of "hovering" in a touch screen, but you could set a LongClickListener for your View, and have a Toast appear after a long press.

    0 讨论(0)
  • 2020-12-17 09:38

    If you need to show tool tip for any view, you can use CheatSheet util class from Roman Nurik. (Uses Toast and optionally content description to show tooltip.)

    It is

    Android helper class for showing cheat sheets (tooltips) for icon-only UI elements on long-press. This is already default platform behavior for icon-only action bar items and tabs. This class provides this behavior for any other such UI element

    0 讨论(0)
提交回复
热议问题