Search code examples
androidtextviewtypeface

how to parse string value in textview typeface?


I created simple Text view application. It's working fine, I need to parse string value in text view. because I have one XML file, my XML file contain some empty tag if I read that empty tag I need bold view in my text view ex: This is my tag <Strong> </Strong>If I read this tag I wish add bold view in my text view, so I am trying to add bold view in my text view.

xml file:

      <text>
<![CDATA[<p style="text-align: center;">
<span style="color:#FFFFFF;">
<strong>
<span style="font-size:30px;">
<span style="font-family:georgia,serif;">
<span style="color: rgb(0, 128, 128);">
welcome </span>
</span>
</span>

</strong>
</span></p>
]]>
                </text>

Code :

   NodeList Bold_value = tt.getElementsByTagName("strong");

            if (Bold_value.getLength() > 0) {
                //Style();
                bod = "Typeface.BOLD";
}

trying to add bold view in text view :

TextView txt = (TextView)findViewById(R.id.sample);

        txt.setBackgroundColor(Color.parseColor(color));

        txt.setTypeface(null, bod);

this line am trying to parse that value:

txt.setTypeface(null, bod);

Solution

  • setTypeFace requires a int as argument (not a String).

    so you can do this:

    txt.setTypeFace(txt.getTypeface(),Typeface.BOLD);
    

    or, as an alternative:

    int bod = Typeface.NORMAL;
    if (Bold_value.getLength() > 0) {
                //Style();
                bod = Typeface.BOLD;
    }
    txt.setTypeFace(txt.getTypeface(),bod);
    

    (and keep the current typeface instead of passing null)