i create following code create bar chart , pie chart using canvas.
here code
public class chartdemo extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //scrollview sv = new scrollview(this); linearlayout llay = new linearlayout(this); llay.setorientation(linearlayout.vertical); float[] values = { 50, 100, 50, 20, 30, 60, 100, 90 }; // bar chart bargraph barchart = new bargraph(this, values); llay.addview(barchart); //pie chart piechartview pie = new piechartview(this, values); llay.addview(pie); //sv.addview(llay); setcontentview(llay); //setcontentview(sv); } }
the above code show barchart only. change code following gives black(blank)screen only.with out no error , exception
public class chartdemo extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); scrollview sv = new scrollview(this); linearlayout llay = new linearlayout(this); llay.setorientation(linearlayout.vertical); float[] values = { 50, 100, 50, 20, 30, 60, 100, 90 }; // bar chart bargraph barchart = new bargraph(this, values); llay.addview(barchart); //pie chart piechartview pie = new piechartview(this, values); llay.addview(pie); sv.addview(llay); setcontentview(sv); } }
and create chart views following
public class piechartview extends view { private float[] values; public piechartview(context context, float[] values) { super(context); this.values = values; } protected void ondraw(canvas canvas) { super.ondraw(canvas); ....... ......... } }
i need add both chart in single screen scroll view . not able add both in single activity.how ??
when add programmatically views layout, linearlayout or scrollview (which derives framelayout), should set layout parameters on views, (just example):
bargraph barchart = new bargraph(this, values); // sure use correct layout params layout linearlayout.layoutparams llp = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); llp.weight = 1.0f; barchart.setlayoutparams(llp); llay.addview(barchart); framelayout.layoutparams flp = new /* ... */; llay.setlayoutparams(flp); sv.addview(llay);
if don't set them, default ones depending on layout, , might job or not depending on added views. (btw conventionally in java variables names begin lower case)
Comments
Post a Comment