TapTargetView icon indicating copy to clipboard operation
TapTargetView copied to clipboard

Feature Request:- Expose method which takes menuitem as target

Open squarenotch opened this issue 10 years ago • 10 comments

I have a situation where I change the visibility of a menuitem in onPrepareOptionsMenu within a fragment. I want to show a TapTargetView when the item becomes visible.

I have a reference to the menuitem and would like to create the TapTargetView with the menuitem as the target, possibly in onPrepareOptionsMenu.

I have tried getting a reference to the toolbar and finding the item in onPrepareOptionsMenu using the id but it always resolves as null in this method.

Could you assist or possibly add the ability to pass a menuitem?

Thanks Jason

squarenotch avatar Nov 20 '16 23:11 squarenotch

Have you tried targeting MenuITem.getActionView() ?

xiphirx avatar Nov 20 '16 23:11 xiphirx

@xiphirx Yes with TapTarget.forView, in onPrepareOptionsMenu, this method returns null. Possibly because it has just been inflated?

squarenotch avatar Nov 20 '16 23:11 squarenotch

TapTarget.forToolbarMenuItem(...) Should work fine. Are you sure you're setting an ID for the item and that it exists at the time you're trying to target it?

xiphirx avatar Nov 20 '16 23:11 xiphirx

The menu is inflated in CreateOptionsMenu and after some changes to the items I am calling TapTarget.forToolbarMenuItem(toolbar, R.id.menu_mymenuitem.... etc) - This always resolves to null.

I took a look at the toolbar variable and although the MenuItem is in the mItems collection, it is not in the mActionItems collection. I am guessing but its either because it has been added by the fragment or that the item is not fully added to the toolbar in the onPrepareOptionsMenu method?

You see my reason for simply being able to supply a menuitem object :) (banging head on wall)

squarenotch avatar Nov 21 '16 00:11 squarenotch

Supplying a MenuItem does the library no good if getActionView is returning null. The library currently requires a view to target.

Could you possible provide a minimal code sample that would replicate this? I can take a further look afterwards

xiphirx avatar Nov 21 '16 00:11 xiphirx

` @Override public void onPrepareOptionsMenu(Menu menu) {

    mShowSubscribedOnlyMenuItem = menu.findItem(R.id.menu_showsubscriptiononly);


    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);

    TapTargetView.showFor(getActivity(),                 // `this` is an Activity
            TapTarget.forToolbarMenuItem(toolbar, R.id.menu_showsubscriptiononly, getText(R.string.taptarget_subscribeview_title), getText(R.string.taptarget_subscribeview_description))
                    // All options below are optional
                    .outerCircleColor(R.color.colorPrimary)      // Specify a color for the outer circle
                    .targetCircleColor(R.color.white)   // Specify a color for the target circle
                    .titleTextSize(30)                  // Specify the size (in sp) of the title text
                    .titleTextColor(R.color.white)      // Specify the color of the title text
                    .descriptionTextSize(20)            // Specify the size (in sp) of the description text
                    .descriptionTextColor(R.color.white)  // Specify the color of the description text
                    .textColor(R.color.white)            // Specify a color for both the title and description text
                    .dimColor(R.color.white)            // If set, will dim behind the view with 30% opacity of the given color
                    .drawShadow(true)                   // Whether to draw a drop shadow or not
                    .cancelable(true)                  // Whether tapping outside the outer circle dismisses the view
                    .tintTarget(true)                   // Whether to tint the target view's color
                    .transparentTarget(false),        // Specify whether the target is transparent (displays the content underneath)
            //.icon(Drawable),                    // Specify a custom drawable to draw as the target
            new TapTargetView.Listener() {          // The listener can listen for regular clicks, long clicks or cancels` etc...

squarenotch avatar Nov 21 '16 00:11 squarenotch

@xiphirx Apologies, this seems to be an issue with the menu element itself. It seems an actionViewClass is needed for the method getActionView to work. The library is now working with forView(MenuItemCompat.getActionView(MenuItem)

Thank you

squarenotch avatar Nov 21 '16 00:11 squarenotch

@xiphirx It is worth noting however that this seems to introduce a problem as only menu items with an ActionViewClass will work. This is fine for something like a search action but for a standard menu item, im at a total loss. Appreciate the work.

squarenotch avatar Nov 21 '16 00:11 squarenotch

I'm skeptical that is actually the case. It seems like something else is going on since the sample app utilizes the forToolbarMenuItem function without issue.

xiphirx avatar Nov 21 '16 00:11 xiphirx

@xiphirx I have used that function as per the sample app without a problem. The sample app calls this in onCreate within an activity. I am calling in the onPrepareOptionsMenu within a fragment as the visibility is changing there. I will dive further.

squarenotch avatar Nov 21 '16 00:11 squarenotch