Categories
discuss

jQuery callback within the remove() function

I need to call a function after a DIV has been removed from the page.

I have tried adding a callback like so, but no luck. Any suggestions?

$(foo).remove( function() {
   stepb();
});

Answer

Try this

$.when($(‘#foo’).remove()).then(stepb()); [Example1][1] and [Example2][2].

$('#foo').remove();
stepb();

​Since the remove method in jQuery is synchronous, stepb() will be invoked after remove() has finished. So, no need to use $.when().then().

Categories
discuss

What is the suggested way of implementing overlays in v2 of the Android Google Maps API?

In the original version of the Android Google Maps API it was very easy to implement an overlay with the following code:

List<Overlay> mapOverlays = mapView.getOverlays();
AlarmOverlay alarmOverlay = new AlarmOverlay();
mapOverlays.add(alarmOverlay);

…it was then possible to override the overlays draw() method and paint vector graphics, override the overlays onTouchEvent() method and give custom controls, etc.

I am at a loss as to how to architect similar custom controls in v2, as the use-case for overlays is not mentioned in the API reference (and markers and polygons are not enough). Does anyone have a suggested way of implementing in v2?

Answer

If you need to place your own image on the surface of the earth, use GroundOverlay. The method addGroundOverlay adds such image. It takes GroundOverlayOptions that allow to specify the image size and location (in lat long terms) and also BitmapDescriptor that, among other options, can use the ordinary Bitmap as the image source. So you can create a new Bitmap, create Canvas around this bitmap and paint there.

Seems a good approach if you need to draw something really complex, something for that polygons and markers are not sufficient. Also, the old code that draws on Canvas probably can be reused.

I have not done enough testing how soon the map will be updated after we update the bitmap. In the worst case, maybe the ground overlay needs to be removed and the new ground overlay added. The bitmap itself probably still can be reused.

Putting additional component on the top may be problematic as it must support zooming, moving around and the map is now even 3D.

Categories
discuss

How to set custom button state background color?

I have a button which contains a drawable and text. I want the background of the button to be different than the normal one provided (preferably a plain color). This works fine, I simply use the android:background attribute in the XML file and assign the color accordingly. However, I want the the background to change to a different color when selected or focused (state selector).

I attempted to create a selector in a drawable folder with the defined colors (which works well when working with the text of a button), like so:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:state_pressed="false" android:color="@color/green" />
        <item android:state_focused="true" android:state_pressed="true" android:color="@color/green" />
        <item android:state_focused="false" android:state_pressed="true" android:color="@color/green" />
        <item android:color="@color/white" />
    </selector> 

and set this xml as the android:background attribute, like so:

android:background="@drawable/button_state"

but this causes a force close stating:

 Caused by: android.content.res.Resources$NotFoundException: File res/drawable/button_state.xml from drawable resource ID #0x7f020070

but the resource is there. Can you not customize the background state? If you can, how? or what am I doing wrong? Thanks for the help!

Answer

The xml you posted is suitable for a color state list, not a state list drawable. Try this instead:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" >
        <shape><solid android:color="@color/green"/></shape>
    </item>
    . . .
</selector>

Alternatively, put your existing file into res/color and use it as you would any other color. However, I don’t remember if you can use a color state list directly as a background for a view.

Categories
discuss

Add Remove Class to a DOM element using only JavaScript, and best of these 2 ways

What is a good approach to add class to a DOM element using JavaScript. And Remove also.

I came across this following codes for adding:

1:

Element.prototype.addClassName = function (cls) {
    if (!this.hasClassName(cls)) {
        this.className = [this.className, cls].join(" ");
    }
};

2:

document.querySelector(element).classList.add(cls)

Both of them seem to work for me. What is the difference between them and which is the best?

Answer

1. If you are moved by the word prototype, you might want to check MDN Docs – Inheritance and prototype chain.

2. The first code you mentioned is a normal cross-browser way of adding a class to an element.
Instead of being a function declaration, its added as a method to the Element‘s prototype – so that when you query an Element by its id (good ol’ JavaScript), you can simply call the method on the element itself.

3. The second code you have posted is per the new DOM Specification. W3 Link. It will work only in those browsers that implement the DOM Level 4 Specs. It won’t work in old browsers.

That goes the difference.

For the best method, a native method is always the best and fastest.
So for the browsers that support clasList, the second should be the best. Per my opinion though, till things (drafts) are not finalized you might want to consider a method that works cross-browser and is compatible with both JavaScript (ECMA-3) and supported DOM Spec.

A simple idea should be to change the className, a property accessible in all new and old browsers, and append your class as a string to it:

var el = document.getElementById(id);
el.className = el.className + " " + cls; 
// mind the space while concatening

Of course you can add validation methods like using regex for trimming spaces while adding and removing.

By the way, I got the full part of the code you posted as the 1st Example:

Element.prototype.hasClassName = function(name) {
    return new RegExp("(?:^|\s+)" + name + "(?:\s+|$)").test(this.className);
};

Element.prototype.addClassName = function(name) {
    if (!this.hasClassName(name)) {
        this.className = this.className ? [this.className, name].join(' ') : name;
    }
};

Element.prototype.removeClassName = function(name) {
    if (this.hasClassName(name)) {
        var c = this.className;
        this.className = c.replace(new RegExp("(?:^|\s+)" + name + "(?:\s+|$)", "g"), "");
    }
};
Categories
discuss

Java equivalent for .charCodeAt()

In JavaScript, .charCodeAt() returns a Unicode value at a certain point in the string which you pass to a function. If I only had one character, I could use the code below to get the Unicode value in Java.

public int charCodeAt(char c) {
     int x;
     return x = (int) c;
}

If I had a string in Java, how would I get the Unicode value of one individual character within the string, like the .charCodeAt() function does for JavaScript?

Answer

Java has the same method: Character.codePointAt(CharSequence seq, int index);

String str = "Hello World";
int codePointAt0 = Character.codePointAt(str, 0);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..