Override layout_height/layout_width for Custom Views


        The default inflater of android for inflating views from layout resource expects both layout_width and layout_height attribute to be specified in the layout manifest. Any attempt to skip these attributes for custom views (having layout params in code) causes a run time exception, "You must supply a layout_width attribute" and with the current version (Jellybean) of layout inflater, custom views will have to override the layout parameters (specified in the layout xml). There are different solutions like updating the layout parameters in View's onMeasure, the only downside being that the onMesaure is invoked more often that what is needed for this purpose. The alternative is to override, setLayoutParams, which is invoked once by the layout inflater.


   @override
   public void setLayoutParams(LayoutParams params) {

      params.height = ...;
      params.width  = ...;

      super.setLayoutParams(params);

   }