splitwise / TokenAutoComplete

Gmail style MultiAutoCompleteTextView for Android
Apache License 2.0
1.3k stars 383 forks source link

Limit the number of character in Token (or add "...") #179

Closed muhammad-naderi closed 5 years ago

muhammad-naderi commented 9 years ago

Hi,

great work on the library, Thanks !

here is a bug_report / request,

when the token's width is longer than the MultiAutoCompleteTextView's width, somehow the token gets cropped (Which is not a bad thing!) and then it gets duplicated in two lines. so either let me know how I can prevent the duplication, or how we can replace the string in the token with string.substring(0,LIMIT) + "..." or any other enhancement

in the first screenshot you can see both TAC s showing the tokens OK! screenshot_2015-11-09-15-30-10

here the right one increased in size, hence the left one gets smaller than the token (ONLY ONE TOKEN, IT HAS) and you can see how it gets duplicated screenshot_2015-11-09-15-30-17

same goes with the left one here

screenshot_2015-11-09-15-30-25

mgod commented 9 years ago

The duplication is definitely a bug. To avoid the extra long token, you just need to return a view from getViewForObject that isn't wider than the available space. Maybe add a maxWidth an appropriate details in your layout?

muhammad-naderi commented 9 years ago

Actually right now I am doing such a thing, but this results in the tokens gets small all the time, frankly it's not that beautiful, screenshot_2015-11-10-11-30-47

what I think would be great is for the token to get re-sized on-demand (while it's bigger than the available space.

mgod commented 9 years ago

I don't think doing a lot of work on the layout is going to do a much better job than just picking a reasonable token size maximum. If we ware going to do fully justified tokens, we'd also need to be able to expand them as well. If we're not, I'm not sure the result would be much better than if you set you max width to about 1/3 of the available space of the field.

mgod commented 9 years ago

More helpful note is that you can probably do this yourself if you want. In your subclass of TokenCompleteTextView you can use Layout.getDesiredWidth on getText and mod that by maxTextWidth. This will give you the amount of space left in the current line. If you subtract that from maxTextWidth, that's how much space your token can take and fit on the last line. You may need to account for the space characters between tokens and the prefix if you have one.

mgod commented 9 years ago

I could see adding a call that would give the amount of space left for tokens on the current line, which would make custom token sizing relatively straightforward. I just don't think adding a bunch of custom layout finagling code to an already tricky library is going to give good results.

mgod commented 9 years ago

I take everything back, this might be very simple.

mgod commented 9 years ago

can you take a look at https://github.com/splitwise/TokenAutoComplete/tree/width-experiment and let me know if it works for you?

muhammad-naderi commented 8 years ago

The only file that has been changed is link to File right? if yes, then No, the problem still exists and the tokens get duplicated

mgod commented 8 years ago

I'm terribly sorry, I failed to actually push the changes. You should see more changed now. This doesn't fix the duplicate issue, but does let you do custom handling for the width of tokens by overriding maxTokenWidth in your TokenCompleteTextView. Something like:

protected float maxTokenWidth() {
    float left = getTextLineWidthLeft();
    if (left < maxTextWidth() / 4) {
        return maxTextWidth();
    } else {
        return left;
    }
}
muhammad-naderi commented 8 years ago

to be completely honest I do not get how this can help me ! my problem is that I generate the TAC in run time, and also change the width while user focuses on the TAC, (in pictures you may see that first, two TACs have weight of 0.5, each, then when user focuses on each one, it gains weight from the other one and the weight distribution becomes 0.8 and 0.2 , and vice versa.

so if you help me out a little more, I can check what ever you want :)

mgod commented 8 years ago

Ah, I see. Sorry to misunderstand. I think live resizing and auto layout of the tokens on field resize is possible, and the code I've added in that branch is the start of it. I don't think this is something I'll be able to get in a comfortable place to add to the main library.

muhammad-naderi commented 8 years ago

No problem, and again thanks for your nice work.

back to the duplication bug, can I have an ETA on it, please?

mgod commented 8 years ago

Unfortunately, It's not something I can see a simple fix to, so I'm not sure when I'll get it done.

mgod commented 5 years ago

Version 3.0.0 cleaned up a lot of the token handling code. Can you try it and let me know if you're still seeing the duplication issue?