I'm trying to show a tooltip on a MenuItem
.
I'm using this library: compile 'com.github.xizzhu:simple-tool-tip:0.5.0'
from this Maven repository: maven { url "https://jitpack.io" }
.
I'm getting the view for the MenuItem
, to use it as an anchor for the ToolTip
but it is launching a nullPointerException
in the toolTipView.show()
method.
When I debug the app, the View
item is correct.
If i replace the View
item with the floating button fab, it works perfectly.
At some point, it worked, but the ToolTip
position was wrong, it appears on the left top corner of the screen, instead of the right position, which would be at bottom of View
item.
Note: I'm creating the ToolTip
inside OnCreateOptionsMenu()
because OnViewCreated()
run before this method, so I have to do it there.
Here is where it happens, if you need any other piece of code I can upload it.
Menu:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tooltipproto="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
tooltipproto:actionViewClass="android.widget.ImageButton"
android:title="@string/action_settings"
tooltipproto:showAsAction="always" />
</menu>
Fragment:
package com.tooltipproto;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.github.xizzhu.simpletooltip.ToolTip;
import com.github.xizzhu.simpletooltip.ToolTipView;
public class ShowFragment extends Fragment{
View item;
FloatingActionButton fab;
public ShowFragment(){
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_settings);
item = menuItem.getActionView();
ToolTip toolTip = new ToolTip.Builder()
.withText("Simple Tool Tip!")
.withTextSize(50)
.withPadding(10,10,10,10)
.withCornerRadius(10)
.build();
final ToolTipView toolTipView = new ToolTipView.Builder(getActivity())
.withAnchor(item)
.withToolTip(toolTip)
.withGravity(Gravity.BOTTOM)
.build();
toolTipView.show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment
setHasOptionsMenu(true);
fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment, parent, false);
}
}