Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android image button tooltip

I'm trying to build an app with image buttons that work like the action bar but i can't get them to show a tooltip on long press.

   <ImageButton
        android:id="@+id/editUrgent"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="48dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/editImportant"
        android:hint="@string/hint_urgent"
        android:contentDescription="@string/hint_urgent"
        android:text="@string/hint_urgent"
        android:src="@drawable/clock_light" />

android:contentDescription works for hovering(s-pen) but long press still does nothing.

like image 817
user3536471 Avatar asked Apr 15 '14 15:04

user3536471


4 Answers

With api level 26 you can use the built-in TooltipText: Via XML:

android:toolTipText="yourText" 

ViewCompatDocs

If your minSdk is below 26, use ToolTipCompat, for example:

TooltipCompat.setTooltipText(yourView, "your String"); 

Unfortunately, there's is no way to do this in your XML-File.

like image 141
AsterixR Avatar answered Sep 22 '22 16:09

AsterixR


This is the code that Support Library v7 uses to show "cheat sheet"s for action menu items:

public boolean onLongClick(View v) {     if (hasText()) {         // Don't show the cheat sheet for items that already show text.         return false;     }      final int[] screenPos = new int[2];     final Rect displayFrame = new Rect();     getLocationOnScreen(screenPos);     getWindowVisibleDisplayFrame(displayFrame);      final Context context = getContext();     final int width = getWidth();     final int height = getHeight();     final int midy = screenPos[1] + height / 2;     int referenceX = screenPos[0] + width / 2;     if (ViewCompat.getLayoutDirection(v) == ViewCompat.LAYOUT_DIRECTION_LTR) {         final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;         referenceX = screenWidth - referenceX; // mirror     }     Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);     if (midy < displayFrame.height()) {         // Show along the top; follow action buttons         cheatSheet.setGravity(Gravity.TOP | GravityCompat.END, referenceX, height);     } else {         // Show along the bottom center         cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);     }     cheatSheet.show();     return true; } 
like image 31
Mansour Avatar answered Sep 23 '22 16:09

Mansour


Solution that works in xml and supports minSdk less than 26

@BindingAdapter("tooltipTextCompat")
fun bindTooltipText(view: View, tooltipText: String) {
    TooltipCompat.setTooltipText(view, tooltipText)
}

and usage in layout.xml

app:tooltipTextCompat="@{@string/add_for_all_tooltip}"
like image 37
krishnakumarp Avatar answered Sep 21 '22 16:09

krishnakumarp


Not exactly what you are looking for but it does something very similar.

1) Insure your view is android:longClickable="true" (should be by default) and has a defined content description android:contentDescription="@string/myText":

<ImageButton android:id="@+id/button_edit"
     android:contentDescription="@string/myText"
     android:src="@drawable/ic_action_edit"
     android:onClick="onEdit"
     android:longClickable="true"/>

2) Register a callback to be invoked when the view is long pressed. Handler will display the content description as a toast message.

findViewById(R.id.button_edit).setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(context,view.getContentDescription(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
like image 35
Pascal Chardon Avatar answered Sep 21 '22 16:09

Pascal Chardon