Tuesday, November 20, 2012

TextView - Bold or Italic - Android

Below is the code that makes a Textview bold or italic


strings.xml
<resources>
    <string name="username"><u><b><i>Peter</i></b></u></string>
</resources>


To set this String to your TextView, do this in your main.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/username"
/>



or In your JAVA code,
   TextView textView=new TextView(this);
    textView.setText(R.string.username);


If you're making use of Dynamic Text, the below code would help
  String tempString="SomeString";
  TextView text=(TextView)findViewById(R.id.text);
  SpannableString spanString = new SpannableString(tempString);
  spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
  spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
  spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
  text.setText(spanString);

No comments:

Post a Comment