Skip to content

Commit 5460704

Browse files
Merge pull request #108 from lionscribe/patch-2
Added support for non-touch screens
2 parents 27a3269 + 194177e commit 5460704

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

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

Lines changed: 36 additions & 4 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;
@@ -206,10 +208,14 @@ public void show() {
206208
mRootView.post(new Runnable() {
207209
@Override
208210
public void run() {
209-
if (mRootView.isShown())
211+
if (mRootView.isShown()) {
210212
mPopupWindow.showAtLocation(mRootView, Gravity.NO_GRAVITY, mRootView.getWidth(), mRootView.getHeight());
211-
else
213+
if (mFocusable)
214+
mContentLayout.requestFocus();
215+
}
216+
else {
212217
Log.e(TAG, "Tooltip cannot be shown, root view is invalid or has been closed.");
218+
}
213219
}
214220
});
215221
}
@@ -316,6 +322,31 @@ private void configContentView() {
316322

317323
mContentLayout = linearLayout;
318324
mContentLayout.setVisibility(View.INVISIBLE);
325+
326+
if (mFocusable)
327+
{
328+
mContentLayout.setFocusableInTouchMode(true);
329+
mContentLayout.setOnKeyListener(new View.OnKeyListener()
330+
{
331+
@Override
332+
public boolean onKey(View v, int keyCode, KeyEvent event)
333+
{
334+
if (event.getAction() == KeyEvent.ACTION_UP) {
335+
switch (keyCode) {
336+
// The following are taken from the KeyEvent.isConfirmKey() function
337+
case KeyEvent.KEYCODE_DPAD_CENTER:
338+
case KeyEvent.KEYCODE_ENTER:
339+
case KeyEvent.KEYCODE_SPACE:
340+
case KeyEvent.KEYCODE_NUMPAD_ENTER:
341+
dismiss();
342+
return true;
343+
}
344+
}
345+
return false;
346+
}
347+
});
348+
}
349+
319350
mPopupWindow.setContentView(mContentLayout);
320351
}
321352

@@ -579,6 +610,7 @@ public static class Builder {
579610

580611
public Builder(Context context) {
581612
this.context = context;
613+
this.focusable = !(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN));
582614
}
583615

584616
public SimpleTooltip build() throws IllegalArgumentException {
@@ -1034,8 +1066,8 @@ public Builder onShowListener(OnShowListener onShowListener) {
10341066
}
10351067

10361068
/**
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>
1069+
* <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>
1070+
* <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>
10391071
*
10401072
* @param focusable <div class="pt">Pode receber o foco.</div>
10411073
* <div class="en">Can receive focus.</div>

0 commit comments

Comments
 (0)