0

So, let's assume that I want to show a text such as : "Hello stranger ", using TextView. How would I do that? I realized that I cannot edit only a part of the text in TextView by changing textStyle attribute.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Huzo
  • 1,652
  • 1
  • 21
  • 52
  • 1
    Simply replace the string in your TextView. And use HTML.fromHTML to show the HTML formatted text. – Phantômaxx Aug 09 '18 at 12:02
  • @KlingKlang I am sorry, but I am relatively new to this. Can you please demonstrate it as an answer? Thanks. – Huzo Aug 09 '18 at 12:07
  • You already know how to use setText(), right? a quick search for `android html textview` will clarify all your doubts. It's very easy to achieve, don't worry. You won't waste a lot of time on this small detail. – Phantômaxx Aug 09 '18 at 12:10
  • Or... wait, I'll mark your answer as a duplicate, so you can take advantage of the answer in the linked post and we get rid of a duplicate question at the same time. – Phantômaxx Aug 09 '18 at 12:13
  • 1
    Possible duplicate of [How to display HTML in TextView?](https://stackoverflow.com/questions/2116162/how-to-display-html-in-textview) – Phantômaxx Aug 09 '18 at 12:13
  • 1
    `SpannableString` is also another option. Check [this](https://stackoverflow.com/a/22420351/9752602) – sneharc Aug 09 '18 at 12:23

3 Answers3

1

Don't bother with complexity.
Place a TextView with the uneditable text on the left of the EditText.
Change the margins and everything that makes it look like a unified view and you're done.

1

Use yourTextViewName.setText(Html.fromHtml(<b>Hello</b> <i>stranger</i>);

Reference here: https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String,%20int,%20android.text.Html.ImageGetter,%20android.text.Html.TagHandler)

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
1

You can define the string in string.xml like this <string name="text"><b>Hello</b><i>stranger</i></string> and call it in your TextView like android:text="@string/text". It will work for you.