How to detect whether a browser has tap highlighting? - android

How can I detect whether a browser has tap highlighting? I could just scan the user agent string for "iphone", "ipad", and "android" and hope to cover most touch screen devices, but that seems rather crude. Do you know of a way to tell reliably? Or any other ideas?
I want to disable my CSS :hover effects if the browser has tap highlighting (having both at the same time is quite disconcerting). In my case that's much preferable to disabling the tap highlighting.
Thanks for your time and I'd appreciate any ideas you might have!

You can use the following code snippet to detect touch screen devices:
function is_touch_device() {
try {
document.createEvent("TouchEvent");
return true;
} catch (e) {
return false;
}
}

Related

Angular event.target.blur() not working for mobile

I have the following code to prevent the buttons to stay focused after they are clicked. It works perfectly for desktop but it doesn't work at all when testing on mobile devices (Both iOS & Android), I'm not sure if I'm missing something here (I already tried replacing click with touchstart and touchend).
this.renderer.listen('document', 'click', (event) => {
if (event.target.nodeName === 'BUTTON') {
event.target.blur();
} else if (event.target.parentNode.nodeName === 'BUTTON') {
event.target.parentNode.blur();
}
});
Ok so I figured it out, in case anyone ever comes across this situation:
It WAS actually working, but on mobile devices an "emulated" hover is also applied after pressing buttons, so what I was seeing was the hover state, not the focus one.
I fixed it by wrapping the hover style of my button inside this block, to make sure that the device supports ACTUAL hover (e.g. using a mouse):
#media (hover: hover) {
your-element:hover {
//hover style
}
}

How to disable android' touchscreen device?

The App currently runs in IMMERSIVE_STICKY mode, but when user swipes from the side - OS shows menu and home/back buttons. So user can turn my app off or run some other stuff which is unacceptable. Therefore, I need to disable touchscreen device completely on android to prevent any taps and swipes.
If i cant disable it via official API, can i disable touchpad using console? Android will be rooted.
I found that my touchpad device is /sys/devices/virtual/input/input1 but still cant find where can I disable it. /power/control takes only 'on' or 'auto'.
I found other solution with xinput but on android there is no one.
I think you can override the function onTouchEvent.
private boolean touch_disabled=true;
#Override
public boolean onTouchEvent(MotionEvent e) {
if (touch_disabled){
return true;
}else {
//do something here if your touch screen is activated
}
}
public disable_touch(boolean b) {
touch_disabled=b; //function to activate or desactivate touch screen
}

Hammer.js taps are slow on Android?

Situation: hammer.js 2.0.4, jQuery 2.1 on a Cordova cross-platform mobile app. I was running into well-documented (for example) issues with delay of click events, so I thought I'd try hammer.js for this. It works beautifully on my iPad, but on my Android phone (Android v4.4) is dreadful: very slow to respond, and frequently misses taps entirely.
I implemented my own small tap detection (using mouseUp events) and it performs much better than Hammer.js on my Android (but terribly on my iPad).
So my question is: are there known issues for hammer.js on Android, or known workarounds? I'd really prefer not to conditionally use two different approaches based on platform, especially when there is no conceivable way for me to test all possible mobile platforms.
Example of the hammer.js tap code; nothing very interesting going on:
$(".menuitem").each( function(i, elem) {
var mc = new Hammer.Manager(elem);
mc.add(new Hammer.Tap());
mc.on("tap", action);
});
In addition there is a top-level swipe recognizer that covers the entire page:
var swipelistener = new Hammer($("body")[0], {
recognizers: [[Hammer.Swipe,{ direction: Hammer.DIRECTION_RIGHT }]]
});
swipelistener.on("swipe", swipeRight );
In total there will be fewer than two dozen elements responding to tap events, and no overlapping or nested elements. I thought it might have something to do with the swipe recognizer overlapping the tap recognizers, but removing the swipe listener didn't change the tap behavior at all.
You need to play with the settings of each recognizer.
hammertime.get('swipe').set({
direction: hammer.DIRECTION_ALL, threshold: 1, velocity: 0.1
});
This worked for me for swipe on 4.1.1
Would be really helpful if someone could write some example code for tap as I'm still fiddling with that.
Also, you don't need mc.add as the Manager by default has all the recognizers. You only need to use .add once you've manually removed (using mc.remove) one.
If you are unsure what settings any of the recognizers have, look on their website eg http://hammerjs.github.io/recognizer-swipe/ shows that I could set direction, threshold and velocity etc as per the code above.
As I can see you need to detect swipe on entire screen without any specific options. Maybe cordova-android-gestures (only for Android) helps you? This plugin "catches" gestures on total device surface. So, for detect swipes:
//check the platform
if (device.platform=="Android") {
MegaduckGestures.swiper(function(direction){
switch (direction) {
case 'rightSwipe':
//do your staff
break;
case 'leftSwipe':
//do your staff
break;
default: break;
}
});
}
else {
//use your iPad approach
}
And for handling tap on menu item:
$(".menuitem").each( function(i, elem) {
//check the platform
if (device.platform=="Android") {
MegaduckGestures.swiper(function(direction){
if (direction=='singleTap') {
//do your staff
}
});
}
else {
//use your iPad approach
}
});

Touchmove & vmousover not working in a Cordova/PhoneGap Android app

I've tried both techniques in this answer to get a "dragging touch highlight" across elements in my PhoneGap App (testing on Android).
Here's my JSFiddle of the touchmove approach
$("td").bind("touchmove", function(evt){
var touch = evt.originalEvent.touches[0]
highlightHoveredObject(touch.clientX, touch.clientY);
});
Here's my JSFiddle of the vmousemove approach
$("#main").bind("vmousemove", function(evt){
$('.catch').each(function(index) {
if ( div_overlap($(this), evt.pageX, evt.pageY) ) {
$('.catch').not('eq('+index+')').removeClass('green');
if (!$(this).hasClass('green')) {
$(this).addClass('green');
}
}
});
});
Both work perfectly when emulating the app from desktop browser. Both work when viewing the JSFiddles from my Android tablet browser. But in the installed app on the tablet, it doesn't work. Instead of an updating highlight as I drag across the elements, all I get is a highlight on the first-touched event. The same for both methods.
Any ideas what's going on?
A comment on this question has an intriguing suggestion that "If you are running on android you also need to cancel the touchmove event to get new ones while touching. Don't ask me why...". Does that ring a bell, and if so, how would I "cancel the touchmove event to get new ones" with either of these approaches?
Alternately, has anyone successfully done a "dragging highlight" effect on a PhoneGap app, and would you care to share your technique?

prevent css/jquery popups from allowing links underneath to be clicked (mobile only)

so i have created !a simple popup using css and jquery. the problem is, when the popup is activated, links underneath the popup can still be clicked. is there any way to prevent this from happening. the click box for some of the links in the popup are small and one can easily click next to it which means the link underneath the popup is clicked.
the site i am working on: taxslayerplayer.com, look at it on an android and you will see what i mean. also, i have experienced this problem on many other websites while browsing on my phone.
any pointers would be appreciated, thanks!
I'm not sure about a strictly-mobile solution, but you could check for the pop-up being visible and, if it's visible, simply return false in the click-handler for links:
$('a').filter(
function(){
return !$(this).closest(popupSelector).length;
}).on('click', function(e){
if ($(popupSelector).is(':visible')) {
return false;
}
else {
// do whatever you'd normally do with the links
}
});
Alternatively, you could instead use a variable, for example popupIsShown, set it to false initially (on DOMReady), and then set it to true when the popup is shown, and reset to false when it's re-hidden, making the if check a little less expensive:
$('a').filter(
function(){
return !$(this).closest(popupSelector).length;
}).on('click', function(e){
if (popupIsShown) {
return false;
}
else {
// do whatever you'd normally do with the links
}
});
Use a boolean value and set it to false in case of pop-ups. It works!
PS : Just checked.. David has already answered it.

Categories

Resources