title: Using Layout resources in a LiveWallpaper on Android
body: <p>When you create a LiveWallpaper in Android 2.2+ you get a canvas (or whatever the 3D equivalent is) to draw on. I'd like to draw some elements using the built-in Android UI tools rather than building everything from scratch using canvas commands or a loading a pre-rendered UI bitmap.</p>
  
  <p>Converting a single View to a Bitmap works fine. i.e. this works great:</p>
  
  <pre><code>// For example this works:
  TextView view = new TextView(ctx);
  view.layout(0, 0, 200, 100);
  view.setText("test");
  Bitmap b = Bitmap.createBitmap( 200, 100, Bitmap.Config.ARGB_8888);                
  Canvas tempC = new Canvas(b);
  view.draw(tempC);
  c.drawBitmap(b, 200, 100, mPaint);
  </code></pre>
  
  <p>But, converting a LinearLayout with children causes problems. You <em>only</em> get the LinearLayout itself and none of it's children. For example, if I set the LinearLayout to have a white background I get a nicely rendered white box, but none of the TextView children are in the Bitmap. I've also tried using DrawingCache with similar results. </p>
  
  <p>The code I'm using is the cube example with the only changes being an extra draw command. The LinearLayout works fine as a toast or as a regular view (i.e. everything nicely shows up), on the LiveWallpaper all I get is the LinearLayout's background rendered.</p>
  
  <pre><code>inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layout = (LinearLayout) inflater.inflate(com.example.android.livecubes.R.layout.testLinearLayout, null);
  layout.layout(0, 0, 400, 200);
  
  Bitmap b = Bitmap.createBitmap( 400, 200, Bitmap.Config.ARGB_8888);
  Canvas tempC = new Canvas(b);
  layout.draw(tempC);
  c.drawBitmap(b, 10, 200, mPaint);
  </code></pre>
  
  <p>Does anyone know if you need to do anything special to get the children rendered properly to my bitmap? i.e. do I need to somehow do something special to make the layout render the rest of the children? Should I write a function to recursively do something to all the children?</p>
  
  <p>I could composite everything myself but, since the display is fairly static (i.e. I draw this once and keep a copy of the bitmap to draw on the background) this seems easier on me and still pretty efficient.</p>
  
  <p><strong>Edit:</strong>
  While digging a bit more into the state of the Layout it looks as though the layout is not progressing down the view tree (i.e. the LinearLayout gets its layout computed when I call layout() but the children have a null (0x0) size). According to the Romain Guy's post in 2008 <a href="http://groups.google.com/group/android-developers/browse_thread/thread/8318671d0313024/e32c50aff962e0a2" rel="nofollow">android developer post</a>. You have to wait for the layout pass or force the layout yourself. The problem is how can I wait for a layout pass from a wall paper engine for a LinearLayout that is not attached to the root view group? And how can I manually layout each child element if layout requires you to set the left, top, right, bottom when I don't know what these should be.</p>
  
  <p>I've tried calling forceLayout on the children but it doesn't seem to help either. I'm not sure how the layout framework works behind the scenes (besides that it does a two pass layout). Is there a way to manually make it do the layout pass, i.e. right now? Since it's not an Activity I don't think a lot of the normal background stuff is happening quite the way I'd like.</p>