Android - API - CharSequence

CharSequence is a native Java Interface.
Its path in the package is android.content.Context.getText(resId)
The class Context extends Object.
We will see this with an Hello World! example.

1. CharSequence getText()

The Android API uses it to create a getText method.

Let's take an example.

The Java class:

// src/


import android.os.Bundle;
import android.widget.TextView;

public class BadprogActivity extends Activity {
       /** Called when the activity is first created. */
       public void onCreate(Bundle savedInstanceState) {
           TextView tv = new TextView(this);
           CharSequence myString = getText(R.string.hello_world_smile);

Then the XML file:

// res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, Bp1Activity!</string>
    <string name="app_name">Badprog</string>
    <string name="hello_world">Hello <i>World</i>!</string>
    <string name="hello_world_smile">Hello <i>World <b>:D</b></i></string>

The result is:

Hello World :D

We can see that the rich text styling is applied to the string.

It can be differentiated from the native String method getText(int resId) that retrieves a String but without any style.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.