Reputation: 55
I am trying to add a static BarChart to the activity, but its covering whole screen. The code i am using is below -
MainActivity.java
BarChart bc = (BarChart) findViewById(R.id.chart);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(4f, 0));
entries.add(new BarEntry(8f, 1));
entries.add(new BarEntry(6f, 2));
entries.add(new BarEntry(12f, 3));
entries.add(new BarEntry(18f, 4));
entries.add(new BarEntry(9f, 5));
entries.add(new BarEntry(12f, 6));
entries.add(new BarEntry(10f, 7));
BarDataSet dataset = new BarDataSet(entries, "# of Calls");
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
labels.add("July");
labels.add("August");
bc=new BarChart(this);
BarData data = new BarData(labels, dataset);
setContentView(bc);
bc.setData(data);
MainActivity.xml
<com.github.mikephil.charting.charts.BarChart
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/chart"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="62dp">
</com.github.mikephil.charting.charts.BarChart>
Where i am going wrong? Any suggestions
Upvotes: 3
Views: 6117
Reputation: 103
When you call setContentView()
the layout parameters of the specified view are ignored, and set to the default MATCH_PARENT
, so in your code setContentView(bc)
will cause your BarChart to fill the entire screen.
One solution would be to place your static BarChart view under a ViewGroup, like LinearLayout
, in activity_main.xml, and call setContentView(R.layout.activity_main)
.
EDIT:
Another thing, in your code you obtained the BarChart defined in your xml by doing:
BarChart bc = (BarChart) findViewById(R.id.chart);
Which is correct... but later on in your code, you made the above line pointless by assigning bc
to another BarChart you instantiated:
bc=new BarChart(this);
So, remove this line and you should be able to see your BarChart.
Upvotes: 2