Skip to content

Instantly share code, notes, and snippets.

@douglasjunior
Created June 8, 2017 11:17
Show Gist options
  • Save douglasjunior/43a024c67c54fe1c9c848f88b1b65d89 to your computer and use it in GitHub Desktop.
Save douglasjunior/43a024c67c54fe1c9c848f88b1b65d89 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_tooltip"
android:orientation="vertical">
<TextView
android:id="@+id/tv_tooltip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Medium TextMedium extMedium Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tv_proximo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/sel_bg_tooltip"
android:drawableRight="@drawable/ic_navigate_next_colorprimary_36dp"
android:gravity="center"
android:paddingLeft="6dp"
android:text="NEXT"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />
</LinearLayout>
/**
* Created by douglas on 02/05/16.
*/
public final class TooltipGenerator {
private Activity activity;
private Dialog dialog;
private Context context;
private List<SimpleTooltip> tooltips = new ArrayList<>();
private TooltipGenerator(Activity activity) {
this.activity = activity;
this.context = activity;
}
private TooltipGenerator(Dialog dialog) {
this.dialog = dialog;
this.context = dialog.getContext();
}
public static TooltipGenerator make(Activity activity) {
return new TooltipGenerator(activity);
}
public static TooltipGenerator make(Dialog dialog) {
return new TooltipGenerator(dialog);
}
public TooltipGenerator add(View atach, final String text, int gravity, SimpleTooltip.OnShowListener onShowListener) {
return add(atach, R.layout.tooltip, text, R.dimen.simpletooltip_max_width, gravity, onShowListener, true);
}
public TooltipGenerator add(View atach, final String text, int gravity) {
return add(atach, R.layout.tooltip, text, R.dimen.simpletooltip_max_width, gravity, null, true);
}
public TooltipGenerator add(View atach, @LayoutRes int layoutRes, final String text, @DimenRes int maxWidth, int gravity, SimpleTooltip.OnShowListener onShowListener, boolean arrow) {
final SimpleTooltip.Builder builder = new SimpleTooltip.Builder(context)
.anchorView(atach)
.contentView(layoutRes, R.id.tv_tooltip)
.gravity(gravity)
.text(text)
.modal(false)
.dismissOnOutsideTouch(true)
.dismissOnInsideTouch(false)
.transparentOverlay(true)
.onShowListener(onShowListener)
.onDismissListener(new SimpleTooltip.OnDismissListener() {
@Override
public void onDismiss(SimpleTooltip tooltip) {
if (tooltips.contains(tooltip))
dismiss();
}
})
.showArrow(arrow)
.margin(0f)
.animationPadding(SimpleTooltipUtils.pxFromDp(2))
.animated(true);
if (maxWidth != 0)
builder.maxWidth(maxWidth);
tooltips.add(builder.build());
return this;
}
public TooltipGenerator add(View atach, @LayoutRes int layoutRes, int gravity) {
return add(atach, layoutRes, "", 0, gravity, null, true);
}
public TooltipGenerator add(View atach, @LayoutRes int layoutRes, int gravity, boolean arrow) {
return add(atach, layoutRes, "", 0, gravity, null, arrow);
}
private void next() {
if (!tooltips.isEmpty() && !windowClosed()) {
final SimpleTooltip tooltip = tooltips.get(0);
TextView tvNext = tooltip.findViewById(R.id.tv_proximo);
tvNext.setText(tooltips.size() > 1 ? "NEXT" : "FINISH");
tvNext.setCompoundDrawablesWithIntrinsicBounds(0, 0, tooltips.size() > 1 ? R.drawable.ic_navigate_next_colorprimary_36dp : R.drawable.ic_check_colorprimary_36dp, 0);
tvNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tooltips.remove(tooltip);
tooltip.dismiss();
next();
}
});
tooltip.show();
}
}
private boolean windowClosed() {
return (activity != null && activity.isFinishing()) || (dialog != null && !dialog.isShowing());
}
public synchronized void show() {
if (!tooltips.isEmpty()) {
next();
}
}
public synchronized void dismissAll() {
for (Iterator<SimpleTooltip> it = tooltips.iterator(); it.hasNext(); ) {
SimpleTooltip st = it.next();
it.remove();
st.dismiss();
}
}
public synchronized boolean hasTooltip() {
boolean has = false;
for (Iterator<SimpleTooltip> it = tooltips.iterator(); it.hasNext(); ) {
SimpleTooltip st = it.next();
if (st.isShowing()) {
has = true;
} else {
it.remove();
}
}
return has;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment