Search code examples
javaandroidlistenerandroid-textwatcher

Can I use The same TextWatcher on different EditText widgets


I have a form. I want to display a calculation in "live time" when the user enters different values into some int fields. I made my Activity implement the TextWatcher interface and set a listener on 3 different EditText fields but it appears the Textwatcher only detects the first EditText declared in the Activity's code.

You can see from the code below, I'm trying to grabe a few fields, convert them to ints and display the output in an EditText field at the bottom of the form. Where am I going wrong? Do I need to implement a textWatcher individually on all of the EditText fields involved? That would be a rather verbose solution making my code a lot longer than I would like

public class NewStageFormActivity extends AppCompatActivity implements TextWatcher{

    Context mContext;

    EditText mStageName, mPaperTargets, mHitsPerTarget, mSteelTargets, mSteelNPMs, mOutput;
    Spinner mScoringType, mStrings;
    CheckBox mNoShoots, mNPMs;
    Button mSaveButton;
    Match mGlobalMatch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stage_form);

        mContext = this;
        mGlobalMatch = GlobalMatch.getMatch();

        mStageName = (EditText)findViewById(R.id.stage_name_et);
        mPaperTargets = (EditText)findViewById(R.id.paper_targets_et);
        mHitsPerTarget = (EditText)findViewById(R.id.hits_per_target_et);
        mSteelTargets = (EditText)findViewById(R.id.steel_targets_et);
        mSteelNPMs = (EditText)findViewById(R.id.steel_npm_et);
        mScoringType = (Spinner)findViewById(R.id.scoring_type_spinner);
        mStrings = (Spinner)findViewById(R.id.strings_spinner);
        mNoShoots = (CheckBox)findViewById(R.id.no_shoots_cb);
        mNPMs = (CheckBox)findViewById(R.id.npm_cb);
        mSaveButton = (Button)findViewById(R.id.save_button);
        mOutput = (EditText)findViewById(R.id.output_et);

        // paper * hitsPer + steel
        mPaperTargets.addTextChangedListener(this);
        mSteelTargets.addTextChangedListener(this);
        mSteelTargets.addTextChangedListener(this);

        mSaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mStageName.getText().toString().equals("") || mPaperTargets.getText().toString().equals("") ||
                        mHitsPerTarget.getText().toString().equals("") || mSteelTargets.getText().toString().equals("") ||
                        mSteelNPMs.getText().toString().equals("")){
                    Toast.makeText(mContext, "You must fill in all form fields", Toast.LENGTH_SHORT).show();
                } else {
                    String name = mStageName.getText().toString();
                    String type = mScoringType.getSelectedItem().toString();
                    int strings = Integer.valueOf(mStrings.getSelectedItem().toString());
                    int paperTargets = Integer.valueOf(mPaperTargets.getText().toString());
                    int hitsPerTarget = Integer.valueOf(mHitsPerTarget.getText().toString());
                    boolean noShoots;
                    boolean npms;
                    if(mNoShoots.isChecked()){
                        noShoots = true;
                    } else {
                        noShoots = false;
                    }
                    if(mNPMs.isChecked()){
                        npms = true;
                    } else {
                        npms = false;
                    }
                    int steelTargets = Integer.valueOf(mSteelTargets.getText().toString());
                    int steelNPMs = Integer.valueOf(mSteelNPMs.getText().toString());

                    MatchStage matchStage = new MatchStage(name, type, strings, paperTargets, hitsPerTarget,
                            noShoots, npms, steelTargets, steelNPMs);

                    mGlobalMatch.getStages().add(matchStage);

                    String jsonString = new Gson().toJson(mGlobalMatch);
                    MatchHelper.updateFile(mContext, MatchHelper.createFileName(mGlobalMatch.getMatchId()),
                            jsonString);

                    Intent intent = new Intent(mContext, StagesListActivity.class);
                    startActivity(intent);
                }
            }
        });


    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        int paper = Integer.valueOf(mPaperTargets.getText().toString());
        int hitsPer = Integer.valueOf(mHitsPerTarget.getText().toString());
        int steel = Integer.valueOf(mSteelTargets.getText().toString());

        int minRound = (paper * hitsPer) + steel;
        int points = minRound * 5;

        mOutput.setText("Minimum rounds: " + (minRound) + "\t\t Points: " + points);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

Solution

  • In my experience I've had issues with using the same TextWatcher on multiple EditText. You could get around this by creating a method that produces a new instance of TextWatcher for each EditText so your code isn't all verbose. Try something like this:

    Get rid of implements TextWatcher for your Activity and it's associated methods. Implement a method createTextWatcher():

    private TextWatcher createTextWatcher() {
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // Copy your original code
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // Copy your original code
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
                // Copy your original code
            }
        };   
    }
    

    Now to use it like this:

    mPaperTargets.addTextChangedListener(createTextWatcher());
    mSteelTargets.addTextChangedListener(createTextWatcher());
    

    The most up-voted answer on @Natan Felipe's link also demonstrates using one instance of TextWatcher per EditText.