Search code examples
androidandroid-edittextlisteners

Copying text from one EditText box to another in the same Activity on the fly, character-by-character


I am building an application to convert Latitude and Longitudes between various formats. My layout is complete but my current challenge is this:

For the Degree values, as the user enters a value in EditText1, I want it to replicate in EditText2 as they are typing, real-time, character for character. I have beat myself up trying OnTouchListener, onKeyboardActionListener, etc.

Later, I will perform calculations on the Minutes, Seconds, and decimal portions as they are typed in. Since the Degree field does not require calculations I am just trying to replicate the users value across multiple EditText boxes for now.

SUMMARY: Capture the characters in EditText1 as they are typed Place EditText1 captured characters into EditText2 on the fly.

Any help would be greatly appreciated.

Mike Murphy


Solution

  • I haven't tested, but I found addTextChangedListener():

    EditText firstEditText = (EditText)findViewById(R.id.firstEditText);
    firstEditText.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s){
            String c = s.toString(); // read Content
            ((EditText)findViewById(R.id.secondEditText)).setText(c); // copy to #2
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){ }
        public void onTextChanged(CharSequence s, int start, int before, int count){ }
    });
    

    http://groups.google.com/group/android-developers/browse_thread/thread/eba1a2ea7d3a2828?fwc=1