get Decorated TextView - Android User Interface

Android examples for User Interface:TextView

Description

get Decorated TextView

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.widget.TextView;

public class Main {
    public static TextView getDecoratedTextView(Context c,
            CharSequence text, boolean centered, boolean bold,
            boolean italic) {
        if (centered) {
            return getCenteredTextView(c,
                    getDecoratedText(text, bold, italic));

        }//from   w ww .  j a  v  a  2  s.  co  m
        return getTextView(c, getDecoratedText(text, bold, italic));
    }

    public static TextView getCenteredTextView(Context c, CharSequence text) {
        TextView t = getTextView(c, text);
        t.setGravity(Gravity.CENTER);
        return t;
    }

    public static SpannableString getDecoratedText(CharSequence text,
            boolean bold, boolean italic) {
        SpannableString s = new SpannableString(text);
        if (bold && italic) {
            s.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, s.length(), 0);
        } else if (bold) {
            s.setSpan(new StyleSpan(Typeface.BOLD), 0, s.length(), 0);
        } else if (italic) {
            s.setSpan(new StyleSpan(Typeface.ITALIC), 0, s.length(), 0);
        }

        return s;
    }

    public static TextView getTextView(Context c, CharSequence text) {
        TextView t = new TextView(c);
        t.setText(text);
        return t;
    }
}

Related Tutorials