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

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

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.