Checkbox onChange() event triggered twice only on Android(Gingerbread 2.3) - android

When the selectAll checkbox is tapped, the onChange() event is triggered twice.
This happens only on Android devices with Gingerbread(2.3). On other devices and desktop browsers the event is triggered only once.
Here is the script which I'm using.
/* Check-Uncheck */
self.elements.listview.on('change', ':checkbox', function (event) {
var unchecked = $(':checkbox:not(:checked)', self.elements.listview);
self.elements.selectAll
.prop('checked', unchecked.length === 0)
.checkboxradio("refresh");
});
/* Check-Uncheck 'Select All' */
self.elements.selectAll.change(function () {
var checkboxes = $(':checkbox', self.elements.listview);
var checked = self.elements.selectAll.is(':checked');
checkboxes
.prop('checked', checked)
.checkboxradio("refresh");
});
Please let me know if anyone has faced this particular issue, or have any suggestions.
Thanks

well, i can't determine the source of the problem...maybe the onclick handler doesn't get overwritten...but an easy workaround would be a lock variable...
var lockCheck = false;
//in onchange code of both functions:
if(lockCheck){
lockCheck = false;
return;
}
lockCheck = true;
[edit] ...well...or Omar's suggestion :)

Related

How to prevent on click event to fire twice

I'm trying to manage events in firetv app, the problem is on event fires Enter key and Click key at the same time, how can I prevent that?
Tried to use inEvent.stopImmediatePropagation(); but it still does not help.
$(document).on('click keydown', '.currentSlide', function (inEvent) {
var keycode;
var logger = $('#error-logger');
if(window.event) {
keycode = inEvent.keyCode;
} else if(e.which) {
keycode = inEvent.which;
} if (inEvent.type === 'click') {
keycode = 1;
}
console.log(`Keycode: ${keycode}`);
});
Clicking the remote button triggers the event twice and I'm trying to get it to only do it once.
Solved my problem by adding 1 more var that I set to true on keydown and on keyup to false again.
I'm not sure if there is better way of doing it but this will do for now.
Write the following as the first line in your function before you do anything
inEvent.stoppropagation()
this will stop any further events from triggering.

Getting "Link text" of the link displayed in WebView

I have Android WebView which displays some links as: Link1TextLink2Text Now I would like to retrieve Link1Text and Link2Text when I long press these links. I have contextMenu implemented in the code and I could successfully get the link urls (http://link1.html, http://link2.html) using HitTestResult getExtra() method but how ccan I get those link texts ?FYI, I require those link texts for implementing "Copy link text" option in the contextMenu.
To get the text of an achor link:
I. Hook a touchstart listener to every web pages in the onPageFinished() callback of WebViewClient via evaluateJavascript. like:
//Javascripts to evaluate in onPageFinished
const w=window;
w.addEventListener('touchstart',wrappedOnDownFunc);
function wrappedOnDownFunc(e){
if(e.touches.length==1){
w._touchtarget = e.touches[0].target;
}
console.log('hey touched something ' +w._touchtarget);
}
note we've saved the touch target.
II. Then implement OnLongClicklisenter for webview. use evaluateJavascript again when you long pressed on a link object:
#Override
public boolean onLongClick(View v) {
WebView.HitTestResult result = ((WebView)v).getHitTestResult();
if (null == result) return false;
int type = result.getType();
switch (type) {
case WebView.HitTestResult.SRC_ANCHOR_TYPE:
if(result.getExtra()!=null){
((WebView)v).evaluateJavascript("window._touchtarget?window._touchtarget.innerText:''", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
System.out.println("hey received link text : "+value);
}
});
}
return true;
}
return false;
}
What's more, we can even choose to select the text of the anchor element! Actually this is one of the options that samsung browser offers when you long-pressed an tag .
To achieve this, we still need that recorded touch target. Besides we need 2 new javascript methods:
function selectTouchtarget(){
var tt = w._touchtarget;
if(tt){
w._touchtarget_href = tt.getAttribute("href");
tt.removeAttribute("href");
var sel = w.getSelection();
var range = document.createRange();
range.selectNodeContents(tt);
sel.removeAllRanges();
sel.addRange(range);
}
}
function restoreTouchtarget(){
var tt = w._touchtarget;
if(tt){
tt.setAttribute("href", w._touchtarget_href);
}
}
Finnaly in the onLongClick listener, instead of just fetch the innerText, we programmatically set the selection, trigger the action menu bar, and restore the removed href attribute of our touch target.
case WebViewmy.HitTestResult.SRC_ANCHOR_TYPE:
if(result.getExtra()!=null){
WebViewmy mWebView = ((WebViewmy)v);
mWebView.evaluateJavascript("selectTouchtarget()", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
/* bring in action mode by a fake click on the programmatically selected text. */
MotionEvent te = MotionEvent.obtain(0,0,KeyEvent.ACTION_DOWN,mWebView.lastX,mWebView.lastY,0);
mWebView.dispatchTouchEvent(te);
te.setAction(KeyEvent.ACTION_UP);
mWebView.dispatchTouchEvent(te);
te.recycle();
//if it's not delayed for a while or the href attribute is not removed, then the above code would click into
// the anchor element instead of select it's text.
/* restore href attribute */
mWebView.postDelayed(() -> mWebView.evaluateJavascript("restoreTouchtarget()", null), 100);
}
});
}
return true;
In my case, I've extended the WebView as WebViewmy to record last touched positions, lastX and lastY, in the onTouchEvent method.
Unfortunately, a clear, official way to do this is not available. Although, there are two APIs (selectText and copySelection) which are pending API council approval, that may help to do this, but they are not available at the moment.

Does EditText.getText().getLength() return 0 when it contains a hint?

I have some code, but it is not working as expected. My logic doesn't seem to be faulty, so I think it is an implementation error. My code:
public boolean[] party_check(){
Date date_ET = new Date(party_dateET.getYear(), party_dateET.getMonth(), party_dateET.getDayOfMonth());///Date is deprecated,
///but easy to handle
///this is used to test the date contained in the datepicker, party_dateET. If it is before the day today, then it will not write,
///and a message is displayed
boolean[] return_array = new boolean[4];
///EditText party_titleET;
///EditText party_timeET;
///EditText party_dateET;
///EditText party_venueET;
return_array[0] = true;
if(party_titleET.getText().length() == 0){
return_array[1] = false;
return_array[0] = false;
}else{
return_array[1] = true;
}
if(date_ET.before(Calendar.getInstance().getTime()) == true){
return_array[2] = false;
return_array[0] = false;
///tests that the date entered is not invalid, ie. is in the future.
///not test for time. I could say that if time == 00:00, then don't run, or use a listener to check if it has changed,
///using a boolean value. But this would not really benefit the task much, so I haven't. It would also
///take more coding, and more complex coding.
}else{
return_array[2] = true;
}
if(party_venueET.getText().length() == 0){
return_array[3] = false;
return_array[0] = false;
}else{
return_array[3] = true;
}
return return_array;
///return_array[0] - 1st value in array - is used as toggle as to whether the party is ready or not.
///return_array[1-4] are used to indicate to the user which textbox is empty/ has a problem.
}
However it does not do what I expect it to do when I return the boolean array. This is used for testing whether the user has entered a text value into the EditText's.
However it does not work as expected. Is my logic faulty, or I have implemented it wrong? Thanks for the help!!
The hint does not get returned, when you use getText(). You can use getHint() though.
Text and Hint are different properties.
So the Text will be empty, if the hint is shown.
Let's say that the hint is a "dress" for the text (not to let it "naked"), but it's not the text itself.

AIR/as3 stage keylistener overriding input textfield

I'm building a mobile AIR app (Android & IOS) with Adobe Flash Builder 4.6 and I'm having this annoying problem.
Because I want to 'catch' the back-key on Android devices I added the following code to my main class:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
private function keyDown(k:KeyboardEvent):void {
if(k.keyCode == Keyboard.BACK) {
backClicked(); // function handling the back-action, not important
k.preventDefault();
}
Now somewhere else - nested in some classes - I've got a textfield:
TF = new TextField();
TF.type = TextFieldType.INPUT;
But when I set focus on the textfield the soft keyboard does appear, but I can't type a single character. When I disable the keylistener: no problem.
Seems like the listener is overriding my input field. Is there any workaround on this?
I have also implemented the back button functionality for my mobile apps , but i used to register keydown event only when my particular view is activated and removed the registered when view get deactivated.
in <s:view ....... viewActivate ="enableHardwareKeyListeners(event)" viewDeactivate="destroyHardwareKeyListeners(event)">
// add listener only for android device
if (Check for android device) {
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown, false, 0);
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp, false, 0);
this.setFocus();
}
private function destroyHardwareKeyListeners(event:ViewNavigatorEvent):void
{
if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_DOWN))
NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown);
if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_UP))
NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp);
}
private function handleHardwareKeysDown(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK) {
e.preventDefault();
// your code
} else {
}
}
private function handleHardwareKeysUp(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK)
e.preventDefault();
}
May this can help you.

Android: Disable text selection in a webview

I am using a webview to present some formatted stuff in my app. For some interaction (which are specific to certain dom elements) I use javascript and WebView.addJavascriptInterface(). Now, I want to recognize a long touch. Unfortunately, onLongTouch, in Android 2.3 the handles for text selection are displayed.
How can I turn off this text selection without setting the onTouchListener and return true? (Then, the interaction with the "website" doesn't work anymore.
This worked for me
mWebView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
mWebView.setLongClickable(false);
I have not tested, if you don't want the vibration caused by the long click, you can try this:
mWebView.setHapticFeedbackEnabled(false);
Setting webkit css property -webkit-user-select to none would solve the problem.
Example CSS to disable selection:
* {
-webkit-user-select: none;
}
I figured it out!! This is how you can implement your own longtouchlistener. In the function longTouch you can make a call to your javascript interface.
var touching = null;
$('selector').each(function() {
this.addEventListener("touchstart", function(e) {
e.preventDefault();
touching = window.setTimeout(longTouch, 500, true);
}, false);
this.addEventListener("touchend", function(e) {
e.preventDefault();
window.clearTimeout(touching);
}, false);
});
function longTouch(e) {
// do something!
}
This works.
It appears that cut/paste via long press is turned off if you used
articleView.setWebChromeClient(new WebChromeClient(){...})
See https://bugzilla.wikimedia.org/show_bug.cgi?id=31484
So if you are using setChromeClient and you WANT to have long click to start copy/paste, the do the following:
webView.setWebChromeClient(new WebChromeClient(){
[.... other overrides....]
// #Override
// https://bugzilla.wikimedia.org/show_bug.cgi?id=31484
// If you DO NOT want to start selection by long click,
// the remove this function
// (All this is undocumented stuff...)
public void onSelectionStart(WebView view) {
// By default we cancel the selection again, thus disabling
// text selection unless the chrome client supports it.
// view.notifySelectDialogDismissed();
}
});
An alternative solution is to subclass WebView and Override performLongClick as bellow:
public class AdvanceWebView extends WebView {
//Add constructors...
#Override
public boolean performLongClick() {
return true;
}
}
It seems that the only option is to set onTouchListener and write your own code to detect long-click. Then return true if it's a long-click and false otherwise.
For kotlin i found the following to work:
webView.isLongClickable = false

Categories

Resources