Skip to content Skip to sidebar Skip to footer

Calculating Percentages Value Of Edittext In Android

I have got the code to add the values of multiple edittext fields and display the total in one field. Now I need to include the percentage calculation of two more edittext fields a

Solution 1:

Suppose service_tax_et and penalty_et are new editTexts, don't add them to the array instead set focusChangeListener on them manually and then try like this:

private View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    float total = 0;
    for (int i=0 ; i<editTexts.length-1 ; i++) {
        try {
            total += Integer.valueOf(editTexts[i].getText().toString());
        }
        catch(Exception e) {}
    }

    try {
        float service_tax_perc = (Integer.valueOf(monthly_rent_et.getText().toString()) + Integer.valueOf(service_tax_et.getText().toString())) / 100;
        total += service_tax_perc;
    } catch (Exception e) {}

    try {
        float penalty_perc = (Integer.valueOf(monthly_rent_et.getText().toString()) + Integer.valueOf(penalty_et.getText().toString())) / 100;
        total += penalty_perc;
    } catch (Exception e) {}

    total_et.setText(total + "");
}};

Post a Comment for "Calculating Percentages Value Of Edittext In Android"