Question

I'm new to the Android SDK, but am pretty well versed in Java. I need to create a graphing function for a class, but I'm not entirely sure how to do it without using swing and in XML. Any suggestions?

Was it helpful?

Solution

If you are talking about displaying a graph such as a bar or line graph I would suggest using a library to help you out. Have a look here:

https://stackoverflow.com/questions/424752/any-good-graphing-packages-for-android?lq=1

OTHER TIPS

You don't have to use XML. Its just the default way eclipse wants you to create your UI. The best way to do what you want is to create a class that extends view and override the ondraw method. Then you call setContentView(yourclass);

Here is an example of some code that simply draws a line on the screen that should be enough to get you started.

The main Activity:

package com.example.myexample;

import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Create class that extends view
    Example myexample = new Example(this);

    setContentView(myexample);
}

}

And the Example class would look like this:

package com.example.myexample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

public class Example extends View{

    //You will need to declare a variable of paint to draw
    Paint mypaint = new Paint();


    //constructor
    public Example(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    //This is the function where you draw to the screen
    @Override
    public void onDraw (Canvas canvas){


        canvas.drawLine(25, 25, 25, 50, mypaint);   
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top