Categories
discuss

Backbone view DOM element removed

I keep reading about the Backbone.js zombie (or memory leak) problem. Basically you have to unbind and remove the element from the DOM when you no longer need it to make sure all events are removed as well.

Now, I have a single page app with a few containers:

<div id="page1"></div>
<div id="page2"></div>

and add my underscore.js templates to these placeholders. I have a model per page like:

HomeView = Backbone.View.extend({
  el: '#page1'
)}

Now, when I click on an element on that page I navigate to another Backbone.js view:

clicked: function(ev){
  $(this.el).remove(); // <-- this is the problem
  $(this.el).unbind();
  App.navigate('page/2', true);
}

This works fine but… I removed the page1 element from the DOM so when I use the back button to go to the previous page my element is gone and there is nothing to attach the HTML to.

I probably don’t understand how to link Backbone.js views with the DOM… should I keep the element with the risk of memory leaks?

Thanks!

Answer

As the article says, (yes, I’ve tried his methods before in my own projects), you have to find a way to remove your view’s DOM element and unbind the events. There are, however, 2 types of events, 1) Backbone events, 2) the events that are bound to your DOM elements with jQuery.

So instead of your:

$(this.el).remove();
$(this.el).unbind();

Do this:

this.remove();
this.unbind();

You are now removing Backbone events as well; and the this.remove on a view will call $(this.el).remove();.

However, that is only how to remove a view and not leave zombies. You should consider his methods for showing a view to make this process more automatic. This is all in his article.

Categories
discuss

Create a radial gradient programmatically

Im trying to reproduce the following gradient programmatically.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="@color/startcolor" 
        android:centerColor="#343434"
        android:endColor="#00000000"
        android:type="radial"
        android:gradientRadius="140"
        android:centerY="45%"
     />
    <corners android:radius="0dp" />
</shape>

How can I set programmatically the paramether? Thanks

        android:centerY="45%"

Answer

http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

To set that specific parameter (I’m assuming a centerX value as you haven’t specified one):

yourGradientDrawable.setGradientCenter(1.0f,  0.45f);

So to create the above gradient (except with different colors) programatically:

GradientDrawable g = new GradientDrawable(Orientation.TL_BR, new int[] { getResources().getColor(R.color.startcolor), Color.rgb(255, 0, 0), Color.BLUE });
g.setGradientType(GradientDrawable.RADIAL_GRADIENT);
g.setGradientRadius(140.0f);
g.setGradientCenter(0.0f, 0.45f);

Note: The orientation is ignored for a radial gradient but is needed for the constructor that takes colors.

Categories
discuss

Android nine-patch not being recognized on device

I just made a nine-patch

it is saved like border.9.png from the draw 9 patch tool.

in my android layout editor, it shows up like it is going to be a proper 9 patch graphic, stretched out yet not pixelated.

yet on a real device, it just shows a giant stretched image, with the 9patch guides seen, it looks really bad

whats going on??? and how do I fix it

my xml:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border"
        android:orientation="vertical"
        android:padding="35dip" >

Answer

You can find all the necessary information how to create a 9.patch here

Your background should be like this

nine patch background

Note: the markers to the bottom and right need to be drawn as complete lines, not single dots.

Edit
Seems that uploading the 9.patch here becomes altered, and cannot be used as a 9.patch anymore.
You can download the functional version from here

Categories
discuss

java.lang.Class and equality

According the javadoc of Class

Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.

But when I run the below

int[] intArray = { 1, 2 };
out.println(intArray.getClass().hashCode());
int[] int2Array = { 1, 2 };
out.println(int2Array.getClass().hashCode());

out.println(intArray.equals(int2Array));

I get the below output

1641745
1641745
false

I am wondering why the equals is returning false even though both the arrays are of int type and have the same dimensions.

Answer

This is because you’re calling equals() on the array instances themselves instead of their Class object. Try:

out.println(intArray.getClass().equals(int2Array.getClass())); //prints true

You could also write:

out.println(int[].class.equals(int[].class)); //prints true thankfully

As an aside, matching hash codes don’t necessarily indicate equality, though that doesn’t matter here.

Categories
discuss

Dynamically resize font of textview

Is it possible to dynamically re-size fonts in a textview according to screen resolution? If yes how? I’m developing from an mdpi avd. But when the app is installed on hdpi text appears too small.

Answer

Use textSize and the scaled pixels sp unit is what alextsc is implying.

If you realy want to be dynmic and make you font as big as possible to fill up the width then it is possible with a textWatcher to render the text and check the size and then tweak the fonts on the fly.

The following is rather specific as I have multiple text views in a linear layout and this only resizes the text smaller once the text in one of them will not fit. It will give you something to work with though.

class LineTextWatcher implements TextWatcher {

static final String TAG = "IpBike";
TextView mTV;
Paint mPaint;

public LineTextWatcher(TextView text) {
    mTV = text;
    mPaint = new Paint();
}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
}

public void afterTextChanged(Editable s) {
    // do the work here.
    // we are looking for the text not fitting.
    ViewParent vp = mTV.getParent();
    if ((vp != null) && (vp instanceof LinearLayout)) {
        LinearLayout parent = (LinearLayout) vp;
        if (parent.getVisibility() == View.VISIBLE) {
            mPaint.setTextSize(mTV.getTextSize());
            final float size = mPaint.measureText(s.toString());
            if ((int) size > mTV.getWidth()) {
                float ts = mTV.getTextSize();
                Log.w(TAG, "Text ellipsized TextSize was: " + ts);
                for (int i = 0; i < parent.getChildCount(); i++) {
                    View child = parent.getChildAt(i);
                    if ((child != null) && (child instanceof TextView)) {
                        TextView tv = (TextView) child;
                        // first off we want to keep the verticle
                        // height.
                        tv.setHeight(tv.getHeight()); // freeze the
                                                      // height.

                        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                                tv.getTextSize() - 1);
                    } else {
                        Log.v(TAG, "afterTextChanged Child not textView");
                    }
                }
            }
        }
    } else {
        Log.v(TAG, "afterTextChanged parent not LinearLayout");
    }
}
}
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..