Look at this answer MPAndroidChart - legend labels are truncated . I have already provided an answer according to your problem. Look for this code that will definitely help you. You will need to implement individual legends with colors and legend labels by following these steps:
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 need to make one layout horizontally or vertically and get the legendary color codes and legends, and in accordance with the length of the legends - create a layout and label. An example code 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 that, add your (child layout that you created at runtime) to the main layout.
Amandeep rohila
source share