0

I have three edittext values for which i am using textwatcher to update data. I want that if i enter any value in anyone of the edit text then according to that value it should calculate and then update the edittext in which i entered the value automatically and after that set the value in other specif edit text and save the final value in string if need to send to any server.

//My Code

public class Land_details extends AppCompatActivity {

    EditText land_acre_edit;
    EditText land_kanal_edit;
    EditText land_marla_edit;
    Button land_button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_land_details);
        land_acre_edit = findViewById(R.id.land_acre_edit);
        land_kanal_edit = findViewById(R.id.land_kanal_edit);
        land_marla_edit = findViewById(R.id.land_marla_edit);
        land_button = findViewById(R.id.save_land_detail);

land_acre_edit.addTextChangedListener(mytext_watcher);
land_marla_edit.addTextChangedListener(mytext_watcher);
land_kanal_edit.addTextChangedListener(mytext_watcher);
}


   private TextWatcher mytext_watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            try {
                String check_marla = land_marla_edit.getText().toString();
                String check_kanal = land_kanal_edit.getText().toString();
                String check_acre = land_acre_edit.getText().toString();

                if (check_marla != null) {
                    if ((Integer.valueOf(check_marla) > 8)) {

                        check_acre =(String.valueOf(Integer.valueOf(check_kanal) + (Integer.valueOf(land_marla) / 20)));
                        check_marla=( String.valueOf(Integer.valueOf(land_marla) % 20));

                        check_acre = String.valueOf(check_acre) + (Integer.valueOf(check_kanal) / 8);
                        check_kanal = String.valueOf(Integer.valueOf(check_kanal) % 8);


                        land_marla_edit.setText(check_marla);
                        land_kanal_edit.setText(check_kanal);
                        land_acre_edit.setText(check_acre);
                    }}

            }catch (NumberFormatException e){

            }


        }

        @Override
        public void afterTextChanged(Editable s) {


        }


    };

I have tried to set my try catch code in aftertextChanged but at first it was giving me numberformatexception as soon as i enter a text above my contion my app crash but after that i tried using try catch to catch a numberformatexception after that i am able to add number above my condition and even after a long wait nothing happen can any one explain where i am going wrong.

  • Do you really want this code to run after every character is entered or just when the user has finished entering text? An `onFocusChangeListener` might be more appropriate. – codebod May 21 '20 at 18:40
  • will it work if i tap anywhere on screen ?? – sunny phogat May 22 '20 at 03:37
  • I think so. Focus can be taken by any `View` that is focusable. You`d have to test. – codebod May 22 '20 at 06:45
  • sir the code worked fine and it solved my problem but i am facing only one problem that it updates the edittext only when i click on other edit text but on anywhere on the view !! – sunny phogat May 22 '20 at 06:52
  • Make sure you are testing for focus being lost, not gained, with `hasFocus == false`. Otherwise, yes you'll have to focus on an `EditText` to trigger your code. – codebod May 22 '20 at 07:06

0 Answers0