Hi I am trying to draw a complex view, defined by an xml layout to a Bitmap (which is afterwards drawn to a canvas). That view is then updated regularly, when something changed in the data model.
Changing statically defined TextView works perfect, but adding new Views to a linearLayout is somehow not working. Where is my mistake?
This is how I am adding new Views:
linearLayoutContainer.removeAllViewsInLayout();
for (int i = 0 ; i < 5; i ++) {
TextView child = new TextView(getContext());
child.setText("example" + i);
child.setVisibility(View.VISIBLE);
linearLayoutContainer.addView(child, i , new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
i++;
}
this is how I am crating the bitmap:
sideBarView.refresh();
sideBarView.invalidate();
sideBarBitmap = sideBarView.getDrawingCache();
which works perfectly for updating the already in xml defined TextViews, but the new added views are not visible.
My xml layout looks like that:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some"
android:textSize="20dp" />
<LinearLayout
android:id="@+id/linearLayoutContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.33"
android:gravity="center_horizontal" >
<!-- THE CONTAINER -->
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.66"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="20%"
android:textSize="30dp" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="35%"
android:textSize="30dp" />
</LinearLayout>
I solved my problem. After refreshing my view by adding new views I should have called measure on the root view again. This is what I did wrong (in pseudo code):
So the problem was in the update code, which did not called measure again, so that the child views have not been remeasured , which of course does not happen when adding a view inside an activity, so thats what is needed:
and in real code: