create TextView and set properties - Android User Interface

Android examples for User Interface:TextView

Description

create TextView and set properties

Demo Code


//package com.java2s;

import android.content.Context;

import android.view.Gravity;

import android.widget.TableRow;
import android.widget.TextView;

public class Main {
    /**//  ww w.j  av a 2s .c  om
     * @fn public static TextView createTextView(Context context,String message,int size,int textColor,int backgroundColor)
     * @brief Creates a textview object.
     * @param context 
     * @param message Message to be displayed in the text view
     * @param size Text size
     * @param textColor Numerical representation of color. Use android.graphics.Color.rgb(red,green,blue)
     * @param backgroundColor Numerical representation of color. Use android.graphics.Color.rgb(red,green,blue)
     * @return Created TextView Object.
     */

    public static TextView createTextView(Context context, String message,
            int size, int textColor, int backgroundColor) {
        /// http://stackoverflow.com/questions/11504635/layout-margin-for-text-view-programmatically
        TextView textView = new TextView(context);
        TableRow.LayoutParams tvlp = new TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.MATCH_PARENT);
        textView.setLayoutParams(tvlp);
        tvlp.setMargins(2, 2, 2, 2);
        textView.setText(message);
        textView.setTextSize(size);
        textView.setBackgroundColor(backgroundColor);
        textView.setTextColor(textColor);
        /// http://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertical-in-a-textview-in-android
        textView.setGravity(Gravity.CENTER_VERTICAL
                | Gravity.CENTER_HORIZONTAL);

        return textView;
    }
}

Related Tutorials