title: rendering a TextView in a Bitmap for an android widget
body: <p>I'm building a widget which displays some text. By widget I mean the kind which lies on the desktop.</p>
  
  <p>The problem is that I want to change text's font at runtime. There is several textview I would like, at runtime, to set the first as bold, the second blue and italic for example, etc.</p>
  
  <p>I came up with this : </p>
  
  <pre><code>TextView tv = new TextView(context);
  tv.setText(stringToDisplay);
  tv.setTextColor(0xa00050ff);  // example
  tv.setTextSize(30);           // example
  
  Bitmap b = loadBitmapFromView(tv);
  updateViews.setImageViewBitmap(R.id.id_of_the_imageview, b);
  </code></pre>
  
  <p>with</p>
  
  <pre><code>private static Bitmap loadBitmapFromView(View v)
  {
   Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height,      Bitmap.Config.ARGB_8888);
  Canvas c = new Canvas(b);
  v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
  v.draw(c);
  return b;
  }
  </code></pre>
  
  <p>but it wont work (NullPointerException on first line of loadBitmap), until I replace <code>
  v.getLayoutParams().width, v.getLayoutParams().height</code> by fixed sizes like 250, 50</p>
  
  <pre><code>Bitmap b = Bitmap.createBitmap(250, 50, Bitmap.Config.ARGB_8888);
  // ...
  v.layout(0, 0, 250, 50);
  </code></pre>
  
  <p>But that's not a good solution ...</p>
  
  <p>so I tried this : </p>
  
  <pre><code>  LayoutInflater li = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = li.inflate(R.layout.widget_text, null);
  
    TextView tv = (TextView) row.findViewById(R.id.id_of_the_textview);
  </code></pre>
  
  <p><code>widget_text</code> being a layout similar to the displayed one but with TextViews instead of ImageViews, in the hope to get some size information out of it ..</p>
  
  <p>but it's not working and I get this exception :</p>
  
  <pre><code>01-02 17:35:06.001: ERROR/AndroidRuntime(11025): Caused by: java.lang.IllegalArgumentException: width and height must be &gt; 0
  </code></pre>
  
  <p>on the call to <code>Bitmap.createBitmap()</code></p>
  
  <p>so, someone could point me in the right direction?</p>