Android TextView Control With Example

In Android, TextView is used to display text to a user because it is a user interface element. TextView can optionally allow them to edit it and base class is configured not allow for editing. TextView is a complete text editor. So, In this Android tutorial, we are going to discuss each and every aspect of Android TextView.

TextView has a parent class called View. Being a subclass of View, TextView control can be used in our Android Studio App GUI ( Graphical User Interface ) inside a ViewGroup, or as the content view of an activity.

 

android textview control

Android TextView Control

TextView Code IN XML:

The following code shows a use of TextView with an XML layout and code to modify the content of the text view:

In above example, TextView has an id attribute whose value is textview_id and layout_height and layout_width having the same value wrap_content which means it only covers the space equal to its text attribute text.

TextView Code IN JAVA:

The following code shows a use of TextView with a Java and code to modify the content of the TextView programmatically:

Now Let’s discuss Android TextView attributes one by one.

Android TextView Attributes

1. android:id 

This is the id attribute which uniquely identifies the TextView control. It is used for get and set the properties of TextView Control in JAVA class.

Below is the example code in which we set the id attribute of a TextView to a textview_id.


2. android:layout_width

This is the layout_width attribute which defines the width of the TextView. Its value can be wrap_content, match_parent and fill_parent.

Below is the example code in which we set the layout_width attribute of a TextView to a wrap_content.


3. android:layout_height

This is the layout_height attribute which defines the height of the TextView. Its value can be wrap_content, match_parent and fill_parent.

Below is the example code in which we set the layout_height attribute of a TextView to a wrap_content.


4. android:text

This is the text attribute which is used to display text to a  user. We can set text in XML as well as in a JAVA class.

Below is the example code in XML where we set the text “Codekila Android Tutorials” in a TextView Control in XML.

Android TextView Control

IN JAVA –

Let’s take an example in which we set the text of a TextView programmatically means in JAVA class.


5. android:textSize

 This is the textSize attribute which defines the size of the text and can be set in XML. We can set the textSize in sp (scale pixel) or either it can be in dp (density pixel). Recommended dimension type for text is “sp” for scaled-pixels (example: 12sp).

Let’s take an example in which we set the text size of a TextView in XML.

IN JAVA –

Let’s take an example in which we set the text size of a TextView programmatically means in JAVA class.


6. android:textColor

 This is the textColor attribute which defines the color of the text and can be set in XML. Color of textView value is in “#rgb”, “#ffff”

Let’s take an example in which we set the color of a TextView in XML.

TextView textColor Attribute

IN JAVA –

Let’s take an example in which we set the textColor of a TextView programmatically means in JAVA class.

Android TextView Example

Now, we take a full example of Android TextView Control in an android studio. Below is the screenshot of the android textview example which we are going to show you now step by step. In this example, we will create a textview and when we click on textview it’s text change.

TextView Example

Step 1: Open Android Studio and Create a new project and name it androidTextView.

Step 2: Open res >  layout > xml (or) activity_main.xml and add below mention code. Here we will create a textview in Relative Layout.

Step 3: Open app  > java  > package ( in my case com.codekila.androidtextview ) and open MainActivity.java and add the below mentioned code. Here we will change the text of TextView after the user clicks on TextView.

Step 4: Below is the default content of AndroidManifest.xml  which is created at the time of creating a new android studio project. −

Result:

Now run the app in Emulator and click on the textview. You will see text will change to “Codekila.com After Click”.

TextView Example Result

Conclusion:

Here we can see that how android TextView control used to display text on the user interface side.If you have any query related to  Android TextView Control With Example post feel free to comment on the comment box. Do not forget to share this post because someone else also needs this. And don’t forget to like our Facebook Page.

Also Read:

  1. Android Tutorial For Beginners
  2. Android EditText Control With Example

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *