Android create TextView programmatically


TextView tv1: change text size, color, background color; handle click and long click event.
tv2: center text horizontally and vertically; set bold or italic; set line space; set multiline.
tv3, tv4: two TextViews side by side, in one line.
tv5: make rounded corner TextView, and draw border.
tv6: make links in the TextView clickable.
  1. Android Create Text example

    //package com.example.view_textview;
    import android.app.Activity;
    import android.graphics.Color;
    import android.graphics.Typeface;
    import android.graphics.drawable.GradientDrawable;
    import android.os.Bundle;
    import android.text.util.Linkify;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            LinearLayout layout = new LinearLayout(this);
            // vertical orientation, vertical list of TextView
            layout.setOrientation(LinearLayout.VERTICAL);
            setContentView(layout, lp);
    
            // color, size, background color
            TextView tv1= new TextView(this);
            lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            tv1.setText("tv1: click me");
            tv1.setTextColor(Color.BLUE);
            tv1.setBackgroundColor(Color.LTGRAY);
            tv1.setTextSize(20.0f);
            layout.addView(tv1,lp);
            
            // add click or tap
            tv1.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    ((TextView)v).setText("tv1: clicked");
                }
            });
            tv1.setOnLongClickListener(new View.OnLongClickListener() {
                @Override public boolean onLongClick(View v) {
                    ((TextView)v).setText("tv1: long clicked");
                    return true;
                }
            });
    
            // Multiline, line space, padding, gravity, bold, italic
            TextView tv2= new TextView(this);
            lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    200);
            tv2.setText("t2: Text - Center + Padding\nsecond line");
            tv2.setSingleLine(false);
            tv2.setMaxLines(2);
            tv2.setLineSpacing(-6.0f, 1.0f);
            tv2.setTextColor(0xffffff00);
            tv2.setBackgroundColor(Color.GRAY);
            tv2.setTextSize(18.0f);
            // set text NORMAL, BOLD, ITALIC, BOLD_ITALIC
            tv2.setTypeface(null, Typeface.ITALIC);
            //center text horizontally and vertically
            tv2.setGravity(Gravity.CENTER);
            tv2.setPadding(0,5,0,25);
            layout.addView(tv2,lp);
    
            //* Two textviews side by side, in one line
            LinearLayout ll = new LinearLayout(this);
            lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.addView(ll, lp);
    
            TextView tv3= new TextView(this);
            lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            tv3.setText("tv3: text ...");
            tv3.setBackgroundColor(Color.LTGRAY);
            ll.addView(tv3,lp);
    
            TextView tv4= new TextView(this);
            lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            tv4.setText("tv4: text ...");
            tv4.setTextColor(Color.WHITE);
            tv4.setBackgroundColor(Color.BLACK);
            ll.addView(tv4,lp);
    
            // Rounded corner, corner radius, draw border
            TextView tv5= new TextView(this);
            RelativeLayout.LayoutParams rlp=new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.leftMargin=50;
            rlp.topMargin=50;
            tv5.setText("tv5: rounded corner, Border");
            tv5.setTextColor(Color.BLUE);
            tv5.setPadding(20,20,20,20);
            GradientDrawable shape = new GradientDrawable();
            shape.setCornerRadius(10f);
            shape.setColor(Color.LTGRAY);
            shape.setStroke(6, Color.RED);
            tv5.setBackground(shape);
            layout.addView(tv5,rlp);
    
            // Make links clickable
            TextView tv6= new TextView(this);
            rlp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.leftMargin=50;
            rlp.topMargin=50;
            tv6.setText("Website: www.yahoo.com\n\nTel:91234567");
            tv6.setTextColor(Color.BLUE);
            tv6.setBackgroundColor(Color.LTGRAY);
            Linkify.addLinks(tv6, Linkify.WEB_URLS|Linkify.PHONE_NUMBERS);
            layout.addView(tv6,rlp);
    
        }
    }