Android TextView Control

0
528

A TextView is one of the UI controls which is assigned to display a text to the user. With this UI control, the end user also sometimes gets the option to edit the text. This UI control is generally considered as a complete text editor but the basic class does not allow the editing of the text.

Also Read: Android Styles and Themes

List of TextView Attributes
Take a look at the below table where we have tabulated all the major and important attributes of the TextView control. In case you wish to view the complete list of TextView attributes and the methods associated with them then you can check out the Android official document. The TextView associated methods can be used to change the value of these attributes at the runtime.

S.No. TextView Attributes Description
1 android:id * This ID refers to the uniquely identify the each UI control.
2 android:capitalize * This attribute allows the user to input the text but all the characters will be capitalized automatically.
* Automated capitalization won’t work – 0
* Capitalization is limited to the first letter of each sentence – 1
* Capitalization will be applied to the first letter of each word – 2
* Automatically capitalizes all the characters of the word – 3
3 android:cursorVisible * If the value is set to true, the cursor becomes invisible.
* By default, the value is set to false which means the cursor remains visible.
4 android:editable * If the value has been set to true then the input method is allowed.
5 android:fontFamily * It is used to set the font family for the provided text.
6 android:gravity * This attribute takes care of the alignment of the text w.r.t. view’s X-axis or Y-axis.
* This can be applied whenever the text size is smaller than the view.
7 android:hint * It displays a hint to enter the text when the text field is empty.
8 android:inputType * This attribute sets the type of data to enter. It can be date, time, password, contact number, etc.
9 android:minHeight * This attribute keeps the size of the TextView control at least this many pixels tall.
10 android:minWidth * This attribute keeps the size of the TextView control at least this many pixels wide.
11 android:maxHeight * This attribute keeps the size of the TextView control at most this many pixels tall.
12 android:maxWidth * This attribute keeps the size of the TextView control at most this many pixels wide.
13 android:password * This attribute makes sure to either keep the characters of the password as it is or in the form of periods (.).
* The value for this attribute can either be true or false.
14 android:phoneNumber * This attribute accepts only phone number.
* The value can either be true or false.
15 android:text * This attribute accepts a particular text as its value which can be then displayed on the screen.
16 android:textAllCaps * With this attribute, you can set the text to ALL CAPS.
* The value for this attribute can either be true or false.
17 android:textColor * This attribute sets the color of the text.
* The value can only be passed in the form of “#rgb”, “#argb”, “#rrggbb”, or “#aarrggbb”.
18 android:textColorHighlight * It sets the color for the selected text highlighter.
19 android:textColorHint * This attribute sets the color of the hint text.
* The color value can be in the form of “#rgb”, “#argb”, “#rrggbb”, or “#aarrggbb”.
20 android:textIsSelectable * The value of this attribute decides whether the non-editable text can be selected or not.
* It supports either of the two values, which are true and false.
21 android:textSize * This sets the size of the text.
* The recommended dimension type for this attribute is ‘sp’. For example, android:textSize=”20sp”. Here, sp stands for scaled pixels.
22 android:textStyle * It sets the style of the text.
* You can apply more than one style to a content by separating the values with ‘|’.
* It supports three values (normal, bold, and italic)
* Normal – 0
* Bold – 1
* Italic – 2
23 android:typeface * This attribute sets the typeface for the text.
* Separate the values by ‘|’ and you will be able to apply more than one typeface to the text.
* It supports four values, which are:
* Normal – 0
* Sans – 1
* Serif – 2
* Monospace – 3

 

For your better understanding, take a look at the following example. With the help of this example, you will get to know how to create an application by the use of Linear Layout and TextView control.

Step 1: We will be starting by creating an Android application on the Android Studio IDE. Name this application as demo and keep it under the com.example.demo package. This step has already been mentioned in the Hello World Example chapter.

Step 2: Open the main activity java file. You can easily find this file at src/com.example.demo/MainActivity.java path. The file already contains all the fundamental lifecycle callback methods. But we will be modifying this file with the required code.

package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      //— text view—
      TextView txtView = (TextView) findViewById(R.id.text_id);
   }
}

Step 3: After this, open the res/layout/activity_main.xml file. In here, we are required to add the code for the inclusion of Android UI control.

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
   xmlns:tools=”http://schemas.android.com/tools”
   android:layout_width=”match_parent”
   android:layout_height=”match_parent”
   android:paddingBottom=”@dimen/activity_vertical_margin”
   android:paddingLeft=”@dimen/activity_horizontal_margin”
   android:paddingRight=”@dimen/activity_horizontal_margin”
   android:paddingTop=”@dimen/activity_vertical_margin”
   tools:context=”.MainActivity” >

   <TextView
      android:id=”@+id/text_id”
      android:layout_width=”300dp”
      android:layout_height=”200dp”
      android:capitalize=”characters”
      android:text=”hello_world”
      android:textColor=”@android:color/holo_blue_dark”
      android:textColorHighlight=”@android:color/primary_text_dark”
      android:layout_centerVertical=”true”
      android:layout_alignParentEnd=”true”
      android:textSize=”50dp”/>
</RelativeLayout>

Step 4: Open the string.xml file. Now, along with the default content of the file, add the following code for defining the two new constants.

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <string name=”app_name”>demo</string>
</resources>

Step 5: Keep the default content of the AndroidManifest.xml file as it is.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.demo” >
   <application
      android:allowBackup=”true”
      android:icon=”@drawable/ic_launcher”
      android:label=”@string/app_name”
      android:supportsRtl=”true”
      android:theme=”@style/AppTheme” >     

      <activity
         android:name=”com.example.demo.MainActivity”
         android:label=”@string/app_name” >      
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>         
      </activity>
   </application>
</manifest>

Step 6: The final step is to run this newly created demo application. For this, you need an already created AVD (created during the environment setup). Now, to run this application, open any of your project’s activity files. Go to the toolbar and hit the Run button. The Studio will initiate the installation of the app on your AVD. If everything falls in right place then you will be displayed with the final output on the Emulator window.