Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when user enters data into edittext immediately shows answer [closed]

How is it possible to detect if a character is inputted into an EditText which makes it from 0 to 1 characters long and then perform some action?

like image 930
user2019287 Avatar asked Jan 31 '13 12:01

user2019287


People also ask

How do I know if EditText is focused?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class.

How do you make EditText not editable and clickable?

Make EditText non editable in Android To use this just set android:inputType="none" and it will become non editable.

Which attribute of EditText controls the initial visibility of the view?

android:visibility This controls the initial visibility of the view.


1 Answers

Since you have a rather abstract question, let me attempt an equally generic answer:

In your onCreate(), declare and cast your EditText

EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(filterTextWatcher);

And then, outside the onCreate(), setup the filterTextWatcher like this:

private TextWatcher filterTextWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // DO THE CALCULATIONS HERE AND SHOW THE RESULT AS PER YOUR CALCULATIONS

        int radius = 0;
        radius = Integer.valueof(s.toString);
        double area = Math.PI * radius * radius;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

EDIT:

Updated code with possible calculation. (UNTESTED CODE: I JUST TYPED IT IN WITHOUT TESTING IT. MODIFY WHERE NECESSARY)

Read more about TextWatcher's here

And here are a few examples to get you started:

  1. http://www.android-ever.com/2012/06/android-edittext-textwatcher-example.html
  2. http://www.cybernetikz.com/blog/android-textwatcher-example/
  3. http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=22b00052-dad0-4e09-a07e-b74f115ab247
  4. http://myandroidsolutions.blogspot.in/2012/06/android-edittext-change-listener.html
like image 108
Siddharth Lele Avatar answered Oct 04 '22 14:10

Siddharth Lele