Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loses the position on device orientation #33

Open
githubsaturn opened this issue Mar 23, 2016 · 2 comments
Open

Loses the position on device orientation #33

githubsaturn opened this issue Mar 23, 2016 · 2 comments

Comments

@githubsaturn
Copy link

Simply rotate the device while showing the tooltip and observe the tooltip position will be wrong after orientation change.

@githubsaturn
Copy link
Author

Anyone any thought?

@stevenlam48
Copy link

You could store a reference to the currently displayed tooltipView, and onSaveInstanceState, save the id of the Tooltipview currently being displayed. Then, in onViewStateRestored, re-create the TooltipView from the saved id if it exists.

Something along the lines of:

View aView;                            // Anchor view
Tooltip.TooltipView currentTooltip;    // reference to currently showing Tooltip
private static final int NO_TOOLTIP = -1;
private static final int TOOLTIP_ONE = 101;    // id of tooltip 1
private static final int TOOLTIP_TWO = 102;    // id of tooltip 2
private static final String TOOLTIP_RESUME_STATE = 101;
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);

        aView = findViewById(R.id.anchor_view);
        ....
        setTooltipOne();
    }
...
    // create and show your tooltip here
    private void setTooltipOne() {
        // set currentTooltip to the one you are showing
        currentTooltip = Tooltip.make(this,
                new Builder(TOOLTIP_ONE)
                        .anchor(aView, Gravity.BOTTOM)
                        .closePolicy(new ClosePolicy()
                                .insidePolicy(true, false)
                                .outsidePolicy(true, false), 3000)
                        .activateDelay(800)
                        .showDelay(300)
                        .text(R.string.hello_world)
                        .maxWidth(500)
                        .withArrow(true)
                        .withOverlay(true)
                        .typeface(mYourCustomFont)
                        .floatingAnimation(AnimationBuilder.DEFAULT)
                        .build()
        ).show();
    }

    private Tooltip.Callback tooltipCallback() {
        return new Tooltip.Callback() {
            @Override
            public void onTooltipClose(Tooltip.TooltipView tooltipView, boolean b, boolean b1) {
                currentTooltip = null;    // reset the savedTooltip
                setTooltipTwo();
            }

            @Override
            public void onTooltipFailed(Tooltip.TooltipView tooltipView) {

            }

            @Override
            public void onTooltipShown(Tooltip.TooltipView tooltipView) {

            }

            @Override
            public void onTooltipHidden(Tooltip.TooltipView tooltipView) {

            }
        };
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (currentTooltip != null) {
            // save the ID here and remove the tooltip
            outState.putInt(TOOLTIP_RESUME_STATE, currentTooltip.getId());
            currentTooltip.remove();
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // check if a tooltip has been stored and recreate it
        if (savedInstanceState != null) {
            int savedTooltipId = savedInstanceState.getInt(TOOLTIP_RESUME_STATE, NO_TOOLTIP);
            if (savedTooltipId != NO_TOOLTIP) {
                switch (savedTooltipId) {
                    case(TOOLTIP_ONE) :
                        setTooltipOne();
                        break;
                    case(TOOLTIP_TWO):
                        setTooltipTwo();
                        break;
                    ...
            }
        }
    }

You could also more cleanly do the switch from within a single method, but this is just a general idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants