Nikhil Wagh
Nikhil Wagh

Reputation: 1498

How to continously get values from a Edit Text in android

In my app I am trying to get data from the android device sensors and then trying to store them in a file. I have been able to retrieve data from sensors and output them successfully in the EditText but I am unable to get the data continuously from the EditText. While loop is significantly not at all useful here. And thread would create a copy of string "entry" but I need it outside, so I was trying to use a callable but am not sure if I am doing it right. Suggest something to do the same.

Here is what I want to do.

btn_start.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View view) {
            Callable callable = new Callable() {
                @Override
                public Object call() throws Exception {
                    while(!btn_stop.isPressed()) {
                        entry += acc_x.getText().toString() + "," +
                                acc_y.getText().toString() + "," +
                                acc_z.getText().toString() + "," +
                                gyro_x.getText().toString() + "," +
                                gyro_y.getText().toString() + "," +
                                gyro_z.getText().toString() + "," +
                                user_name.getText().toString() + "\n";
                    }
                    return entry;
                }
            };

            ExecutorService executorService = Executors.newFixedThreadPool(2);
            Future<String> stringFuture = executorService.submit(callable);
            try {
                entry = stringFuture.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    });

Here is the complete code if you need more reference.

Upvotes: 2

Views: 382

Answers (1)

humazed
humazed

Reputation: 76942

if you want to get the data continuously from the EditText.

editText.addTextChangedListener(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) {
//save it to file here or what ever you want
    }

    @Override
    public void afterTextChanged(Editable s) {}
});

Upvotes: 1

Related Questions