how to display ToolTip in android?

后端 未结 11 1182
情书的邮戳
情书的邮戳 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:42

    Based on GregoryK's answer, I've created a new ImageButton class - see code below. To use it, all you need to do is replace the ImageButton in your layouts with com.yourpackage.ImageButtonWithToolTip and give it an android:contentDescription attribute (as that is the text that will be shown in the tool tip).

    package com.yourpackage;
    
    import android.annotation.TargetApi;
    import android.content.Context;
    import android.graphics.Rect;
    import android.text.TextUtils;
    import android.util.AttributeSet;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.Toast;
    
    public class ImageButtonWithToolTip extends ImageButton {
    
        private static final int ESTIMATED_TOAST_HEIGHT_DIPS = 48;
    
        public ImageButtonWithToolTip(Context context) {
            super(context);
            init();
        }
    
        public ImageButtonWithToolTip(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public ImageButtonWithToolTip(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        @TargetApi(21)
        public ImageButtonWithToolTip(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            init();
        }
    
        private void init() {
            setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
    
                    /**
                     * You should set the android:contentDescription attribute in this view's XML layout file.
                     */
    
                    String contentDescription = getContentDescription().toString();
    
                    if (TextUtils.isEmpty(contentDescription)) {
    
                        /**
                         * There's no content description, so do nothing.
                         */
    
                        return false; // Not consumed
    
                    }
                    else {
    
                        final int[] screenPos = new int[2]; // origin is device display
                        final Rect displayFrame = new Rect(); // includes decorations (e.g. status bar)
                        view.getLocationOnScreen(screenPos);
                        view.getWindowVisibleDisplayFrame(displayFrame);
    
                        final Context context = view.getContext();
                        final int viewWidth = view.getWidth();
                        final int viewHeight = view.getHeight();
                        final int viewCenterX = screenPos[0] + viewWidth / 2;
                        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
                        final int estimatedToastHeight = (int) (ESTIMATED_TOAST_HEIGHT_DIPS
                                * context.getResources().getDisplayMetrics().density);
    
                        Toast toolTipToast = Toast.makeText(context, contentDescription, Toast.LENGTH_SHORT);
                        boolean showBelow = screenPos[1] < estimatedToastHeight;
                        if (showBelow) {
                            // Show below
                            // Offsets are after decorations (e.g. status bar) are factored in
                            toolTipToast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
                                    viewCenterX - screenWidth / 2,
                                    screenPos[1] - displayFrame.top + viewHeight);
                        }
                        else {
                            // Show above
                            // Offsets are after decorations (e.g. status bar) are factored in
                            // NOTE: We can't use Gravity.BOTTOM because when the keyboard is up
                            // its height isn't factored in.
                            toolTipToast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
                                    viewCenterX - screenWidth / 2,
                                    screenPos[1] - displayFrame.top - estimatedToastHeight);
                        }
    
                        toolTipToast.show();
    
                        return true; // Consumed
    
                    }
    
                }
    
            });
    
        }
    
    }
    

    You can use the same approach for extending other views - for example, Button.

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

    Android supports "tool-tip" only for ActionBar buttons from Android 4.0 on. But as Jaguar already mentioned, tool-tips in Android doesnt make so much sense, since there is no concept of hovering.

    From Android 4.0 the normal title text (that you set in the xml file or via code) will appear if you make a long click on the button. But if enough space is on the screen, it will be visible in the ActionBar all the time beside the icon.

    If you want to have it for a custom view, you need to implement it yourself by adding a LongClickListener to your view, and show a Toast when pressed long:

    view.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Toast.makeText(v.getContext(), "My tool-tip text", Toast.LENGTH_SHORT).show();
            return true;
        }
    }
    

    Of course you should use a resource for the string, and not the hard coded string.

    0 讨论(0)
  • 2020-12-17 09:48
    package com.nbfc.tekis.tooltipexample;
    
    import android.support.v7.app.AppCompatActivity; import
    android.os.Bundle; import android.view.View; import
    android.widget.Button; import android.widget.GridView;
    
    import it.sephiroth.android.library.tooltip.Tooltip;
    
    public class MainActivity extends AppCompatActivity {
        /*Button button1,button2,button3,button4;*/
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void bottomTooltip(View view) {
            Button button1=(Button)findViewById(R.id.button1);
            Tooltip.make(this,new Tooltip.Builder()
                .anchor(button1, Tooltip.Gravity.BOTTOM)
                .closePolicy(new Tooltip.ClosePolicy()
                .insidePolicy(true,false)
                .outsidePolicy(true,false),4000)
                .activateDelay(900)
                .showDelay(400)
                .text("Android tooltip bottom")
                .maxWidth(600)
                .withArrow(true)
                .withOverlay(true)
                .build())
                .show();
        }
    
        public void topTooltip(View view) {
            Button button3=(Button)findViewById(R.id.button3);
            Tooltip.make(this,new Tooltip.Builder()
                .anchor(button3, Tooltip.Gravity.TOP)
                .closePolicy(new Tooltip.ClosePolicy()
                .insidePolicy(true,false)
                .outsidePolicy(true,false),4000)
                .activateDelay(900)
                .showDelay(400)
                .text("Android tooltip top")
                .maxWidth(600)
                .withOverlay(true)
                .withArrow(true)
                .build())
                .show();
        }
    
        public void rightTooltip(View view) {
            Button button2=(Button)findViewById(R.id.button2);
            Tooltip.make(this,new Tooltip.Builder()
                .anchor(button2, Tooltip.Gravity.RIGHT)
                .closePolicy(new Tooltip.ClosePolicy()
                .insidePolicy(true,false)
                .outsidePolicy(true,false),4000)
                .activateDelay(900)
                .showDelay(400)
                .text("Android tooltip right")
                .maxWidth(600)
                .withArrow(true)
                .withOverlay(true)
                .build())
                .show();
        }
    
        public void leftTooltip(View view) {
            Button button4=(Button)findViewById(R.id.button4);
            Tooltip.make(this,new Tooltip.Builder()
                .anchor(button4, Tooltip.Gravity.LEFT)
                .closePolicy(new Tooltip.ClosePolicy()
                .insidePolicy(true,false)
                .outsidePolicy(true,false),4000)
                .text("Android tooltip left")
                .maxWidth(600)
                .withArrow(true)
                .withOverlay(true)
                .build())
                .show();
        } 
    }
    
    0 讨论(0)
  • 2020-12-17 09:49

    Possibly using myView.setTooltipText(CharSequence) (from API-level 26) or TooltipCompat (prior to API-level 26) is an additonal option:

    TooltipCompat.setTooltipText(myView, context.getString(R.string.myString));
    

    Documentation says:

    Helper class used to emulate the behavior of {@link View#setTooltipText(CharSequence)} prior to API level 26.

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

    I will Happy to help you

    Please sir Try this -> android-simple-tooltip

    I hope that will work for you

    Example : Release Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }
    

    Add the dependency

    dependencies {
        implementation 'com.github.douglasjunior:android-simple-tooltip:0.2.3'
    }
    

    Add this code in your MainActivity class and make an object for your view which will you want to bind with tooltip

    View yourView = findViewById(R.id.your_view);
    
        new SimpleTooltip.Builder(this)
            .anchorView(yourView)
            .text("Texto do Tooltip")
            .gravity(Gravity.END)
            .animated(true)
            .transparentOverlay(false)
            .build()
            .show();
    

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