Android TextView – 2 (Adding Properties from Java file)


You have learned adding properties of TextView in  Android TextView – 1. In this tutorial you will learn to add all these properties from File instead of XML Layout file, to create a rich User Interface on “Run Time”.


Source Code :


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) TextView2 which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/TextView2/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.



1. activity_main.xml : Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:textSize="20sp" />			
        <!-- This TextView will get text from MainActivity.Java File -->

        android:text="Changing Text Size from Java File " />        
        <!-- Changing text Size from MainActivity.Java File --> -->

        android:text="Changing Text Color"
    	<!-- Changing TextView background color from MainAvtivity.Java -->
        android:text="Adding Text Background Color"
        android:textSize="20sp" />		
        <!-- Adding Background Color From -->

        android:text="Text  Bold - Italic"
        android:textSize="20sp" />			
        <!-- Changing Text Bold - Italics from MainActivity.Java file  -->

        android:textSize="20sp" />			
        <!-- Rendering HTML TAGS inside our TextView-->

	    android:text="Click me to update this Text" 
	    android:onClick="onClick_handler_textView7" />			
		<!-- This TextView is made Clickable , try clicking on it from your Virtual Device (AVD) or tap on screen in mobile -->
		<!-- The Onclick_handler_textView7 is just a method name we can write any desired name, just it should match in java file where it is called   -->


Explaining above Code :


  • Over here in activity_main.xml we have just declared 7 Android TextView. Rest all things have been done in file, ” except adding onclick Property for 7th TextView “. So just explaining 7th TextView :
  • Line-66 We have made our TextView clickable which means it will work like an Anchor TAG of HTML. It will raise a click event when user clicks on it.
  • Line-67 This line declares a click Handler, which listens to TextView clicks. You can name this handler as desired but you should keep in mind that the Java file should contain a method with same name and we have to pass    ( View v )    as a parameter in Java file since we are working on UI thread. Now whenever user clicks on 7th TextView our onClick_handler_textView7 will be called from, so we can add our desired code snippet over there.




package com.nestedif.textview2;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.example.textview.R;

public class MainActivity extends Activity {

	//This declares 7 TextViews which we require in our Activity
	TextView text_view1 , text_view2 , text_view3 , text_view4 , text_view5 , text_view6 , text_view7 ;
	protected void onCreate(Bundle savedInstanceState) {
		//	Above Declared TextView are linked with TextView of activity_main.xml
		text_view1 = (TextView)findViewById(;
		text_view2 = (TextView)findViewById(;
		text_view3 = (TextView)findViewById(;
		text_view4 = (TextView)findViewById(;
		text_view5 = (TextView)findViewById(;
		text_view6 = (TextView)findViewById(;
		text_view7 = (TextView)findViewById(;
		//	Below Lines for Updating TextView 1 - 6 will be called when the Activity is created (i.e. on pageLoad) 
		//	Setting Text To our 1st TextView
		text_view1.setText("Text From Java File");
		//	This line sets font Size = 25 from our java file (This can also be done from XML layout file) 
		//	Adding Colour to TextView 
		//	Adding Background Colour to TextView 
		//	Changing TextView to Bold - Italic 
		text_view5.setTypeface(null , Typeface.BOLD_ITALIC);
		//text_view5.setTypeface(null , Typeface.BOLD);
		//text_view5.setTypeface(null , Typeface.ITALIC);
		//	this TextView is displayed using HTML, we can also include images ,change font type , etc.
		text_view6.setText( Html.fromHtml("<B><font color=#4FB9B1> HTML Formatted Text </font></B>") );

	//	We have created a method which will run only when we click our text_view7
	public void onClick_handler_textView7(View v)
		// Over Here we update old Text with new one when user clicks 7th TextView  
		text_view7.setText("New Text After Clicking.");
		//  We can also change font size , color , style , etc from here try removing below comments 


Explaining above Code :


  • Line-16 we have declared our TextView in java file, in similar way we declare variable identifiers.
  • Line 24-30 We have linked all our TextView declared in Line-16 of MainActivity with each TextView added in activity_main.xml(Layout File), so as to access their properties.
  • Line-41 Since our text_view1 has no text attached to it at this point, we are adding text to it using  property setText(),  syntax :   Textview_Name . setText(“Text String”);
  • Line-46 In similar way we can change Size of our TextView using textSize(), Syntax :  Textview_Name.setText(Size in integer);
  • Line-51 To add text color we use  setTextColor() & pass a Hexa-decimal color code using Color Parser. Syntax : Textview_Name.setTextColor(Color.parseColor(“#ααRRGGBB”));
  • Line-56 In very similar way to setting text color we can set text background. Its Syntax is as follows : Textview_Name . setBackgroundColor(Color.parseColor(“#ααRRGGBB”));
  • Line-61 To make our Text Italic or Bold we can use Textview_Name.setTypeface(null ,Typeface.BOLD_ITALIC ); Try out other two options by removing below comments.
  • Line-69 We can take benefit of  HTML by rendering HTML TAGS with TextView using Html.fromHtml()  property, Syntax : TextView_Name.setText(Html.fromHtml( HTML formatted Text));
  • Line-79 We have made our 7th TextView clickable, so as to listen to user clicks we have made handler method called onClick_handler_textView7 ( View v ) . To this method ” we have passed instance of  View Class  as an argument since we are working on a UI Thread. So whenever we click on 7th Textview this method will be called.
  • Line-82 Clicking on 7th TextView we intend to change its text, so we place  setText() method inside onClick_handler_textView7 method. We can perform various events on click it like Changing textColor, textBackground, textSize etc. By removing below comments you can check it out.



Final Output :

Android TextView Final Out-Put




Report Errors + Bugs & Become Insider for

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to you could help other peers.