Open top menu



In this tutorial we will create the programmatically textview. then explain the text view properties how to set programmatic in Main_activity.

 hight & width.
 how to set backgound color?
 how to set textview padding?
 how to set text?
 how to text in capital?
 all these explain in this tutorial.


package com.androidbeginner.testcode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class example extends Activity {

       @SuppressLint("NewApi")
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.registration_form);
              RelativeLayout layout = (RelativeLayout) findViewById(R.id.text);

               

              // Create a LayoutParams for TextView
              LayoutParams params = new RelativeLayout.LayoutParams(
                           LayoutParams.WRAP_CONTENT, // Width of TextView
                           LayoutParams.WRAP_CONTENT); // Height of TextView
              TextView dal_subj = new TextView(this);
             
                  // you have set the text background color
              dal_subj.setBackgroundColor(getResources().getColor(R.color.Light_Gray));
                 // you have set the layout hight and width of textview
              dal_subj.setLayoutParams(params);
                // you have set the textview gravity           
              dal_subj.setGravity(Gravity.CENTER_VERTICAL);
               // all text in uppercase
              dal_subj.setAllCaps(true);
               // set the textview padding
              dal_subj.setPadding(10, 3, 10, 3);
               // set the text in textview
              dal_subj.setText("Your wellcome in Android Beginner point Tutorial");
              // add the textview in RelativeLayout
              layout.addView(dal_subj);

       }

}
Tagged

0 comments