Kotlin TextView.text +=


Kotlin TextView.text +=



I want to keep the text in the text view and add 0 to it
i have tried txtCalc.text = "The text" + "0" and it doesn't work





idownvotedbecau.se/itsnotworking
– cricket_007
Jul 1 at 14:27





You might want to look at android data binding if you want to actually use modifiable view strings
– cricket_007
Jul 1 at 14:30




2 Answers
2



If you read the current text in the TextView, you'll get a CharSequence, which you'll have to turn into a string before concatenating anything to it:


TextView


CharSequence


textView.text = textView.text.toString() + "0"



Or you can just use the append method of TextView:


append


TextView


textView.append("0")



Or if you literally want to use +=, you can create your own extension on TextView:


+=


TextView


inline operator fun TextView.plusAssign(text: CharSequence) = append(text)

textView += "0"





thanks i have used textView.append("0") it's good but how about if i want to add 0 before the old text Like textView.text = "0" + textView.text
– Lezhar Ayman
Jul 1 at 15:17





You can still use the first code example I've shown you - either as is, or with string templates as shown in the other answer by @Willi Mentzel
– zsmb13
Jul 1 at 15:23



A CharSequence, which text is, does not define the + (plus operator).


text


+



Using a string template you can write it more concise anyway.



Note:toString() is called implicitly on text (CharSequence) which turns it into a String.


toString()


CharSequence


String


textView.text = "${textView.text}0"





Not Working Still the same Error image.ibb.co/eAx46d/Screenshot_from_2018_07_01_15_58_34.png
– Lezhar Ayman
Jul 1 at 15:03






image.ibb.co/eAx46d/Screenshot_from_2018_07_01_15_58_34.png
– Lezhar Ayman
Jul 1 at 15:05






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Moria Casán

How to make file upload 'Required' in Contact Form 7?

Quinn's Post Commonwealth War Graves Commission Cemetery