Search code examples
androidandroid-graphview

Set the background color of a GraphView


I am attempting to set the background color of a GraphView (http://android-graphview.org). I looked at the following StackOverflow question: https://stackoverflow.com/questions/24276370/changing-graphview-background-not-working but there was no joy. I also took a peek at the GraphView demos at: https://github.com/jjoe64/GraphView-Demos but unfortunately, none of them set the background color to something other than white. So, what I did try was:

graphView.setBackgroundColor(getResources().getColor(R.color.teal));

and I also tried:

graphView.setBackgroundColor(getResources().getColor(R.color.teal));
((LineGraphView) graphView).setDrawBackground(true);

neither wanted to work. Thoughts on what I need to do?


Solution

  • Your code looks fine - that should work perfectly. It does for me:

    graphView.setDrawBackground(true);
    graphView.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
    

    If you want the series to have a slightly transparent background, you can set an alpha-color value:

    graphView.setBackgroundColor(Color.argb(50, 50, 0, 200));
    

    As an aside, if you are looking to set the background for each series independantly, I have left the details on another question: