I am using MPAndroidChart library. I want customize legends in MPAndroidChart. In MPAndroidChart library i tried to set the position of legends. by given code legend.setPosition(LegendPosition.BELOW_CHART_CENTER) but unable to do it. I have to set legends as shown in following image
help will be appreciate
Please look for the given answer MPAndroidChart - Legend labels are being cut off. I have already provided the answer according to your problem. Look for given code which will definitely help you. You will have to implement customized legends with their legends colours and labels by following the steps below:
Step 1
Legend legend = mChart.getLegend();
Step 2
int colorcodes[] = legend.Colors();
Steps 3
for (int i = 0; i < legend.Colors().length-1; i++) {
.....
.....
}
Steps 4
Then you will have to take one layout horizontal or vertical and get legends color codes and legends label and according to legends length create layout and label. Code sample is given below:
LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parms_left_layout.weight = 1F;
LinearLayout left_layout = new LinearLayout(context);
left_layout.setOrientation(LinearLayout.HORIZONTAL);
left_layout.setGravity(Gravity.CENTER);
left_layout.setLayoutParams(parms_left_layout);
LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
20, 20);
parms_legen_layout.setMargins(0, 0, 20, 0);
LinearLayout legend_layout = new LinearLayout(context);
legend_layout.setLayoutParams(parms_legen_layout);
legend_layout.setOrientation(LinearLayout.HORIZONTAL);
legend_layout.setBackgroundColor(colorcodes[i]);
left_layout.addView(legend_layout);
TextView txt_unit = new TextView(context);
txt_unit.setText(legend.getLabel(i));
left_layout.addView(txt_unit);
LinearLayout.LayoutParams parms_middle_layout = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parms_middle_layout.weight = 1F;
LinearLayout middle_layout = new LinearLayout(this);
middle_layout.setOrientation(LinearLayout.HORIZONTAL);
middle_layout.setGravity(Gravity.CENTER);
middle_layout.setLayoutParams(parms_middle_layout);
TextView txt_leads = new TextView(this);
txt_leads.setText("450");
middle_layout.addView(txt_leads);
LinearLayout.LayoutParams parms_right_layout = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parms_right_layout.weight = 1F;
LinearLayout right_layout = new LinearLayout(this);
right_layout.setOrientation(LinearLayout.HORIZONTAL);
right_layout.setGravity(Gravity.CENTER);
right_layout.setLayoutParams(parms_right_layout);
TextView txt_leads_percentage = new TextView(this);
txt_leads_percentage.setText(munit_percentage_list.get(i) + "");
right_layout.addView(txt_leads_percentage);
childlayout.addView(left_layout);
childlayout.addView(middle_layout);
childlayout.addView(right_layout);
And after this add your (child layout which you have created at runtime ) to the main layout.