Android TextView - Fill From StringBuilder

January 15, 2018 Oclemy Android TextView, Java StringBuilder 2 minutes, 16 seconds

android.widget.TextView is a class used to render texts.

java.lang.StringBuilder on the other hand allows for creation of modifiable string of characters. StringBuilder is the replacemnet for StringBuffer class for non-concurrent use.

In this example we'll see how to:

  • Create a StringBuilder with multiple items.
  • Render the StringBuilder items in a TextView line by line.

For this example we don't need any XML layout. Instead we create and set an android.view.View object as our contentView for our activity.

Let's go.

Classes in Java are normally grouped into packages. So we first specify the package for our MainActivity class.

package com.tutorials.hp.textviewstringbuilder;

We then define the class:

public class MainActivity{}

We'll then add several imports above the class:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

Then make the class derive from AppCompatActivity.

public class MainActivity extends AppCompatActivity {}

AppCompatActivity makes your activity backword compatible with older devices. To use it your app level build.gradle dependencies section must contain the following support library. Note the version can differ:

compile 'com.android.support:appcompat-v7:26.+'

We then override the onCreate() method inside our class. This is a lifecycle callback for android that gets raised when the activity is created. We'll do our stuff right here.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   }

Note that we have to call the super class onCreate() method as above and pass it the savedInstanceState.

Then we instantiate the StringBuilder class:

StringBuilder sb=new StringBuilder();

All these we do inside the onCreate() method.

Then append our data using method chaining. This is possible since each append() method returns an instance of the StringBuilder.

sb.append("Mercury \n").append("Venus \n").append("Earth \n").append("Mars \n").append("Jupiter \n").append("Saturn \n").append("Neptune\n").append("Uranus \n");

Then instantiate our TextView, passing in our Context object.

TextView planetsTxt=new TextView(this);

Then convert our StringBuilder to String using the toString() method so that we can display it in the TextView.

planetsTxt.setText(sb.toString());

Lastly we call the setContentView() method of the AppCompatActivity class. This method will set our TextView as the main view of our activity.

setContentView(planetsTxt);

Here's the full source code. Note we don't need an XML for this example.

package com.tutorials.hp.textviewstringbuilder;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StringBuilder sb=new StringBuilder();
        sb.append("Mercury \n").append("Venus \n").append("Earth \n").append("Mars \n")
        .append("Jupiter \n").append("Saturn \n").append("Neptune \n").append("Uranus \n");

        TextView planetsTxt=new TextView(this);
        planetsTxt.setText(sb.toString());
        setContentView(planetsTxt);
    }
}

Comments