getWidth() returns 0 if set by android:layout_width="match_parent"

A view's size isn't available until after onMeasure(), particularly if its set to wrap_content, or fill_parent.

You should either access the size in or after onMeasure() in the View code, or in a a Layout Tree Observer:

LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight(); 

    } 
});

You will also need to add android:id="@+id/mylayout" to your LinearLayout for the second one to work.