Androidplot - creating chart without XML -
i need create piecharts on activity, , quantity of charts varies. try create piechart dynamically code, , it's doesn't work. layout correct - button added successfully. can me? code: layout:
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="example.bros.nik.tabs1.mealsactivity" android:layout_width="wrap_content" android:layout_height="wrap_content"> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/l_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" android:orientation="vertical" > </linearlayout> </scrollview>
java:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_meals); linearlayout l_layout = (linearlayout) findviewbyid(r.id.l_layout); meals_params = resultsactivity.get_meals_params(); linearlayout.layoutparams lp_view = new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content, 1.0f); piechart pie = new piechart(this, "chart#1"); segment s1 = new segment("Углеводы", meals_params[0].get_carbohydrates_percent()); segment s2 = new segment("Жиры", meals_params[0].get_fats_percent()); segment s3 = new segment("Белки", meals_params[0].get_proteins_percent()); embossmaskfilter emf = new embossmaskfilter(new float[]{1, 1, 1}, 0.4f, 10, 8.2f); segmentformatter sf1 = new segmentformatter(); sf1.configure(getapplicationcontext(), r.xml.pie_segment_formatter1); sf1.getfillpaint().setmaskfilter(emf); segmentformatter sf2 = new segmentformatter(); sf2.configure(getapplicationcontext(), r.xml.pie_segment_formatter2); sf2.getfillpaint().setmaskfilter(emf); segmentformatter sf3 = new segmentformatter(); sf3.configure(getapplicationcontext(), r.xml.pie_segment_formatter3); sf3.getfillpaint().setmaskfilter(emf); pie.addseries(s1, sf1); pie.addseries(s2, sf2); pie.addseries(s3, sf3); pie.getrenderer(pierenderer.class).setdonutsize(0.1f,pierenderer.donutmode.percent); pie.redraw(); pie.getborderpaint().setcolor(color.transparent); pie.getbackgroundpaint().setcolor(color.transparent); l_layout.addview(pie, lp_view);
i try this:
pie.setlayoutparams(lp_view); l_layout.addview(pie);
and it's doesn't me.
and if add button layout:
pie.setlayoutparams(lp_view); l_layout.addview(pie); button = new button(this); but.settext("bla"); l_layout.addview(but, lp_view);
then see button (look @ screenshot). added button
edit: in android docs written, in constructor
viewgroup.layoutparams (int width, int height)
attributes may absolute sizes of view, or may constants - wrap_content (int value -2), match_parent (int value -1). probably, piechart doesn't recognize these constants, , absolute size negative values. found solution:
private int[] getdisplayparams() { int[] display_params = new int[2]; display display = getwindowmanager().getdefaultdisplay(); if (build.version.sdk_int >= 13) { point size = new point(); display.getsize(size); display_params[0] = size.x; display_params[1] = size.y; } else { display_params[0] = display.getwidth(); display_params[1] = display.getheight(); } return display_params; }
and use size of screen determining size of chart:
int[] display_params = getdisplayparams(); double pie_size = display_params[0]*0.8; int pie_size_int = pie_size.intvalue(); viewgroup.layoutparams lp_view = new linearlayout.layoutparams(pie_size_int, pie_size_int);
looks not providing layoutparams pie. try doing before adding pie l_layout:
pie.setlayoutparams(new linearlayout.layoutparams( linearlayout.layoutparams.fill_parent, linearlayout.layoutparams.fill_parent));
edit: went , noticed layout enclosed in scrollview scrollview set wrap content , content set match parent seems might chicken , egg problem as described here. believe buttons, etc. have default non-zero values these whereas plots not, might explain why button works.
as sanity check, happens if specify absolute size plot this:
linearlayout.layoutparams lp_view = new linearlayout.layoutparams(100, 100);
you can try:
plot.setminimumheight(100); plot.setminimumwidth(100);
Comments
Post a Comment