worker8 / TourGuide

TourGuide is an Android library that aims to provide an easy way to add pointers with animations over a desired Android View
MIT License
2.63k stars 416 forks source link

Overlay "hole" don't resize well when showed twice #117

Closed acorn371 closed 6 years ago

acorn371 commented 6 years ago

Hi, I'd like to show a simple tooltip on a searchView in my fragment. I'm running Androidstudio Emulator with android 7.1.1 and support library. Here the code:

    final View searchView = getActivity().findViewById(R.id.searchView);

    final Overlay overlay = new Overlay().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tourGuide.cleanUp();
            tourGuide.playOn(searchView);
        }
    });

    tourGuide = TourGuide.init(getActivity())
            .setOverlay(overlay)
            .setPointer(new Pointer())
            .setToolTip(new ToolTip().setGravity(Gravity.BOTTOM|Gravity.CENTER).setTitle("Title").setDescription("Description"))
            .playOn(searchView);

Here the fragment layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical">

<android.support.v7.widget.SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/searchView"
    android:iconifiedByDefault="false"
    android:imeOptions="actionSearch"
    app:queryHint="@string/order_search_hint"
    android:focusable="false" />

<android.support.v7.widget.RecyclerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/lineeXBacinoListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="LinearLayoutManager"
    tools:context="it.marcuzzi.android.tperme.ui.MainActivity"
    tools:listitem="@layout/line_selector_item" android:layout_alignParentBottom="true"
    android:layout_below="@id/searchView"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/messageTextView"
    android:layout_alignParentBottom="false"
    android:visibility="visible"
    android:text="@string/no_data_loaded"
    android:layout_centerInParent="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp" android:layout_centerVertical="true"/>

At the first time, my tooltip is showed well. droid screen-1

When I play it again, the Overlay "hole" is quite bigger and my tooltip is not visible, showing outside the display, i suppose. You can see my tooltip squeezed on the right of the second picture.

droid screen-3

acorn371 commented 6 years ago

Well, I apologize, i found it, it was my fault! I wal calling tourGuide.playOn(searchView) from within Fragment.onCreateOptionsMenu, when target view searchView wasn't layed out yet.

I moved my code in a post-layout event as below:


view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < 16)
                    view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                else
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                tourGuide = TourGuide.init(getActivity())
                        .setOverlay(overlay)
                        .setPointer(new Pointer())
                        .setToolTip(new ToolTip().setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL).setTitle("Title").setDescription("Description"))
                        .playOn(searchView);

            }
});

And now it works!