Skip to content

Commit c414f08

Browse files
authored
Added support for non-touch screens
Added support for non-touch screens so that they can dismiss tooltips either by pressing the DPAD_CENTER button, or the ENTER button. This is in addition to the BACK button that works by default. This all works only if the tooltip is set to be focusable, so on non-touch devices, we set the tooltip focusable by default.
1 parent 53d0c51 commit c414f08

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/SimpleTooltip.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.animation.ObjectAnimator;
3131
import android.annotation.TargetApi;
3232
import android.content.Context;
33+
import android.content.pm.PackageManager;
3334
import android.graphics.Color;
3435
import android.graphics.PointF;
3536
import android.graphics.RectF;
@@ -38,6 +39,7 @@
3839
import android.os.Build;
3940
import android.util.Log;
4041
import android.view.Gravity;
42+
import android.view.KeyEvent;
4143
import android.view.LayoutInflater;
4244
import android.view.MotionEvent;
4345
import android.view.View;
@@ -316,6 +318,24 @@ private void configContentView() {
316318

317319
mContentLayout = linearLayout;
318320
mContentLayout.setVisibility(View.INVISIBLE);
321+
322+
if (mFocusable)
323+
{
324+
mContentLayout.setFocusableInTouchMode(true);
325+
mContentLayout.setOnKeyListener(new View.OnKeyListener()
326+
{
327+
@Override
328+
public boolean onKey(View v, int keyCode, KeyEvent event)
329+
{
330+
if ((keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) && event.getAction() == KeyEvent.ACTION_UP) {
331+
dismiss();
332+
return true;
333+
}
334+
return false;
335+
}
336+
});
337+
}
338+
319339
mPopupWindow.setContentView(mContentLayout);
320340
}
321341

@@ -579,6 +599,7 @@ public static class Builder {
579599

580600
public Builder(Context context) {
581601
this.context = context;
602+
this.focusable = !(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN));
582603
}
583604

584605
public SimpleTooltip build() throws IllegalArgumentException {
@@ -1034,8 +1055,8 @@ public Builder onShowListener(OnShowListener onShowListener) {
10341055
}
10351056

10361057
/**
1037-
* <div class="pt">Habilita o foco no conteúdo da tooltip. Padrão é <tt>false</tt>.</div>
1038-
* <div class="en">Enables focus in the tooltip content. Default is <tt>false</tt>.</div>
1058+
* <div class="pt">Habilita o foco no conteúdo da tooltip. Padrão é <tt>false</tt> em dispositivos sensíveis ao toque e <tt>true</tt> em dispositivos não sensíveis ao toque.</div>
1059+
* <div class="en">Enables focus in the tooltip content. Default is <tt>false</tt> on touch devices, and <tt>true</tt> on non-touch devices.</div>
10391060
*
10401061
* @param focusable <div class="pt">Pode receber o foco.</div>
10411062
* <div class="en">Can receive focus.</div>

0 commit comments

Comments
 (0)