0

I am trying to draw a graph in Android. I want the graph to scale to current screen size, so instead of setting them explicitly in the constants, I get the size of an LinearLayout which is intended to contain the graph. However, there is a problem that it's not possible to get sizes in activity's onCreate(), so I use a custom LinearLayout with overridden onSizeChanged(). I include it into layout with:

view class="com.nnevod.loggraph.graph$GraphDisplayLayout"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:background="@color/White"

I've omitted angular brackets.

In the overridden onSizeChanged(), dimensions of the view are read, then everything is done as described in many graphing examples: a Bitmap is created, passed to a graph-drawing class, an ImageView is created, is set with the Bitmap, and then added to the custom LinearLayout.

Problem is, the graph's image is not visible. However, if I try to inspect it with HierarchyViewer, the image becomes visible. If I cut-paste the code from onSizeChanged() to activity's onCreate(), sans using preset dimensions instead of measured ones, everything is displayed nicely.

So, my question is what I'm doing wrong? I've had assumptions that either I'm using a wrong context in custom onSizeChanged(), or it is not possible to update the view from there, and I should somehow pass the measured dimensions to the activity and do the attachment of the Bitmap in some of activity's methods. Though on the context part, I've tried using all available contexts, including using activity's context, to no avail.

If needed, I'll provide more code.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nnevod
  • 61
  • 2
  • 6

1 Answers1

1

I had a similar problem once, and I was told that what Hierarchy viewer does is a requestLayout(). So, I was told, I probably forgot to call it somewhere, but I don't think I did. Later I've found that a similar situation was reported as a bug, being a workaround finding the way to call requestLayout() somewhere.

It was strange because actually at some conditions the thing worked (adding some random view here and there, or updating the drawing). Anyway, at the end I preferred creating a custom view that simply directly drew what I needed (and as drawing happens after onMeasure, there's no problem in making an adaptive drawing).

(funny it's again about graphs!)

references (1) and (2).

Community
  • 1
  • 1
bigstones
  • 15,087
  • 7
  • 65
  • 82