Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setText(.., ...) doesn't call "invalidate()" #65

Closed
jbiral opened this issue Aug 16, 2016 · 8 comments
Closed

setText(.., ...) doesn't call "invalidate()" #65

jbiral opened this issue Aug 16, 2016 · 8 comments
Labels

Comments

@jbiral
Copy link

jbiral commented Aug 16, 2016

When setting the text in code, invalidate() isn't called (just requestLayout()), which doesn't redraw the text. I would suggest calling both invalidate() and requestLayout(), as explained here: Why is requestLayout being called directly after invalidate?

Also, if I set a text with xml attributes (kswTextOn), the text will be centered. Now, if I update the text in code, the text will keep the position of the initial text and won't look great.

@kyleduo kyleduo added the bug label Aug 16, 2016
@kyleduo
Copy link
Owner

kyleduo commented Aug 16, 2016

Hey, these feature works fine on my phone in Demo project. Could you provide your layout to me?

@jbiral
Copy link
Author

jbiral commented Aug 16, 2016

It works fine in most of the standard scenarios, but in my case, I am adding a countdown to the switch and updating the text (which is the remaining time) with a CountdownTimer. The switch is part of an item in a RecyclerView, and requestLayout() doesn't make it redraw the view. I could obviously call notifyOnDataSetChanged() / notifyItemChanged(int i), but I don't want to do that in this case because it would crash the app if the user was to toggle the switch at the time the view is redrawn, or cause artifacts.

Basically, I want to refresh the text of the SwitchButton in a RecyclerView without requesting the View to redraw the item entirely, I would like the switch to update itself and it doesn't work because invalidate() is not called. I would add that it works fine with a standard TextView, because TextView's setText() calls invalidate() after updating the text. I made a workaround by calling invalidate() after setText(), but I think it belongs in the setText() method.

Concerning the second issue (text not centered if different from the initial one) here's a screenshot of what it looks like: I set the initial text o an empty string, and then updated the text to "0:00 left". You can see that it is not centered anymore, because the text keep the same initial positionas when it was an empty string.

switchbutton text cutoff

@kyleduo
Copy link
Owner

kyleduo commented Aug 17, 2016

I've fixed these problem and pushed to branch fix_65. Could you please check out that branch and see whether the problem has been solved? Waiting for your reply.

@jbiral
Copy link
Author

jbiral commented Aug 17, 2016

Thanks for the quick fix!

EDIT:

There is a one thing that seems to be still buggy:

  • When settings a very long off text, but a short on text, the on text is uncentered. I did that in code with setText("short", "very very long text");

@kyleduo
Copy link
Owner

kyleduo commented Aug 18, 2016

I've upload the library and you can use the newest version 1.4.4 with Gradle.

I changed the logic of positioning text and add a new attribute "kswAutoAdjustTextPosition". The new logic will make text looks centered when there are round corners. You can set this attribute to false when at special situation.

@jbiral
Copy link
Author

jbiral commented Aug 18, 2016

Hey, I thought everything was fine, but I played with a bigger track width and the text is again uncentered. You can reproduce it by, for example, doubling the kswBackMeasureRatio. In my case, the text is again cutoff, as you can see below:

big track switch

I tried playing with kswAutoAdjustTextPosition but it didn't help either in this case.

@kyleduo
Copy link
Owner

kyleduo commented Aug 23, 2016

I try to help but I can not reproduce your situation of text not centered. Could you paste your xml? This is what I got when I tried.
wechatimg1

@nkukday
Copy link

nkukday commented Sep 23, 2016

margins are recomputed only when text does not fit into the size of the current layout but we also need to recompute margins for text when size of the text is smaller than or equal to the size of the current layout to center it properly.

Solved with help and magic from @julienbiral

@kyleduo kyleduo closed this as completed Nov 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants