0

string.xml Here is my string

<string name="bullet_text"><font color="#38d98a">●</font>\t\tCucumber Detox Water (1 glass)\n<font color="#38d98a">●</font>\t\tSkimmed Milk (1 glass)\n<font color="#38d98a">●</font>\t\tPeas Poha (1.5 katori)</string>

When this string is used in xml it works perfectly but when using this string programmatically then it does not works

xml code

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bullet_text"
        android:textSize="20sp"
        android:layout_margin="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

Working Perfectly

enter image description here

Kotlin Code

 val textView:TextView = findViewById(R.id.textView)
 textView.text = getString(R.string.bullet_text)

Not Working Perfectly

enter image description here

3
  • try Like This. Commented Sep 16, 2021 at 12:18
  • it works but \n in string not working Commented Sep 16, 2021 at 12:36
  • now it works I replace \n to <br/> Commented Sep 16, 2021 at 12:50

3 Answers 3

1

Now It Works Answer is here thanks @ADM and @Rasheed for commenting help

strings.xml

<string name="bullet_text"><![CDATA[<font color="#38d98a">●</font>\t\tCucumber Detox Water (1 glass)<br/><font color="#38d98a">●</font>\t\tSkimmed Milk (1 glass)<br/><font color="#38d98a">●</font>\t\tPeas Poha (1.5 katori)]]></string>

Kotlin code

val textView: TextView = findViewById(R.id.textView)
        textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(getString(R.string.hello_worldRed), Html.FROM_HTML_MODE_COMPACT)
        } else {
            Html.fromHtml(getString(R.string.hello_worldRed))
        }

Output

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Please use HtmlCompat instead of Html to not do this check by yourself.
1

you can use SpannableString to set bullet before your string :

val string = SpannableString("Text with\nBullet point")
        string.setSpan(
            BulletSpan(40, Color.GREEN, 20),
            10,
            22,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        binding.textView.text = string

here is the result

enter image description here

Also see this->BulletSpan

UPDATE

if (Build.VERSION.SDK_INT >= 28) {

    val string = SpannableString("Text with\nBullet point")
    string.setSpan(
        BulletSpan(40, Color.GREEN, 20),
        10,
        22,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    binding.textView.text = string
}else {
        // your own code
    }

2 Comments

it does not work bellow API Level 28
@Yaqoob Bhatti now its ok
0
You can use custom style instead of string
<style name="CustomFontStyle">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:capitalize">characters</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">12pt</item>
    <item name="fontFamily">your font</item>
    <item name="android:textColor">#38d98a</item>/>
</style>

3 Comments

But in my scenario i need string
use <br/> if you want to break the string in two parts like
<string name="register"> Sign up <br/> Enjoy a new shopping life stack.com!! </string>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.