Android TextView


  1. Static TextView Creation in Android. 

  2. Dynamic TextView Creation in Android. 

  3. Different Appearance of TextView Creation in Android. 


 
1.Static TextView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 
  
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code : 

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   xmlns:tools="http://schemas.android.com/tools"   
   android:layout_width="match_parent"   
   android:layout_height="match_parent" >   
   <TextView   
     android:id="@+id/textView1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="50dp"   
     android:clickable="true"   
     android:gravity="center"   
     android:lineSpacingMultiplier="1.5"   
     android:text="Static TextView By Hemanth Somaraju"   
     android:textColor="#0000FF"   
     android:textSize="20dp" />   
   <TextView   
     android:id="@+id/textView2"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/textView1"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="76dp"   
     android:clickable="true"   
     android:gravity="center"   
     android:lineSpacingMultiplier="1.5"   
     android:text="Java"   
     android:textColor="#FF9900"   
     android:textSize="20dp" />   
   <TextView   
     android:id="@+id/textView3"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/textView2"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="64dp"   
     android:clickable="true"   
     android:gravity="center"   
     android:lineSpacingMultiplier="1.5"   
     android:text="Android"   
     android:textColor="#FF0000"   
     android:textSize="20dp" />   
 </RelativeLayout>   
  
Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.view.View;   
 import android.widget.TextView;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
 TextView tv1, tv2, tv3;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     tv1 = (TextView) findViewById(R.id.textView1);   
     tv2 = (TextView) findViewById(R.id.textView2);   
     tv3 = (TextView) findViewById(R.id.textView3);   
     tv1.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Static TextView Clicked",   
 Toast.LENGTH_SHORT).show();   
 }   
 });   
     tv2.setOnClickListener(new View.OnClickListener() {   
    public void onClick(View v) {   
    // TODO Auto-generated method stub   
    Toast.makeText(getBaseContext(), "Java Clicked",   
    Toast.LENGTH_SHORT).show();   
    }   
    });   
     tv2.setOnLongClickListener(new View.OnLongClickListener() {   
 public boolean onLongClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Java Long Clicked",   
 Toast.LENGTH_SHORT).show();   
 return false;   
 }   
 });   
     tv3.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Android Clicked",   
 Toast.LENGTH_SHORT).show();   
 }   
 });   
     tv3.setOnLongClickListener(new View.OnLongClickListener() {   
 public boolean onLongClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Android Long Clicked",   
 Toast.LENGTH_SHORT).show();   
 return true;   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   
  
Step 4 : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="18" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       android:label="@string/app_name" >   
       <intent-filter>   
         <action android:name="android.intent.action.MAIN" />   
         <category android:name="android.intent.category.LAUNCHER" />   
       </intent-filter>   
     </activity>   
   </application>   
 </manifest>   
  
Step 5 : Our output will be like this : 
  
Image Image 
Image 
  
  
  
  
2.Dynamic TextView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 
  
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code : 

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   xmlns:tools="http://schemas.android.com/tools"   
   android:id="@+id/rl"   
   android:layout_width="match_parent"   
   android:layout_height="match_parent" >   
 </RelativeLayout>   
  
Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.graphics.Color;   
 import android.view.Menu;   
 import android.view.View;   
 import android.view.ViewGroup.LayoutParams;   
 import android.widget.RelativeLayout;   
 import android.widget.TextView;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
 RelativeLayout rl;   
 TextView tv1, tv2, tv3;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     rl = (RelativeLayout) findViewById(R.id.rl);   
     tv1 = new TextView (MainActivity.this);   
     tv2 = new TextView (MainActivity.this);   
     tv3 = new TextView (MainActivity.this);   
     tv1.setText("Dynamic TextView");   
     tv2.setText("Java");   
     tv3.setText("Android By Hemanth Somaraju");   
     tv1.setTextColor(Color.RED);   
     tv2.setTextColor(Color.MAGENTA);   
     tv3.setTextColor(Color.BLUE);   
     tv1.setTextSize(20);   
     tv2.setTextSize(20);   
     tv3.setTextSize(20);       
     RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);   
     params1.leftMargin=115;   
     params1.topMargin=120;   
     RelativeLayout.LayoutParams params2=new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);   
     params2.leftMargin=190;   
     params2.topMargin=190;   
     RelativeLayout.LayoutParams params3=new RelativeLayout.LayoutParams   
      ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);   
     params3.leftMargin=100;   
     params3.topMargin=260;   
     tv1.setLayoutParams(params1);   
     tv2.setLayoutParams(params2);   
     tv3.setLayoutParams(params3);       
     rl.addView(tv1);   
     rl.addView(tv2);   
     rl.addView(tv3);   
     tv1.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Dynamic TextView Clicked",   
 Toast.LENGTH_SHORT).show();   
 }   
 });   
     tv2.setOnClickListener(new View.OnClickListener() {   
    public void onClick(View v) {   
    // TODO Auto-generated method stub   
    Toast.makeText(getBaseContext(), "Java Clicked",   
    Toast.LENGTH_SHORT).show();   
    }   
    });   
     tv2.setOnLongClickListener(new View.OnLongClickListener() {   
 public boolean onLongClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Java Long Clicked",   
 Toast.LENGTH_SHORT).show();   
 return false;   
 }   
 });   
     tv3.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Android Clicked",   
 Toast.LENGTH_SHORT).show();   
 }   
 });   
     tv3.setOnLongClickListener(new View.OnLongClickListener() {   
 public boolean onLongClick(View v) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "Android Long Clicked",   
 Toast.LENGTH_SHORT).show();   
 return true;   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   
  
Step 4 : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="18" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       android:label="@string/app_name" >   
       <intent-filter>   
         <action android:name="android.intent.action.MAIN" />   
         <category android:name="android.intent.category.LAUNCHER" />   
       </intent-filter>   
     </activity>   
   </application>   
 </manifest>   
  
Step 5 : Our output will be like this : 
  
Image 
 Image 
  
Image 
 Image 
  
  
  
  
3.Different Appearance of TextView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 
  
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code : 

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   xmlns:tools="http://schemas.android.com/tools"   
   android:layout_width="match_parent"   
   android:layout_height="match_parent" >   
   <TextView   
     android:id="@+id/textView1"   
     android:textColor="#FF0000"   
     android:textSize="20dp"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="80dp"   
     android:text="TextView Appearance" />   
   <TextView   
     android:id="@+id/textView2"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="150dp"   
     android:text="Kakinada"   
     android:textAppearance="?android:attr/textAppearanceLarge"   
     android:textColor="#0000FF" />   
   <TextView   
     android:id="@+id/textView3"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="200dp"   
     android:text="India"   
     android:textAppearance="?android:attr/textAppearanceMedium" />   
   <TextView   
     android:id="@+id/textView4"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="250dp"   
     android:text="Hemanth Somaraju"   
     android:textAppearance="?android:attr/textAppearanceSmall"   
     android:textColor="#FF00FF" />   
 </RelativeLayout>   
  
Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 public class MainActivity extends Activity {   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   


Step 4 : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="18" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       android:label="@string/app_name" >   
       <intent-filter>   
         <action android:name="android.intent.action.MAIN" />   
         <category android:name="android.intent.category.LAUNCHER" />   
       </intent-filter>   
     </activity>   
   </application>   
 </manifest>   
  
Step 5 : Our output will be like this : 
  
Image