Note: TextView is similar to Label in other languages. For example it is similar to JLable in Java Swing package
public class TextView
extends View` implements ViewTreeObserver.OnPreDrawListener
As TextView is subclass of the View therefore can be included in other ViewGroup or as the content view of the Activity Class.
TextView can be declared in Layout XML file or using JAVA as programmatically.
Creating TextView Directely Using XML
<TextView android:id="@+id/simpleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Creating a TextView programatically in JAVA.
We can also create TextView quite easily using JAVA, Code snippet for this given belowRelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); // Create a TextView programmatically. TextView tv = new TextView(getApplicationContext()); // Create a LayoutParams for TextView LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, // Width of TextView LayoutParams.WRAP_CONTENT); // Height of TextView // Apply the layout parameters to TextView widget tv.setLayoutParams(lp); // Set text to display in TextView tv.setText("This is a sample TextView..."); // Set a text color for TextView text tv.setTextColor(Color.parseColor("#ff0000")); // Add newly created TextView to parent view group (RelativeLayout) rl.addView(tv);
Above code snippet creates a TextView and adds to RelativeLayout. This code Snippet required following Layout XML file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="@android:color/white" > </RelativeLayout>
Creating TextView Using Android Studio.
It is quite simple to create a TextView using Android Studio by simply dragging and dropping the TextView Widget on Editor. It simply adds the TextView to UI and also creates the required XML code for this TextView in Layout XML file.As shown in below Screen
Some of the Important Attributes of the TextView
1. id: id is an attribute used to uniquely identify a text view. Below is the example code in which we set the id of a text view.<TextView android:id="@+id/simpleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
In above XML code the simpleTextView is the name or id given to the TextView. This is an important attribute. This TextView is accessed and manipulated programatically using this id attribute.
For example below Java Code access and set the text in the simpleTextView
TextView textView = (TextView) findViewById(R.id.textView); textView.setText("Naveed Khan"); //set text for text view
2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.
Below is the example code with explanation included in which we set the center_horizontal gravity for text of a TextView.
<TextView android:id="@+id/simpleTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Naveed Khan" android:textSize="20sp" android:gravity="center_horizontal"/> <!--center horizontal gravity-->
3. text: text attribute is used to set the text in a text view. We can set the text in xml as well as in the java class.
Below is the example code with explanation included in which we set the text “Naveed Khan” in a text view.
<TextView android:id="@+id/simpleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginBottom="240dp" android:layout_marginTop="8dp" android:text="Naveed Khan" android:textSize="25sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /><!--Display Text in Center->
Similary there are number of other attributes exists for the TextView which may include textColor, textSize,Padding,Background,drawableLeft etc