Android Popupwindow with Tooltip Arrow

Android drawable tooltip arrow box

It's not that difficult, key points are three to make this layer-list Drawable:

  • The tooltip (triangle) must be fixed size.

  • The background must NOT be fixed size, since the content size is unknown.

  • Adjust the margin and padding (inside the Drawable).


1. Create the layer-list Drawable:

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">

<item //background part
android:top="17dp"> //margin top, half of the 【rotated】 rectangle height

<shape
android:shape="rectangle">

<solid
android:color="@color/purple_200" />

<corners
android:radius="8dp" />

<padding //important part
android:top="17dp" /> //offset the margin top, shows up the tooltip perfectly

</shape>

</item>

<item //tooltip part
android:width="24dp" //size must be fixed
android:height="24dp"
android:end="32dp" //margin end (right)
android:gravity="end" //place wherever you want
android:top="-12dp"> //margin top, negative half of its height, display half of this rotated rectangle

<rotate
android:fromDegrees="45">

<shape
android:shape="rectangle">

<solid
android:color="@color/purple_500" /> //change back to background part color after your experiment and testing

</shape>

</rotate>

</item>

</layer-list>

2. Apply to View:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi, I'm Sam"
android:textColor="@color/black"
android:textSize="40sp"
android:background="@drawable/chat_background" //here
android:paddingHorizontal="16dp"/> //apply padding bottom in Drawable, not in here

<TextView //just for comparison
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Hi, I'm Sam"
android:textColor="@color/black"
android:textSize="40sp"
android:background="@color/purple_200"
android:paddingHorizontal="16dp"/>

Result:

Sample Image


Little Math:

  • Un-rotated rectangle height is 24dp.
  • Rotated rectangle height is square root of (24^2)*2 = 33.9411.
  • Half of the Rotated rectangle height is 16.9706, so we take 17.

Tips For applying vertical padding:

  • Modify the padding top of Background part (ex: 17dp + 8dp = 25dp).

  • Modify the margin top of Tooltip (rotated rectangle) part to cancel out (ex: -12dp - 8dp = -20dp).

Android PopupWindow with Tooltip Arrow

There are many libraries and codes available into Market. Links are given below:

This is the QuickAction UI pattern. Take a look at:

  1. QuickAction-Dialog

  2. Quick-action-pattern-in-Android

  3. Chrome Style Help Popups

Another alternative would be "super-tooltips":

https://github.com/nhaarman/supertooltips

Here's a demo of it:

https://play.google.com/store/apps/details?id=com.haarman.supertooltips

From that first link/example looks like below image.
These are just demos, but you can customize as per your requirement.

Sample Image

How to implement a small popup with some images on a button click

you can use this library to achieve your goal look at this

  • Android Tooltip library 1
  • Android Tooltip library 2
  • Android Tooltip library 3
  • Android Tooltip library 4

and if you want create your custom than use following class
create one custom class like this

public class TooltipWindow {

private static final int MSG_DISMISS_TOOLTIP = 100;
private Context ctx;
private PopupWindow tipWindow;
private View contentView;
private LayoutInflater inflater;

public TooltipWindow(Context ctx) {
this.ctx = ctx;
tipWindow = new PopupWindow(ctx);

inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.tooltip_layout, null);
}

void showToolTip(View anchor) {

tipWindow.setHeight(LayoutParams.WRAP_CONTENT);
tipWindow.setWidth(LayoutParams.WRAP_CONTENT);

tipWindow.setOutsideTouchable(true);
tipWindow.setTouchable(true);
tipWindow.setFocusable(true);
tipWindow.setBackgroundDrawable(new BitmapDrawable());

tipWindow.setContentView(contentView);

int screen_pos[] = new int[2];
// Get location of anchor view on screen
anchor.getLocationOnScreen(screen_pos);

// Get rect for anchor view
Rect anchor_rect = new Rect(screen_pos[0], screen_pos[1], screen_pos[0]
+ anchor.getWidth(), screen_pos[1] + anchor.getHeight());

// Call view measure to calculate how big your view should be.
contentView.measure(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);

int contentViewHeight = contentView.getMeasuredHeight();
int contentViewWidth = contentView.getMeasuredWidth();
// In this case , i dont need much calculation for x and y position of
// tooltip
// For cases if anchor is near screen border, you need to take care of
// direction as well
// to show left, right, above or below of anchor view
int position_x = anchor_rect.centerX() - (contentViewWidth / 2);
int position_y = anchor_rect.bottom - (anchor_rect.height() / 2);

tipWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, position_x, position_y);

// send message to handler to dismiss tipWindow after X milliseconds
handler.sendEmptyMessageDelayed(MSG_DISMISS_TOOLTIP, 4000);
}

boolean isTooltipShown() {
if (tipWindow != null && tipWindow.isShowing())
return true;
return false;
}

void dismissTooltip() {
if (tipWindow != null && tipWindow.isShowing())
tipWindow.dismiss();
}

Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_DISMISS_TOOLTIP:
if (tipWindow != null && tipWindow.isShowing())
tipWindow.dismiss();
break;
}
}

;
};

}

now Crete your custom layout for this like tooltip_layout

<?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:orientation="vertical" >

<ImageView
android:id="@+id/tooltip_nav_up"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:background="@drawable/nav_up" />

<TextView
android:id="@+id/tooltip_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tooltip_bg"
android:gravity="center"
android:padding="10dp"
android:text="Tooltip using PopupWindow:)"
android:textColor="@android:color/white"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>

create one drawable file nav_up like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:pivotX="-50%"
android:pivotY="80%"
android:toDegrees="45" >
<shape android:shape="rectangle" >
<solid android:color="#000000" >
</solid>

<stroke
android:width="2dp"
android:color="#000000" />
</shape>
</rotate>
</item>

now use this tooltip like this

TooltipWindow tipWindow = new TooltipWindow(MainActivity.this);
@Override
public void onClick(View v) {
if (!tipWindow.isTooltipShown())
tipWindow.showToolTip(v);
}

ask me in case of any query

Add tooltip arrow to DialogFragment

There are many open source libraries and codes available to find out how it can be developed.

  1. https://github.com/nhaarman/supertooltips
  2. https://github.com/nidhinek/android-tooltip
  3. https://github.com/douglasjunior/android-simple-tooltip
  4. https://github.com/michaelye/EasyDialog

How to set the popupwindow background with anchor

Try the below example

1) SuperTooltips

2) ShowcaseView

From that first link example look like below image

Sample Image

how to display ToolTip in android?

Android supports "tool-tip" only for ActionBar buttons from Android 4.0 on. But as Jaguar already mentioned, tool-tips in Android doesnt make so much sense, since there is no concept of hovering.

From Android 4.0 the normal title text (that you set in the xml file or via code) will appear if you make a long click on the button. But if enough space is on the screen, it will be visible in the ActionBar all the time beside the icon.

If you want to have it for a custom view, you need to implement it yourself by adding a LongClickListener to your view, and show a Toast when pressed long:

view.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "My tool-tip text", Toast.LENGTH_SHORT).show();
return true;
}
}

Of course you should use a resource for the string, and not the hard coded string.

show/hide Android tooltips programmaticaly

Might be this library can help you with animated tooltip also have method to dismiss and hide the tooptip.

implementation 'com.github.douglasjunior:android-simple-tooltip:0.2.3'

Code :

 final SimpleTooltip tooltip = new SimpleTooltip.Builder(this)
.anchorView(v)
.text("your text")
.gravity(Gravity.TOP)
.animated(true)
.transparentOverlay(true)
.build();
tooltip.show();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
tooltip.dismiss();
}
}, 3000);


Related Topics



Leave a reply



Submit