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



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:


Do this:


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.


Create a radial gradient programmatically

Im trying to reproduce the following gradient programmatically.

<shape xmlns:android="">
    <corners android:radius="0dp" />

How can I set programmatically the paramether? Thanks



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.setGradientCenter(0.0f, 0.45f);

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


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:

        android:padding="35dip" >


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.

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


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 };
int[] int2Array = { 1, 2 };


I get the below output


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


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.


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.


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