you should overried onlayout and onmeasure without that it wont work, it
gives the size to your component.

Look into creating custom view first.

http://developer.android.com/intl/de/guide/topics/ui/custom-components.html




On Mon, Apr 19, 2010 at 3:00 PM, zebdor <[email protected]> wrote:

> Hello,
>
> I hope this question belongs in the Android Developers and not Android
> Beginners group but I have a question about ScrollViews. I am
> currently working with a customized GraphView class and am trying to
> put it into a ScrollView but it doesn't seem to work. Whenever I run
> the code, it comes up with a blank screen. I've written the code in
> XML and also tried to create the ScrollView encapsulating the
> GraphView programmatically but whenever the code runs, it comes up
> with just a blank screen and GraphView's onDraw() is never called.
>
> Here's the code (with XML file):
>
> GraphView.java:
> public class GraphView extends View {
>
>     private Paint paint;
>     private float[] values;
>     private String[] horlabels;
>     private String[] verlabels;
>     private String title;
>
>     public GraphView(Context context, AttributeSet attributeSet) {
>          super(context, attributeSet);
>
>          title = "Graph";
>          float[] values = new float[] { 2.0f, 1.5f, 2.5f, 1.0f,
> 3.0f };
>          String[] verlabels = new String[] { "great", "ok", "bad" };
>          String[] horlabels = new String[] { "today", "tomorrow",
> "next week", "next month" };
>
>          if (values == null)
>               values = new float[0];
>          else
>               this.values = values;
>
>          if (horlabels == null)
>               this.horlabels = new String[0];
>          else
>               this.horlabels = horlabels;
>          if (verlabels == null)
>               this.verlabels = new String[0];
>          else
>               this.verlabels = verlabels;
>          paint = new Paint();
>     }
>
>     @Override
>     protected void onDraw(Canvas canvas) {
>          float border = 20;
>          float horstart = border * 2;
>          float height = getHeight();
>          float width = getWidth() - 1;
>          float max = getMax();
>          float min = getMin();
>          float diff = max - min;
>          float graphheight = height - (2 * border);
>          float graphwidth = width - (2 * border);
>
>          paint.setTextAlign(Align.LEFT);
>          int vers = verlabels.length - 1;
>          for (int i = 0; i < verlabels.length; i++) {
>               paint.setColor(Color.DKGRAY);
>               float y = ((graphheight / vers) * i) + border;
>               canvas.drawLine(horstart, y, width, y, paint);
>               paint.setColor(Color.WHITE);
>               canvas.drawText(verlabels[i], 0, y, paint);
>          }
>          int hors = horlabels.length - 1;
>          for (int i = 0; i < horlabels.length; i++) {
>               paint.setColor(Color.DKGRAY);
>               float x = ((graphwidth / hors) * i) + horstart;
>               canvas.drawLine(x, height - border, x, border, paint);
>               paint.setTextAlign(Align.CENTER);
>               if (i==horlabels.length-1)
>                    paint.setTextAlign(Align.RIGHT);
>               if (i==0)
>                    paint.setTextAlign(Align.LEFT);
>               paint.setColor(Color.WHITE);
>               canvas.drawText(horlabels[i], x, height - 4, paint);
>          }
>
>          paint.setTextAlign(Align.CENTER);
>          canvas.drawText(title, (graphwidth / 2) + horstart, border -
> 4, paint);
>
>          float colwidth = (width - (horstart)) / (horlabels.length -
> 1);
>          float lasth = 0;
>          for (int i = 0; i < values.length; i++) {
>               float val = values[i] - min;
>               float rat = val / diff;
>               float h = graphheight * rat;
>               if (i > 0)
>                    canvas.drawLine(((i - 1) * colwidth) + horstart,
> (border - lasth) + graphheight, (i * colwidth) + horstart, (border -
> h) + graphheight, paint);
>               lasth = h;
>          }
>     }
>
>     private float getMax() {
>          float largest = Integer.MIN_VALUE;
>          for (int i = 0; i < values.length; i++)
>               if (values[i] > largest)
>                    largest = values[i];
>          return largest;
>     }
>
>     private float getMin() {
>          float smallest = Integer.MAX_VALUE;
>          for (int i = 0; i < values.length; i++)
>               if (values[i] < smallest)
>                    smallest = values[i];
>          return smallest;
>     }
> }
>
>
> Graph.java:
> public class Graph extends Activity {
>    /** Called when the activity is first created. */
>    @Override
>    public void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
>        setContentView(R.layout.main);
>    }
> }
>
>
> main.xml:
> <?xml version="1.0" encoding="utf-8"?>
> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android";
>    android:layout_width="fill_parent"
>    android:layout_height="fill_parent"
>    >
>     <com.examples.graph.GraphView android:layout_width="fill_parent"
>          android:layout_height="fill_parent"
>     />
> </ScrollView>
>
>
>
> Any help would be greatly appreciated! Thanks!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to