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/com.badprog.mobile/BadprogActivity.java

package com.badprog.mobile;

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

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

Then the XML file:

// res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <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>
</resources>

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.