Saturday, August 6, 2016

android - Display bulleted list on a TextView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d6dad2"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:gravity="start"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.BulletSpan;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TextView mTextView;

    SpannableStringBuilder mSSBuilder;
    String mText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTextView = (TextView) findViewById(R.id.tv);

        // Initialize a new String variable
        mText = "Test text to display bulleted list." +
                " \nRed \nGreen \nBlue \nYellow";

        // Initialize a new SpannableStringBuilder instance
        mSSBuilder = new SpannableStringBuilder(mText);

        // Generate bulleted list
        showBullet("Red");
        showBullet("Green");
        showBullet("Blue");
        showBullet("Yellow");

        // Display the spannable text to TextView
        mTextView.setText(mSSBuilder);
    }

    // Custom method to generate a bulleted list
    protected void showBullet(String textToBullet){
        // Initialize a new BulletSpan
        BulletSpan bulletSpan = new BulletSpan(
                30, // Gap width
                Color.RED // Color for Bullet
        );

        // Apply the bullet to the span
        mSSBuilder.setSpan(
                bulletSpan, // Span to add
                mText.indexOf(textToBullet), // Start of the span (inclusive)
                mText.indexOf(textToBullet) + 1,  // End of the span (exclusive)
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE // Do not extend the span when text add later
        );
    }
}