androidandroid-tablelayoutaddtextchangedlisteneronfocuschangelistener

Update TextView after data is entered in edittext field 1 and 2


I am using a Tablelayout and I am adding 5 rows pragmatically. Each row has 4 columns, Textview description at pos 0, 2 edittext attempt & made views at pos 1 and 2 and finally a TextView pct at pos 3. When a user provides input in edit text attempt and made, only then I want to calculate madeET/attemptET * 100 and setText in textview pct.

Here is the current code so far. The desired result only happens on final input on row 6. I have tried addTextChangedListener on madeET as well as setOnFocusChangeListener

private void setTableRows(){
    String[] desc = new String[]{"Flat level putts","Uphill putts","Downhill putts","Left-to-right putts","Right-to-left putts"};
    int totalputts = 0;

    for (int i=0;i<desc.length;i++){
        TableRow tr = new TableRow(this);

        TextView descTV = new TextView(this);
        descTV.setText(desc[i]);
        descTV.setTag(i);
        descTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        descTV.setPadding(5, 0, 0, 0);
        tr.addView(descTV);

        final TextView pctMade = new TextView(this);
        pctMade.setGravity(Gravity.CENTER);
        pctMade.setTag("pct"+i);
        //pctMade.setText(String.valueOf(pctmadecalc));
        pctMade.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        pctMade.setPadding(5, 0, 0, 0);

        final EditText madeET = new EditText(this);
        madeET.setInputType(InputType.TYPE_CLASS_NUMBER);
        madeET.setTag("m"+i);
        madeET.setFilters(new InputFilter[]{new GolfAppUtils.InputFilterMinMax("0","10")});
        madeET.setGravity(Gravity.CENTER);
        madeET.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        madeET.setPadding(5, 0, 0, 0);
        final int finalI = i;
        madeET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                /*if(s.length() > 0){
                    double pctmadecalc = Integer.parseInt(s.toString()) / PUTTS_PER_CATEGORY;
                }*/
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                pctMade.setText(null);
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0) {
                    //calcPct(finalI);
                    //float pctmadecalc = (((float)(Integer.parseInt(s.toString()))) / ((float) PUTTS_PER_CATEGORY)) * 100;
                    /*NumberFormat df = DecimalFormat.getInstance();
                    df.setMinimumFractionDigits(2);
                    df.setMaximumFractionDigits(4);
                    df.setRoundingMode(RoundingMode.DOWN);*/
                    //Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
                    //pctMade.setText(String.valueOf(String.format("%.2f",pctmadecalc)));
                }
            }
        });

        madeET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                calcPct();
            }
        });


        EditText attemptET = new EditText(this);
        attemptET.setInputType(InputType.TYPE_CLASS_NUMBER);
        attemptET.setTag("a"+i);
        attemptET.setFilters(new InputFilter[]{new GolfAppUtils.InputFilterMinMax("0","10")});
        attemptET.setGravity(Gravity.CENTER);
        attemptET.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        attemptET.setPadding(5, 0, 0, 0);
        /*attemptET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                *//*if(s.length() > 0){
                    double pctmadecalc = Integer.parseInt(s.toString()) / PUTTS_PER_CATEGORY;
                }*//*
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                pctMade.setText(null);
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0) {
                    float pctmadecalc = (((float)(Integer.parseInt(s.toString()))) / ((float) PUTTS_PER_CATEGORY)) * 100;
                    *//*NumberFormat df = DecimalFormat.getInstance();
                    df.setMinimumFractionDigits(2);
                    df.setMaximumFractionDigits(4);
                    df.setRoundingMode(RoundingMode.DOWN);*//*
                    //Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
                    pctMade.setText(String.valueOf(String.format("%.2f",pctmadecalc)));
                }
            }
        });*/

        tr.addView(attemptET);
        tr.addView(madeET);
        tr.addView(pctMade);
        table.addView(tr);
    }
}

private void calcPct() {
    EditText attempt = null,made = null;
    TextView pct = null;
    /*TableRow row = (TableRow) table.getChildAt(pos);
    attempt = (EditText) row.findViewWithTag("a" + pos);
    made = (EditText) row.findViewWithTag("m" + pos);
    pct = (TextView) row.findViewWithTag("pct" + pos);*/
    for(int i = 1, j = table.getChildCount(); i < j; i++) { //i=0 is header
            TableRow row = (TableRow)table.getChildAt(i);
            attempt = (EditText) row.getChildAt(1);
            made = (EditText) row.getChildAt(2);
            pct = (TextView) row.getChildAt(3);
    }

    if (!TextUtils.isEmpty(attempt.getText())) {
        if (!TextUtils.isEmpty(made.getText())) {
            float pctmadecalc = ((float) Integer.parseInt(made.getText().toString()) /
                    ((float) Integer.parseInt(attempt.getText().toString()))) * 100;
            //Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
            pct.setText(String.valueOf(String.format("%.2f", pctmadecalc)));
        }
    }
}

Current Image

Thanks in advance for your help

RESOLUTION FROM RESPONSE BELOW

public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    pctMade.setText("");
                    float pctmadecalc = ((float) Integer.parseInt(madeET.getText().toString()) /
                            ((float) Integer.parseInt(attemptET.getText().toString()))) * 100;
                    //Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
                    pctMade.setText(String.valueOf(String.format("%.2f", pctmadecalc)));
                    //pctMade.setText(calcPct());
                } else {
                    pctMade.setText("");
                }
            }

fixed Code


Solution

  • It will be something like:

     @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    pctMade.setText("");
                    pctMade.setText(s);
    
                    //etc..
                }