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 MainActivity.java File instead of XML Layout file, to create a rich User Interface on “Run Time”.


 

Download
Source Code : TextView2.zip


 

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="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />			
        <!-- This TextView will get text from MainActivity.Java File -->


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Changing Text Size from Java File " />        
        <!-- Changing text Size from MainActivity.Java File --> -->


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Changing Text Color"
        android:textSize="20sp"/>
    	<!-- Changing TextView background color from MainAvtivity.Java -->
       
       
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Adding Text Background Color"
        android:textSize="20sp" />		
        <!-- Adding Background Color From MainActivity.java -->


    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text  Bold - Italic"
        android:textSize="20sp" />			
        <!-- Changing Text Bold - Italics from MainActivity.Java file  -->


    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />			
        <!-- Rendering HTML TAGS inside our TextView-->

        
	<TextView
	    android:id="@+id/textView7"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="Click me to update this Text" 
	    android:textSize="20sp"
	    android:clickable="true"
	    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   -->


                    
</LinearLayout>

Explaining above Code :

 

  • Over here in activity_main.xml we have just declared 7 Android TextView. Rest all things have been done in MainActivity.java 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 MainActivity.java, so we can add our desired code snippet over there.

 

 

2. MainActivity.java

package com.nestedif.textview2;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
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 ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//	Above Declared TextView are linked with TextView of activity_main.xml
		text_view1 = (TextView)findViewById(R.id.textView1);
		text_view2 = (TextView)findViewById(R.id.textView2);
		text_view3 = (TextView)findViewById(R.id.textView3);
		text_view4 = (TextView)findViewById(R.id.textView4);
		text_view5 = (TextView)findViewById(R.id.textView5);
		text_view6 = (TextView)findViewById(R.id.textView6);
		text_view7 = (TextView)findViewById(R.id.textView7);
		
		
		
		
		
		//	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) 
		text_view2.setTextSize(25);
		
		
		
		//	Adding Colour to TextView 
		text_view3.setTextColor(Color.parseColor("#AAFF0000"));
		
		
		
		//	Adding Background Colour to TextView 
		text_view4.setBackgroundColor(Color.parseColor("#22FF00FF"));
		
		
		
		//	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 
		//text_view7.setTextSize(25);
		//text_view7.setTextColor(Color.BLUE);
		
	}
	

}

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 Nestedif.com

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