I've been trying to draw a listView to a canvas so I can create a PDF document out of it, but have been unsuccessful. The problem is that my listView is longer than my screen, so only 6 elements out of the 9 are actually on screen, and only those 6 get drawn.
I've tried everything I can think of, but nothing seems to work. How do I get the entire listView so that I can draw it to a canvas?
Here is the XML of the listView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/cl_lv_k"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:dividerHeight="0dp" >
</ListView>
....
When I try and access the listView by findViewById(R.id.cl_lv_k)
it only gives me the current elements that are on the screen.
I've tried everything I can think of. Help would be greatly appreciated.
EDIT:
I got a solution. Thanks to @Mike M. for finding a related question: Android get screenshot of all ListView items
I used the following code in my program, and it worked.
ListView listView = (ListView) findViewById (R.id.cl_lv_k);
PageInfo pageInfo =
new PageInfo.Builder(pageWidth, 12000, 1).create();
Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
ChartAdapter adapter = listView.getAdapter(); //this is just a custom adapter
for (int i = 0; i < adapter.size(); i++)
{
View childView = adapter.getView(i, null, kColumn);
childView.measure(MeasureSpec.makeMeasureSpec(kColumn.getWidth(),
MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(),
childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
childView.getDrawingCache();
childView.draw(canvas);
canvas.translate(0, childView.getMeasuredHeight());
}
....
I made a PDF document out of this page, so I filled in the rest of the PDF code and it worked.