Text View Code Snippet for Android Studio

 

Welcome. In this tutorial you will learn about Text View in Android Studio.

Text View Example for Android Studio.

A Text View is a UI element that is used to display Texts inside our android app. It is the most basic type of View element that works as a text editor in Android Studio.

Inserting a Text View using XML

To insert a text view, just add the following lines in your activity_main.xml file:-

The text view is a subclass of it’s parent type View. You can customize the contents of a text View by its various other properties which android studio has provided to us. For example:- You can change background, add padding, margin, gravity like center, left, right, etc. You can also change textSize, textColor, textStyle of the text.

 

Changing Text in Text View using java

To change the text of your text view from anywhere in your java code, use the following code:-

The textView.setText attribute is used to change text anywhere from inside the java file, where textView is a variable of type TextView.

 

An example of Text View is shown in the image below. When the button is presses, a text will be shown in the Text View:-

Text View Example code for Android Studio

 

For creating the above App, the Full Java Code along with the XML layout  is shown below:-

JAVA (MainActivity.java)

 

 

XML (activity_main.xml)

 

 

You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with various other settings on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your UI more interesting.

For a more detailed guide, you can also visit android’s official guide below:-

https://developer.android.com/reference/android/widget/TextView

Comment down below if you are facing any problems.

 

Also Read:-

Add a Comment

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