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:-
1 2 3 4 |
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sample Text" /> |
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:-
1 2 |
TextView textView = (TextView) findViewById(R.id.textView); textView.setText("Changing text"); |
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:-
For creating the above App, the Full Java Code along with the XML layout is shown below:-
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.view.View; public class TextView extends Activity { private android.widget.TextView greetingView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text_view); greetingView = findViewById(R.id.textvie); } public void greet(View v) { greetingView.setText(R.string.hello); } } |
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TextView"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginBottom="84dp" android:onClick="greet" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/greeting" /> <TextView android:id="@+id/textvie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="32dp" android:layout_marginTop="32dp" android:textSize="28sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
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.