Setting a variable in the closure scope

I think I understand why variables exist outside of the function they were declared in, because you’re returning another function:

myFunction = function() {
    var closure = 'closure scope'
    return function() {
        return closure;
A = myFunction(); // myFunction returns a function, not a value
B = A(); // A is a function, which when run, returns:
console.log(B); // 'closure scope'

The way that it’s written now, calling A() is like a getter.

Q: How can I write myFunction so that calling A(123) is a setter?


Try the following:

myFunction = function() {
    var closure = 'closure scope'

    // value is optional
    return function(value) {
        // if it will be omitted
        if(arguments.length == 0) {
            // the method is a getter
            return closure;
        } else {
            // otherwise a setter
            closure = value;
            // with fluid interface ;)
            return this;
A = myFunction(); // myFunction returns a function, not a value
A(123); // set value
B = A(); // A is a function, which when run, returns:
console.log(B); // '123'

Android View Parameter for OnClickListener Method

please dont mind if stupid question but please i need to clear my confusion ..

For OnClickListener on a button in android i did put this in main.xml


and in java file i did

  public void clicked(View v)
   //  my code here 

my question is why we need to pass View view when we call clicked method .



Because method is called without getting the Button in onCreate. And in order to access that Button you need to have a view.

i.e View v represents your button view.

If you want to access button , suppose you want to get text on It. How do you get it without getting button by using findViewbyId ? you will get your button like this

Button b=(Button)v;

To get the text

String text=b.getText();

So here we are not getting the button by using findViewById

In general if you want to access the button you need to get that object. But here you’ll get that from the view. Without using findViewById.


Drawing filled polygon with libGDX

I want to draw some (filled) polygons with libGDX. It shoudn’t be filled with a graphic/texture. I have only the vertices of the polygon (closed path) and tried to visualize with meshes but at some point this is not the best solution, I think.

My code for an rectangle is:

private Mesh mesh;

public void create() {
    if (mesh == null) {
        mesh = new Mesh(
            true, 4, 0, 
            new VertexAttribute(Usage.Position, 3, "a_position")
        mesh.setVertices(new float[] { 
            -0.5f, -0.5f, 0
            0.5f, -0.5f, 0,
            -0.5f, 0.5f, 0,
            0.5f, 0.5f, 0 

// ...

public void render() {;
    mesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4);

is there a function or something to draw filled polygons in an easier way?


Here is a libGDX example which draws a 2D concave polygon.

Define class members for PolygonSprite PolygonSpriteBatch

PolygonSprite poly;
PolygonSpriteBatch polyBatch;
Texture textureSolid;

Create instances, 1×1 size texture used with red pixel as workaround. An array of coordinates (x, y) is used for initialization of the polygon.

ctor() {
    textureSolid = makeTextureBox(1, 0xFFFF0000, 0, 0); 
    float a = 100;
    float b = 100;
    PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid),
      new float[] {
        a*0, b*0,
        a*0, b*2,
        a*3, b*2,
        a*3, b*0,
        a*2, b*0,
        a*2, b*1,
        a*1, b*1,
        a*1, b*0,
    poly = new PolygonSprite(polyReg);
    poly.setOrigin(a, b);
    polyBatch = new PolygonSpriteBatch();

Draw and rotate polygon

void draw() {

Insert character at specific point but preserving tags?

Update #2

Okay, more testing ensues. It looks like the code works fine when I use a faux spacer, but the regex eventually fails. Specifically, the following scenarios work:

  1. Select words above or below the a tag
  2. You select only one line either directly above or below the a tag
  3. You select more than one line above/below the a tag
  4. You select more than one line specifically below any a tag

The following scenarios do not work:

  1. You select the line/more lines above the a tag, and then the line/more lines below the a tag

What happens when it “doesn’t work” is that it removes the a tag spacer from the DOM. This is probably a problem with the regex…

Basically, it fails when you select text around the a tag.


I don’t need to wrap each line in a p tag, I can instead use an inline element, such as an a, span, or label tag, with display:inline-block and a height + width to act as a new line element (<br />). This should make it easier to modify the code, as the only part that should have to change is where I get the text in between the bounds. I should only have to change that part, selectedText.textContent, to retrieve the HTML that is also within the bounds instead of just the text.

I am creating a Phonegap that requires the user to select text. I need fine control over the text selection, however, and can no longer plop the entire text content in a pre styled p tag. Instead, I need represent a linebreak with something like <a class="space"></a>, so that the correct words can be highlighted precisely. When my text looks like this:

<p class="text">This is line one

Line two

Line three

And has .text{ white-space:pre-wrap }, the following code allows me to select words, then wrap the text with span elements to show that the text is highlighted:

$("p").on("copy", highlight);

function highlight() {
    var text = window.getSelection().toString();
    var selection = window.getSelection().getRangeAt(0);
    var selectedText = selection.extractContents();
    var span = $("<span class='highlight'>" + selectedText.textContent + "</span>");
    if (selectedText.childNodes[1] != undefined) {
    var txt = $('.text').html();
    $('.text').html(txt.replace(/</span>(?:s)*<span class="highlight">/g, ''));
    $(".output ul").html("");
    $(".text .highlight").each(function () {
        $(".output ul").append("<li>" + $(this).text() + "</li>");

function clearSelection() {
    if (document.selection) {
    } else if (window.getSelection) {

This code works beautifully, but not when each line is separated by a spacer tag. The new text looks like this:

<p class="text">
    Line one
    <a class="space"></a>
    Line two
    <a class="space"></a>
    Line three

When I modify the above code to work with have new lines represented by <a class="space"></a>, the code fails. It only retrieves the text of the selection, and not the HTML (selectedText.textContent). I’m not sure if the regex will also fail with an a element acting as a new line either. The a element could be a span or label, or any normally inline positioned element, to trick iOS into allowing me to select letters instead of block elements.

Is there anyway to change the code to preserve the new line elements?


The desired output should look like this:

If the text “Line one” was highlighted:

<p class="text">
    <span class="highlight">Line one</span>
    <a class="space"></a>
    Line two
    <a class="space"></a>
    Line three

If the text “Line one Line two” was highlighted:

<p class="text">
    <span class="highlight">Line one
    <a class="space"></a>
    Line two</span>
    <a class="space"></a>
    Line three

Of course different parts and individual letters can and will be highlighted as well instead of full lines.


Well, I came up with a solution, rather straightforward as well.

If .text looks like this:

<p class="text">Line one
<a class="space"></a>Line two
<a class="space"></a>Line three</p>

With the exact markup and line breaks as above, then I can find each n and replace it with a spacer element.

if (textStr.indexOf("n") >= 0) {
    textStr = textStr.replace(/n/g, "n<a class='space'></a>");

This isn’t versatile at all, and will fail if there are more than one line breaks, if the tags are different, etc. So, I encourage anyone who has a better method to answer the question! It can’t be that hard, I figured it out.


Setting Roboto font in TextView – xml

I’ve found several posts regarding this topic, but all of this topics either sets the font with setTypeFace() method on a TextView object, or creating a custom class which sets the font to Roboto and extends TextView. As far as I know from API-Level 11(?) or something, we are able to set the TypeFace as a xml attribute, some how. Like this:

      android:text="Hello, world">

What is the right way to do this? Is it possible to have a fallback if the application runs on a device lower than API level 11(?) something like:



Take a look at the RobotoTextView project. Works down to Android 1.5, and you can set the typeface using XML attributes. It also includes other views like RobotoButton, RobotoCheckbox, etc.

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..