How to add text dynamically inside the Mpandroid Pie chart?












0















I have followed the MpAndroid Library for chart creation.I wanted to set Dynamic text on the center of the Pie chart .I am creating a BMI calculator ,so i wanted to display the BMI value inside the center of the Pie chart .
This is the code which i used to display the text inside the Pie chart .
`



        mChart.setDrawCenterText(true);
mChart.setCenterTextTypeface(mTfLight);

mChart.setCenterTextColor(Color.WHITE);
mChart.setCenterTextSize(15f);
mChart.setCenterText("BMI:"+bmi);


`



Please have a look at the full code .In the Oncreate() method i have set the above code for setting the text in the center but this is showing a static text , how to set the text inside the center dynamically (i.e. the calculated value of the BMI).????



MainActivity.java



    public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private int selectedId;
private EditText age;
private EditText height;
private EditText weight;
private TextView result;
private Spinner height_spinner;
private Spinner weight_spinner;
private Button calculate;
private PieChart mChart;

String spinner_height; //Height Spinner
String spinner_weight; //Weight Spinner
String selected_item1;
String selected_item2;

float bmi=0f;

protected String BMIcategory = new String
{
"Very Severly Underweight", "Severly Underweight", "Underweight", "Normal",
"Overweight", "Obese Class I", "Obese Class II", "Obese Class III",
};




protected Typeface mTfRegular;
protected Typeface mTfLight;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("BMI Calculator");

mTfRegular=Typeface.MONOSPACE;
mTfLight = Typeface.MONOSPACE;
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
mChart=(PieChart)findViewById(R.id.chart);
age=(EditText) findViewById(R.id.age);
height=(EditText) findViewById(R.id.height);
weight=(EditText) findViewById(R.id.weight);
// result=(TextView) findViewById(R.id.result);
height_spinner=(Spinner) findViewById(R.id.height_spinner);
weight_spinner=(Spinner) findViewById(R.id.weight_spinner);
calculate=(Button) findViewById(R.id.calculate_button);

mChart.setBackgroundColor(Color.rgb(76,0,0));
// moveOffScreen();
mChart.setUsePercentValues(false);
mChart.getDescription().setEnabled(false);

mChart.setDrawCenterText(true);
mChart.setCenterTextTypeface(mTfLight);

mChart.setCenterTextColor(Color.WHITE);
mChart.setCenterTextSize(15f);
mChart.setCenterText("BMI:"+bmi);

mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.rgb(76,0,0));

mChart.setTransparentCircleColor(Color.rgb(76,0,0));
mChart.setTransparentCircleAlpha(110);

mChart.setHoleRadius(65f);
mChart.setTransparentCircleRadius(68f);
mChart.setDrawSliceText(false);
mChart.setDrawEntryLabels(false);

mChart.setRotationEnabled(false);
mChart.setHighlightPerTapEnabled(false);

mChart.setMaxAngle(270f); // HALF CHART
mChart.setRotationAngle(135f);
// mChart.setRotation(180f);


// mChart.setCenterTextOffset(0, -20);

setData(8, 80);

mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
mChart.getLegend().setEnabled(false);

calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculate();
}
});


set_spinner();

}

private void setData(int count, float range) {

ArrayList<PieEntry> values = new ArrayList<>();

values.add(new PieEntry(10f,BMIcategory[0]));
values.add(new PieEntry(15f,BMIcategory[1]));
values.add(new PieEntry(8f,BMIcategory[2]));
values.add(new PieEntry(12f,BMIcategory[3]));
values.add(new PieEntry(5f,BMIcategory[4]));
values.add(new PieEntry(5f,BMIcategory[5]));
values.add(new PieEntry(15f,BMIcategory[6]));
values.add(new PieEntry(10f,BMIcategory[7]));


PieDataSet dataSet = new PieDataSet(values, "BMI Category");
dataSet.setSliceSpace(1f);
dataSet.setSelectionShift(5f);

// add many colors
ArrayList<Integer> colors = new ArrayList<Integer>();

for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);

for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);

for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);

for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);

for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);

colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);


//dataSet.setSelectionShift(0f);

PieData data = new PieData(dataSet);
data.setDrawValues(false);

mChart.setData(data);
mChart.invalidate();
}


public void set_spinner(){

ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.height_spinner_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
height_spinner.setAdapter(adapter1);

height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
int a = parent.getSelectedItemPosition();
spinner_height = height_spinner.getSelectedItem().toString();
if (a == 0) {
selected_item1="Meters";
}
if (a == 1) {
selected_item1="Inches";

}
if(a==2)
{
selected_item1="Centimeters";
}

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});


ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.weight_spinner_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weight_spinner.setAdapter(adapter2);
weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
int b = parent.getSelectedItemPosition();
spinner_weight = weight_spinner.getSelectedItem().toString();
if(b==0)
{
selected_item2="Kgs";
}
if(b==1)
{
selected_item2="Lbs";
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {} } );


}

public void calculate() {

float getHeight, getWeight;

DecimalFormat df = new DecimalFormat("#0.00");
// for height in m and weight in kgs

if (height.getText().toString().equals("")) {
getHeight = 0f;
} else {
getHeight = Float.parseFloat(height.getText().toString());
}
if (weight.getText().toString().equals("")) {
getWeight = 0f;
} else {
getWeight = Float.parseFloat(weight.getText().toString());
}
if(height.getText().toString().equals("") || weight.getText().toString().equals("") || age.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter All the Values",Toast.LENGTH_SHORT).show();
return;
}

if(selected_item1.equals("Meters"))
{
if(selected_item2.equals("Kgs"))
{
bmi = getWeight / (getHeight * getHeight);

}
if(selected_item2.equals("Lbs"))
{
bmi = (getWeight*0.453592f) / (getHeight * getHeight);
}
}
if(selected_item1.equals("Inches"))
{
if(selected_item2.equals("Kgs"))
{
bmi = (getWeight*318.87517f) / (getHeight * getHeight);

}
if(selected_item2.equals("Lbs"))
{
bmi = (getWeight*703f) / (getHeight * getHeight);
}

}
if(selected_item1.equals("Centimeters"))
{
if(selected_item2.equals("Kgs"))
{
bmi = (getWeight*10000) / (getHeight * getHeight);
}
if(selected_item2.equals("Lbs"))
{
bmi=(getWeight*0.453592f*10000) / (getHeight * getHeight);
}
}
// result.setText("Your BMI is " + df.format((double)bmi));



}


@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}



}


One more thing i wanted to ask ,How do i decrease the width of the Slices of my Pie chart .There is a option to adjust the radius of the Inner circle ,but how do i adjust the width of the Slices .??



Any Kind of Help is most Welcome .Thanks !!










share|improve this question



























    0















    I have followed the MpAndroid Library for chart creation.I wanted to set Dynamic text on the center of the Pie chart .I am creating a BMI calculator ,so i wanted to display the BMI value inside the center of the Pie chart .
    This is the code which i used to display the text inside the Pie chart .
    `



            mChart.setDrawCenterText(true);
    mChart.setCenterTextTypeface(mTfLight);

    mChart.setCenterTextColor(Color.WHITE);
    mChart.setCenterTextSize(15f);
    mChart.setCenterText("BMI:"+bmi);


    `



    Please have a look at the full code .In the Oncreate() method i have set the above code for setting the text in the center but this is showing a static text , how to set the text inside the center dynamically (i.e. the calculated value of the BMI).????



    MainActivity.java



        public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    private RadioGroup radioSexGroup;
    private RadioButton radioSexButton;
    private int selectedId;
    private EditText age;
    private EditText height;
    private EditText weight;
    private TextView result;
    private Spinner height_spinner;
    private Spinner weight_spinner;
    private Button calculate;
    private PieChart mChart;

    String spinner_height; //Height Spinner
    String spinner_weight; //Weight Spinner
    String selected_item1;
    String selected_item2;

    float bmi=0f;

    protected String BMIcategory = new String
    {
    "Very Severly Underweight", "Severly Underweight", "Underweight", "Normal",
    "Overweight", "Obese Class I", "Obese Class II", "Obese Class III",
    };




    protected Typeface mTfRegular;
    protected Typeface mTfLight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setTitle("BMI Calculator");

    mTfRegular=Typeface.MONOSPACE;
    mTfLight = Typeface.MONOSPACE;
    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    selectedId = radioSexGroup.getCheckedRadioButtonId();
    radioSexButton = (RadioButton) findViewById(selectedId);
    mChart=(PieChart)findViewById(R.id.chart);
    age=(EditText) findViewById(R.id.age);
    height=(EditText) findViewById(R.id.height);
    weight=(EditText) findViewById(R.id.weight);
    // result=(TextView) findViewById(R.id.result);
    height_spinner=(Spinner) findViewById(R.id.height_spinner);
    weight_spinner=(Spinner) findViewById(R.id.weight_spinner);
    calculate=(Button) findViewById(R.id.calculate_button);

    mChart.setBackgroundColor(Color.rgb(76,0,0));
    // moveOffScreen();
    mChart.setUsePercentValues(false);
    mChart.getDescription().setEnabled(false);

    mChart.setDrawCenterText(true);
    mChart.setCenterTextTypeface(mTfLight);

    mChart.setCenterTextColor(Color.WHITE);
    mChart.setCenterTextSize(15f);
    mChart.setCenterText("BMI:"+bmi);

    mChart.setDrawHoleEnabled(true);
    mChart.setHoleColor(Color.rgb(76,0,0));

    mChart.setTransparentCircleColor(Color.rgb(76,0,0));
    mChart.setTransparentCircleAlpha(110);

    mChart.setHoleRadius(65f);
    mChart.setTransparentCircleRadius(68f);
    mChart.setDrawSliceText(false);
    mChart.setDrawEntryLabels(false);

    mChart.setRotationEnabled(false);
    mChart.setHighlightPerTapEnabled(false);

    mChart.setMaxAngle(270f); // HALF CHART
    mChart.setRotationAngle(135f);
    // mChart.setRotation(180f);


    // mChart.setCenterTextOffset(0, -20);

    setData(8, 80);

    mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
    mChart.getLegend().setEnabled(false);

    calculate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    calculate();
    }
    });


    set_spinner();

    }

    private void setData(int count, float range) {

    ArrayList<PieEntry> values = new ArrayList<>();

    values.add(new PieEntry(10f,BMIcategory[0]));
    values.add(new PieEntry(15f,BMIcategory[1]));
    values.add(new PieEntry(8f,BMIcategory[2]));
    values.add(new PieEntry(12f,BMIcategory[3]));
    values.add(new PieEntry(5f,BMIcategory[4]));
    values.add(new PieEntry(5f,BMIcategory[5]));
    values.add(new PieEntry(15f,BMIcategory[6]));
    values.add(new PieEntry(10f,BMIcategory[7]));


    PieDataSet dataSet = new PieDataSet(values, "BMI Category");
    dataSet.setSliceSpace(1f);
    dataSet.setSelectionShift(5f);

    // add many colors
    ArrayList<Integer> colors = new ArrayList<Integer>();

    for (int c : ColorTemplate.VORDIPLOM_COLORS)
    colors.add(c);

    for (int c : ColorTemplate.JOYFUL_COLORS)
    colors.add(c);

    for (int c : ColorTemplate.COLORFUL_COLORS)
    colors.add(c);

    for (int c : ColorTemplate.LIBERTY_COLORS)
    colors.add(c);

    for (int c : ColorTemplate.PASTEL_COLORS)
    colors.add(c);

    colors.add(ColorTemplate.getHoloBlue());
    dataSet.setColors(colors);


    //dataSet.setSelectionShift(0f);

    PieData data = new PieData(dataSet);
    data.setDrawValues(false);

    mChart.setData(data);
    mChart.invalidate();
    }


    public void set_spinner(){

    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.height_spinner_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    height_spinner.setAdapter(adapter1);

    height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    int a = parent.getSelectedItemPosition();
    spinner_height = height_spinner.getSelectedItem().toString();
    if (a == 0) {
    selected_item1="Meters";
    }
    if (a == 1) {
    selected_item1="Inches";

    }
    if(a==2)
    {
    selected_item1="Centimeters";
    }

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
    }
    });


    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.weight_spinner_array, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    weight_spinner.setAdapter(adapter2);
    weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    int b = parent.getSelectedItemPosition();
    spinner_weight = weight_spinner.getSelectedItem().toString();
    if(b==0)
    {
    selected_item2="Kgs";
    }
    if(b==1)
    {
    selected_item2="Lbs";
    }
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {} } );


    }

    public void calculate() {

    float getHeight, getWeight;

    DecimalFormat df = new DecimalFormat("#0.00");
    // for height in m and weight in kgs

    if (height.getText().toString().equals("")) {
    getHeight = 0f;
    } else {
    getHeight = Float.parseFloat(height.getText().toString());
    }
    if (weight.getText().toString().equals("")) {
    getWeight = 0f;
    } else {
    getWeight = Float.parseFloat(weight.getText().toString());
    }
    if(height.getText().toString().equals("") || weight.getText().toString().equals("") || age.getText().toString().equals(""))
    {
    Toast.makeText(this,"Please Enter All the Values",Toast.LENGTH_SHORT).show();
    return;
    }

    if(selected_item1.equals("Meters"))
    {
    if(selected_item2.equals("Kgs"))
    {
    bmi = getWeight / (getHeight * getHeight);

    }
    if(selected_item2.equals("Lbs"))
    {
    bmi = (getWeight*0.453592f) / (getHeight * getHeight);
    }
    }
    if(selected_item1.equals("Inches"))
    {
    if(selected_item2.equals("Kgs"))
    {
    bmi = (getWeight*318.87517f) / (getHeight * getHeight);

    }
    if(selected_item2.equals("Lbs"))
    {
    bmi = (getWeight*703f) / (getHeight * getHeight);
    }

    }
    if(selected_item1.equals("Centimeters"))
    {
    if(selected_item2.equals("Kgs"))
    {
    bmi = (getWeight*10000) / (getHeight * getHeight);
    }
    if(selected_item2.equals("Lbs"))
    {
    bmi=(getWeight*0.453592f*10000) / (getHeight * getHeight);
    }
    }
    // result.setText("Your BMI is " + df.format((double)bmi));



    }


    @Override
    public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
    drawer.closeDrawer(GravityCompat.START);
    } else {
    super.onBackPressed();
    }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }

    return super.onOptionsItemSelected(item);
    }



    }


    One more thing i wanted to ask ,How do i decrease the width of the Slices of my Pie chart .There is a option to adjust the radius of the Inner circle ,but how do i adjust the width of the Slices .??



    Any Kind of Help is most Welcome .Thanks !!










    share|improve this question

























      0












      0








      0


      1






      I have followed the MpAndroid Library for chart creation.I wanted to set Dynamic text on the center of the Pie chart .I am creating a BMI calculator ,so i wanted to display the BMI value inside the center of the Pie chart .
      This is the code which i used to display the text inside the Pie chart .
      `



              mChart.setDrawCenterText(true);
      mChart.setCenterTextTypeface(mTfLight);

      mChart.setCenterTextColor(Color.WHITE);
      mChart.setCenterTextSize(15f);
      mChart.setCenterText("BMI:"+bmi);


      `



      Please have a look at the full code .In the Oncreate() method i have set the above code for setting the text in the center but this is showing a static text , how to set the text inside the center dynamically (i.e. the calculated value of the BMI).????



      MainActivity.java



          public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {

      private RadioGroup radioSexGroup;
      private RadioButton radioSexButton;
      private int selectedId;
      private EditText age;
      private EditText height;
      private EditText weight;
      private TextView result;
      private Spinner height_spinner;
      private Spinner weight_spinner;
      private Button calculate;
      private PieChart mChart;

      String spinner_height; //Height Spinner
      String spinner_weight; //Weight Spinner
      String selected_item1;
      String selected_item2;

      float bmi=0f;

      protected String BMIcategory = new String
      {
      "Very Severly Underweight", "Severly Underweight", "Underweight", "Normal",
      "Overweight", "Obese Class I", "Obese Class II", "Obese Class III",
      };




      protected Typeface mTfRegular;
      protected Typeface mTfLight;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      setTitle("BMI Calculator");

      mTfRegular=Typeface.MONOSPACE;
      mTfLight = Typeface.MONOSPACE;
      radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
      selectedId = radioSexGroup.getCheckedRadioButtonId();
      radioSexButton = (RadioButton) findViewById(selectedId);
      mChart=(PieChart)findViewById(R.id.chart);
      age=(EditText) findViewById(R.id.age);
      height=(EditText) findViewById(R.id.height);
      weight=(EditText) findViewById(R.id.weight);
      // result=(TextView) findViewById(R.id.result);
      height_spinner=(Spinner) findViewById(R.id.height_spinner);
      weight_spinner=(Spinner) findViewById(R.id.weight_spinner);
      calculate=(Button) findViewById(R.id.calculate_button);

      mChart.setBackgroundColor(Color.rgb(76,0,0));
      // moveOffScreen();
      mChart.setUsePercentValues(false);
      mChart.getDescription().setEnabled(false);

      mChart.setDrawCenterText(true);
      mChart.setCenterTextTypeface(mTfLight);

      mChart.setCenterTextColor(Color.WHITE);
      mChart.setCenterTextSize(15f);
      mChart.setCenterText("BMI:"+bmi);

      mChart.setDrawHoleEnabled(true);
      mChart.setHoleColor(Color.rgb(76,0,0));

      mChart.setTransparentCircleColor(Color.rgb(76,0,0));
      mChart.setTransparentCircleAlpha(110);

      mChart.setHoleRadius(65f);
      mChart.setTransparentCircleRadius(68f);
      mChart.setDrawSliceText(false);
      mChart.setDrawEntryLabels(false);

      mChart.setRotationEnabled(false);
      mChart.setHighlightPerTapEnabled(false);

      mChart.setMaxAngle(270f); // HALF CHART
      mChart.setRotationAngle(135f);
      // mChart.setRotation(180f);


      // mChart.setCenterTextOffset(0, -20);

      setData(8, 80);

      mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
      mChart.getLegend().setEnabled(false);

      calculate.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      calculate();
      }
      });


      set_spinner();

      }

      private void setData(int count, float range) {

      ArrayList<PieEntry> values = new ArrayList<>();

      values.add(new PieEntry(10f,BMIcategory[0]));
      values.add(new PieEntry(15f,BMIcategory[1]));
      values.add(new PieEntry(8f,BMIcategory[2]));
      values.add(new PieEntry(12f,BMIcategory[3]));
      values.add(new PieEntry(5f,BMIcategory[4]));
      values.add(new PieEntry(5f,BMIcategory[5]));
      values.add(new PieEntry(15f,BMIcategory[6]));
      values.add(new PieEntry(10f,BMIcategory[7]));


      PieDataSet dataSet = new PieDataSet(values, "BMI Category");
      dataSet.setSliceSpace(1f);
      dataSet.setSelectionShift(5f);

      // add many colors
      ArrayList<Integer> colors = new ArrayList<Integer>();

      for (int c : ColorTemplate.VORDIPLOM_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.JOYFUL_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.COLORFUL_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.LIBERTY_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.PASTEL_COLORS)
      colors.add(c);

      colors.add(ColorTemplate.getHoloBlue());
      dataSet.setColors(colors);


      //dataSet.setSelectionShift(0f);

      PieData data = new PieData(dataSet);
      data.setDrawValues(false);

      mChart.setData(data);
      mChart.invalidate();
      }


      public void set_spinner(){

      ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.height_spinner_array, android.R.layout.simple_spinner_item);
      adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      height_spinner.setAdapter(adapter1);

      height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
      int a = parent.getSelectedItemPosition();
      spinner_height = height_spinner.getSelectedItem().toString();
      if (a == 0) {
      selected_item1="Meters";
      }
      if (a == 1) {
      selected_item1="Inches";

      }
      if(a==2)
      {
      selected_item1="Centimeters";
      }

      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      // Another interface callback
      }
      });


      ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.weight_spinner_array, android.R.layout.simple_spinner_item);
      adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      weight_spinner.setAdapter(adapter2);
      weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
      int b = parent.getSelectedItemPosition();
      spinner_weight = weight_spinner.getSelectedItem().toString();
      if(b==0)
      {
      selected_item2="Kgs";
      }
      if(b==1)
      {
      selected_item2="Lbs";
      }
      }
      @Override
      public void onNothingSelected(AdapterView<?> parent) {} } );


      }

      public void calculate() {

      float getHeight, getWeight;

      DecimalFormat df = new DecimalFormat("#0.00");
      // for height in m and weight in kgs

      if (height.getText().toString().equals("")) {
      getHeight = 0f;
      } else {
      getHeight = Float.parseFloat(height.getText().toString());
      }
      if (weight.getText().toString().equals("")) {
      getWeight = 0f;
      } else {
      getWeight = Float.parseFloat(weight.getText().toString());
      }
      if(height.getText().toString().equals("") || weight.getText().toString().equals("") || age.getText().toString().equals(""))
      {
      Toast.makeText(this,"Please Enter All the Values",Toast.LENGTH_SHORT).show();
      return;
      }

      if(selected_item1.equals("Meters"))
      {
      if(selected_item2.equals("Kgs"))
      {
      bmi = getWeight / (getHeight * getHeight);

      }
      if(selected_item2.equals("Lbs"))
      {
      bmi = (getWeight*0.453592f) / (getHeight * getHeight);
      }
      }
      if(selected_item1.equals("Inches"))
      {
      if(selected_item2.equals("Kgs"))
      {
      bmi = (getWeight*318.87517f) / (getHeight * getHeight);

      }
      if(selected_item2.equals("Lbs"))
      {
      bmi = (getWeight*703f) / (getHeight * getHeight);
      }

      }
      if(selected_item1.equals("Centimeters"))
      {
      if(selected_item2.equals("Kgs"))
      {
      bmi = (getWeight*10000) / (getHeight * getHeight);
      }
      if(selected_item2.equals("Lbs"))
      {
      bmi=(getWeight*0.453592f*10000) / (getHeight * getHeight);
      }
      }
      // result.setText("Your BMI is " + df.format((double)bmi));



      }


      @Override
      public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
      drawer.closeDrawer(GravityCompat.START);
      } else {
      super.onBackPressed();
      }
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
      return true;
      }

      return super.onOptionsItemSelected(item);
      }



      }


      One more thing i wanted to ask ,How do i decrease the width of the Slices of my Pie chart .There is a option to adjust the radius of the Inner circle ,but how do i adjust the width of the Slices .??



      Any Kind of Help is most Welcome .Thanks !!










      share|improve this question














      I have followed the MpAndroid Library for chart creation.I wanted to set Dynamic text on the center of the Pie chart .I am creating a BMI calculator ,so i wanted to display the BMI value inside the center of the Pie chart .
      This is the code which i used to display the text inside the Pie chart .
      `



              mChart.setDrawCenterText(true);
      mChart.setCenterTextTypeface(mTfLight);

      mChart.setCenterTextColor(Color.WHITE);
      mChart.setCenterTextSize(15f);
      mChart.setCenterText("BMI:"+bmi);


      `



      Please have a look at the full code .In the Oncreate() method i have set the above code for setting the text in the center but this is showing a static text , how to set the text inside the center dynamically (i.e. the calculated value of the BMI).????



      MainActivity.java



          public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {

      private RadioGroup radioSexGroup;
      private RadioButton radioSexButton;
      private int selectedId;
      private EditText age;
      private EditText height;
      private EditText weight;
      private TextView result;
      private Spinner height_spinner;
      private Spinner weight_spinner;
      private Button calculate;
      private PieChart mChart;

      String spinner_height; //Height Spinner
      String spinner_weight; //Weight Spinner
      String selected_item1;
      String selected_item2;

      float bmi=0f;

      protected String BMIcategory = new String
      {
      "Very Severly Underweight", "Severly Underweight", "Underweight", "Normal",
      "Overweight", "Obese Class I", "Obese Class II", "Obese Class III",
      };




      protected Typeface mTfRegular;
      protected Typeface mTfLight;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      setTitle("BMI Calculator");

      mTfRegular=Typeface.MONOSPACE;
      mTfLight = Typeface.MONOSPACE;
      radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
      selectedId = radioSexGroup.getCheckedRadioButtonId();
      radioSexButton = (RadioButton) findViewById(selectedId);
      mChart=(PieChart)findViewById(R.id.chart);
      age=(EditText) findViewById(R.id.age);
      height=(EditText) findViewById(R.id.height);
      weight=(EditText) findViewById(R.id.weight);
      // result=(TextView) findViewById(R.id.result);
      height_spinner=(Spinner) findViewById(R.id.height_spinner);
      weight_spinner=(Spinner) findViewById(R.id.weight_spinner);
      calculate=(Button) findViewById(R.id.calculate_button);

      mChart.setBackgroundColor(Color.rgb(76,0,0));
      // moveOffScreen();
      mChart.setUsePercentValues(false);
      mChart.getDescription().setEnabled(false);

      mChart.setDrawCenterText(true);
      mChart.setCenterTextTypeface(mTfLight);

      mChart.setCenterTextColor(Color.WHITE);
      mChart.setCenterTextSize(15f);
      mChart.setCenterText("BMI:"+bmi);

      mChart.setDrawHoleEnabled(true);
      mChart.setHoleColor(Color.rgb(76,0,0));

      mChart.setTransparentCircleColor(Color.rgb(76,0,0));
      mChart.setTransparentCircleAlpha(110);

      mChart.setHoleRadius(65f);
      mChart.setTransparentCircleRadius(68f);
      mChart.setDrawSliceText(false);
      mChart.setDrawEntryLabels(false);

      mChart.setRotationEnabled(false);
      mChart.setHighlightPerTapEnabled(false);

      mChart.setMaxAngle(270f); // HALF CHART
      mChart.setRotationAngle(135f);
      // mChart.setRotation(180f);


      // mChart.setCenterTextOffset(0, -20);

      setData(8, 80);

      mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
      mChart.getLegend().setEnabled(false);

      calculate.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      calculate();
      }
      });


      set_spinner();

      }

      private void setData(int count, float range) {

      ArrayList<PieEntry> values = new ArrayList<>();

      values.add(new PieEntry(10f,BMIcategory[0]));
      values.add(new PieEntry(15f,BMIcategory[1]));
      values.add(new PieEntry(8f,BMIcategory[2]));
      values.add(new PieEntry(12f,BMIcategory[3]));
      values.add(new PieEntry(5f,BMIcategory[4]));
      values.add(new PieEntry(5f,BMIcategory[5]));
      values.add(new PieEntry(15f,BMIcategory[6]));
      values.add(new PieEntry(10f,BMIcategory[7]));


      PieDataSet dataSet = new PieDataSet(values, "BMI Category");
      dataSet.setSliceSpace(1f);
      dataSet.setSelectionShift(5f);

      // add many colors
      ArrayList<Integer> colors = new ArrayList<Integer>();

      for (int c : ColorTemplate.VORDIPLOM_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.JOYFUL_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.COLORFUL_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.LIBERTY_COLORS)
      colors.add(c);

      for (int c : ColorTemplate.PASTEL_COLORS)
      colors.add(c);

      colors.add(ColorTemplate.getHoloBlue());
      dataSet.setColors(colors);


      //dataSet.setSelectionShift(0f);

      PieData data = new PieData(dataSet);
      data.setDrawValues(false);

      mChart.setData(data);
      mChart.invalidate();
      }


      public void set_spinner(){

      ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.height_spinner_array, android.R.layout.simple_spinner_item);
      adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      height_spinner.setAdapter(adapter1);

      height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
      int a = parent.getSelectedItemPosition();
      spinner_height = height_spinner.getSelectedItem().toString();
      if (a == 0) {
      selected_item1="Meters";
      }
      if (a == 1) {
      selected_item1="Inches";

      }
      if(a==2)
      {
      selected_item1="Centimeters";
      }

      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      // Another interface callback
      }
      });


      ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.weight_spinner_array, android.R.layout.simple_spinner_item);
      adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      weight_spinner.setAdapter(adapter2);
      weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
      int b = parent.getSelectedItemPosition();
      spinner_weight = weight_spinner.getSelectedItem().toString();
      if(b==0)
      {
      selected_item2="Kgs";
      }
      if(b==1)
      {
      selected_item2="Lbs";
      }
      }
      @Override
      public void onNothingSelected(AdapterView<?> parent) {} } );


      }

      public void calculate() {

      float getHeight, getWeight;

      DecimalFormat df = new DecimalFormat("#0.00");
      // for height in m and weight in kgs

      if (height.getText().toString().equals("")) {
      getHeight = 0f;
      } else {
      getHeight = Float.parseFloat(height.getText().toString());
      }
      if (weight.getText().toString().equals("")) {
      getWeight = 0f;
      } else {
      getWeight = Float.parseFloat(weight.getText().toString());
      }
      if(height.getText().toString().equals("") || weight.getText().toString().equals("") || age.getText().toString().equals(""))
      {
      Toast.makeText(this,"Please Enter All the Values",Toast.LENGTH_SHORT).show();
      return;
      }

      if(selected_item1.equals("Meters"))
      {
      if(selected_item2.equals("Kgs"))
      {
      bmi = getWeight / (getHeight * getHeight);

      }
      if(selected_item2.equals("Lbs"))
      {
      bmi = (getWeight*0.453592f) / (getHeight * getHeight);
      }
      }
      if(selected_item1.equals("Inches"))
      {
      if(selected_item2.equals("Kgs"))
      {
      bmi = (getWeight*318.87517f) / (getHeight * getHeight);

      }
      if(selected_item2.equals("Lbs"))
      {
      bmi = (getWeight*703f) / (getHeight * getHeight);
      }

      }
      if(selected_item1.equals("Centimeters"))
      {
      if(selected_item2.equals("Kgs"))
      {
      bmi = (getWeight*10000) / (getHeight * getHeight);
      }
      if(selected_item2.equals("Lbs"))
      {
      bmi=(getWeight*0.453592f*10000) / (getHeight * getHeight);
      }
      }
      // result.setText("Your BMI is " + df.format((double)bmi));



      }


      @Override
      public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
      drawer.closeDrawer(GravityCompat.START);
      } else {
      super.onBackPressed();
      }
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
      return true;
      }

      return super.onOptionsItemSelected(item);
      }



      }


      One more thing i wanted to ask ,How do i decrease the width of the Slices of my Pie chart .There is a option to adjust the radius of the Inner circle ,but how do i adjust the width of the Slices .??



      Any Kind of Help is most Welcome .Thanks !!







      java android pie-chart mpandroidchart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '16 at 12:35









      Samarth KejriwalSamarth Kejriwal

      706921




      706921
























          1 Answer
          1






          active

          oldest

          votes


















          3














          You have asked two questions:




          how to set the text inside the center dynamically?




          Just call again setCenterText and then invalidate method of Chart:



          mChart.setCenterText("BMI:" + bmi);
          mChart.invalidate();





          How do i decrease the width of the Slices of my Pie chart




          There is a method of PieChart:



          setHoleRadius



          You are using it incorrectly. It's parameter should a percent of the `PieChart' radius. So the call:



          mChart.setHoleRadius(0.65f);


          means, the hole should take 65% of chart radius.






          share|improve this answer
























          • Got it done Sir .Thanks :)

            – Samarth Kejriwal
            Nov 21 '16 at 14:02











          • If the answer was useful, then mark as accepted and vote it.

            – R. Zagórski
            Nov 21 '16 at 14:03













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40704070%2fhow-to-add-text-dynamically-inside-the-mpandroid-pie-chart%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You have asked two questions:




          how to set the text inside the center dynamically?




          Just call again setCenterText and then invalidate method of Chart:



          mChart.setCenterText("BMI:" + bmi);
          mChart.invalidate();





          How do i decrease the width of the Slices of my Pie chart




          There is a method of PieChart:



          setHoleRadius



          You are using it incorrectly. It's parameter should a percent of the `PieChart' radius. So the call:



          mChart.setHoleRadius(0.65f);


          means, the hole should take 65% of chart radius.






          share|improve this answer
























          • Got it done Sir .Thanks :)

            – Samarth Kejriwal
            Nov 21 '16 at 14:02











          • If the answer was useful, then mark as accepted and vote it.

            – R. Zagórski
            Nov 21 '16 at 14:03


















          3














          You have asked two questions:




          how to set the text inside the center dynamically?




          Just call again setCenterText and then invalidate method of Chart:



          mChart.setCenterText("BMI:" + bmi);
          mChart.invalidate();





          How do i decrease the width of the Slices of my Pie chart




          There is a method of PieChart:



          setHoleRadius



          You are using it incorrectly. It's parameter should a percent of the `PieChart' radius. So the call:



          mChart.setHoleRadius(0.65f);


          means, the hole should take 65% of chart radius.






          share|improve this answer
























          • Got it done Sir .Thanks :)

            – Samarth Kejriwal
            Nov 21 '16 at 14:02











          • If the answer was useful, then mark as accepted and vote it.

            – R. Zagórski
            Nov 21 '16 at 14:03
















          3












          3








          3







          You have asked two questions:




          how to set the text inside the center dynamically?




          Just call again setCenterText and then invalidate method of Chart:



          mChart.setCenterText("BMI:" + bmi);
          mChart.invalidate();





          How do i decrease the width of the Slices of my Pie chart




          There is a method of PieChart:



          setHoleRadius



          You are using it incorrectly. It's parameter should a percent of the `PieChart' radius. So the call:



          mChart.setHoleRadius(0.65f);


          means, the hole should take 65% of chart radius.






          share|improve this answer













          You have asked two questions:




          how to set the text inside the center dynamically?




          Just call again setCenterText and then invalidate method of Chart:



          mChart.setCenterText("BMI:" + bmi);
          mChart.invalidate();





          How do i decrease the width of the Slices of my Pie chart




          There is a method of PieChart:



          setHoleRadius



          You are using it incorrectly. It's parameter should a percent of the `PieChart' radius. So the call:



          mChart.setHoleRadius(0.65f);


          means, the hole should take 65% of chart radius.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '16 at 9:38









          R. ZagórskiR. Zagórski

          13.8k23563




          13.8k23563













          • Got it done Sir .Thanks :)

            – Samarth Kejriwal
            Nov 21 '16 at 14:02











          • If the answer was useful, then mark as accepted and vote it.

            – R. Zagórski
            Nov 21 '16 at 14:03





















          • Got it done Sir .Thanks :)

            – Samarth Kejriwal
            Nov 21 '16 at 14:02











          • If the answer was useful, then mark as accepted and vote it.

            – R. Zagórski
            Nov 21 '16 at 14:03



















          Got it done Sir .Thanks :)

          – Samarth Kejriwal
          Nov 21 '16 at 14:02





          Got it done Sir .Thanks :)

          – Samarth Kejriwal
          Nov 21 '16 at 14:02













          If the answer was useful, then mark as accepted and vote it.

          – R. Zagórski
          Nov 21 '16 at 14:03







          If the answer was useful, then mark as accepted and vote it.

          – R. Zagórski
          Nov 21 '16 at 14:03




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40704070%2fhow-to-add-text-dynamically-inside-the-mpandroid-pie-chart%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas