tags:

views:

153

answers:

1

Hello,

I was wondering what the best way to do a live character count of an edit-text box is in Android. I was looking at this but I couldn't seem to make any sense of it.

To describe the problem, I have an EditText and I'm trying to limit the characters to 150. I can do this with an input filter, however I want to show right below the text box the number of characters a user has entered(Almost like stack overflow is doing right now).

If someone could write a small snippet of example code or point me in the right direction I'd appreciate it a lot.

Thanks in advance.

A: 

Try doing something like this:

  EditText et;
  et.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      /* Do something interesting with the length */
      v.getText().length();
      return false;
    }
  });

setOnEditorActionListener

Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user.

Macarse
Thanks, i'll try this out and report the feedback.
Taylor Perkins
I tried to get [this](http://pastebin.com/YtPZzh9P) working but to no avail,
Taylor Perkins
what's the output of that? No log shown?
Macarse
The output is nothing. No log shown because the Log didn't do anything.Again [here](http://pastebin.com/BMAzVQHz "here") is the full pastebin.
Taylor Perkins
Someone should take a look at this... Its a very interesting question. I tried his pastebin code and it didn't work as well.
hwrdprkns
@hwrdprkns: The code on pastebin depends on twitter4j, facebook sdk, taggstr. It would be nice to have a code that doesn't depend on that to test it out.
Macarse
@Macarse, It shouldn't depend on what are the imports for that listener to work. That listener is independent of any other code in that pastebin. As long as the user can type (which I'm assuming they can since its a questions about editboxes) that listener should be active on every keystroke.
hwrdprkns