Using Spannable text and set style : Text « 2D Graphics « Android






Using Spannable text and set style

   

package app.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
import android.text.util.Linkify;
import android.widget.EditText;
import android.widget.TextView;

public class Test extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tv =(TextView)this.findViewById(R.id.tv);
        tv.setAutoLinkMask(Linkify.ALL);
        tv.setText("asdf");

        TextView tv3 =(TextView)this.findViewById(R.id.tv3);
        tv3.setText("asdf",TextView.BufferType.SPANNABLE);
        Spannable spn = (Spannable) tv3.getText();
        spn.setSpan(new BackgroundColorSpan(Color.RED), 0, 7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spn.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        EditText et =(EditText)this.findViewById(R.id.et);
        et.setText("asdf");
        Spannable spn2 = (Spannable) et.getText();
        spn2.setSpan(new BackgroundColorSpan(Color.RED), 0, 7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spn2.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"  android:layout_height="fill_parent"
    >
<TextView   
    android:layout_width="fill_parent"  android:layout_height="wrap_content" 
    android:autoLink="web|map"
    android:text="Please visit www.androidbook.com for more help on using Android."
    android:minLines="5"
    android:typeface="serif"
    />
<TextView android:id="@+id/tv"
    android:layout_width="fill_parent"  android:layout_height="wrap_content" 
    />
<TextView android:id="@+id/tvStyled"
    android:layout_width="fill_parent"  android:layout_height="wrap_content"
    android:text="@+string/styledText"
    />
<TextView android:id="@+id/tv3"
    android:layout_width="fill_parent"  android:layout_height="wrap_content" 
    />
<EditText android:id="@+id/et"
    android:layout_width="fill_parent"  android:layout_height="wrap_content" 
    android:inputType="text|textAutoCorrect|textAutoComplete|textMultiLine"
    />
<Button android:id="@+id/button1"
  android:layout_width="wrap_content"  android:layout_height="wrap_content"
  android:text="@string/tv"
  />
<TextView android:id="@+id/errors"
    style="@style/ErrorText"
    android:text="There's trouble down at the mill."
    />
<TextView android:id="@+id/danger"
    style="@style/ErrorText.Danger"
    android:text="SERIOUSLY! THERE'S TROUBLE DOWN AT THE MILL!!"
    />
</LinearLayout>

//strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, ActivityMenu</string>
  <string name="app_name">HelloMenu</string>
  <string name="button1">button1</string>
  <string name="button2">button2</string>
    <string name="tv">I am a TextView</string>
    <string name="et">I am an EditText</string>
    <string name="actv">AutoComplete:</string>
    <string name="mactv">MultiAutoComplete:</string>  
</resources>

//styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="ErrorText">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#FF0000</item>
        <item name="android:typeface">monospace</item>
    </style>
    <style name="ErrorText.Danger"  >
        <item name="android:textStyle">bold</item>
    </style>
</resources>

   
    
    
  








Related examples in the same category

1.Set Paint Text size
2.Draw text
3.Text on a Path
4.Text Align
5.Measure Text
6.Get Text Width
7.Rotate and transformation