0% found this document useful (0 votes)
20 views6 pages

Android TextView Control Overview

The document discusses the TextView control in Android, describing its basic functionality, important attributes, and providing an example of how to implement a TextView in an Android application using XML layouts and Java code. Key attributes like text, textColor, gravity and fontFamily are described. An example demonstrates creating an app with a TextView, applying attributes in XML and accessing it in Java code.

Uploaded by

html backup
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Android TextView Control Overview

The document discusses the TextView control in Android, describing its basic functionality, important attributes, and providing an example of how to implement a TextView in an Android application using XML layouts and Java code. Key attributes like text, textColor, gravity and fontFamily are described. An example demonstrates creating an app with a TextView, applying attributes in XML and accessing it in Java code.

Uploaded by

html backup
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Android - TextView Control

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text
editor, however the basic class is configured to not allow editing.

TextView Attributes
Following are the important attributes related to TextView control. You can check Android official documentation
for complete list of attributes and related methods which you can use to change these attributes are run time.

/
[Link]. Attribute & Description

1
android:id

This is the ID which uniquely identifies the control.

2
android:capitalize

If set, specifies that this TextView has a textual input method and should automatically capitalize what
the user types.
Don't automatically capitalize anything - 0
Capitalize the first word of each sentence - 1
Capitalize the first letter of every word - 2
Capitalize every character - 3

3
android:cursorVisible

Makes the cursor visible (the default) or invisible. Default is false.

4
android:editable

If set to true, specifies that this TextView has an input method.

5
android:fontFamily
Font family (named by string) for the text.

6
android:gravity

Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.

7
android:hint

Hint text to display when the text is empty.

8
android:inputType

The type of data being placed in a text field. Phone, Date, Time, Number, Password etc.

9
android:maxHeight

Makes the TextView be at most this many pixels tall.

10
android:maxWidth

Makes the TextView be at most this many pixels wide.

11
android:minHeight

Makes the TextView be at least this many pixels tall. /


12
android:minWidth

Makes the TextView be at least this many pixels wide.

13
android:password

Whether the characters of the field are displayed as password dots instead of themselves. Possible
value either "true" or "false".

14
android:phoneNumber

If set, specifies that this TextView has a phone number input method. Possible value either "true" or
"false".

15
android:text

Text to display.

16
android:textAllCaps

Present the text in ALL CAPS. Possible value either "true" or "false".

17
android:textColor

Text color. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

18
android:textColorHighlight

Color of the text selection highlight.

19
android:textColorHint

Color of the hint text. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

20
android:textIsSelectable

Indicates that the content of a non-editable text can be selected. Possible value either "true" or "false".

21
android:textSize

Size of the text. Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp).

22
android:textStyle

Style (bold, italic, bolditalic) for the text. You can use or more of the following values separated by '|'.
normal - 0
bold - 1
italic - 2

/
23 android:typeface

Typeface (normal, sans, serif, monospace) for the text. You can use or more of the following values
separated by '|'.
normal - 0
sans - 1
serif - 2
monospace - 3

Example

This example will take you through simple steps to show how to create your own Android application using Linear
Layout and TextView.

Step Description

1 You will use Android studio to create an Android application and name it as demo under a package
[Link] as explained in the Hello World Example chapter.

2 Modify src/[Link] file to add necessary code .

2 Modify the default content of res/layout/activity_main.xml file to include Android UI control.

3 No need to change default string constants at [Link] file. Android studio takes care of default string
constants.

4 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/[Link]/[Link]. This file can
include each of the fundamental lifecycle methods.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

//--- text view---


TextView txtView = (TextView) findViewById([Link].text_id);
}
}

Following will be the content of res/layout/activity_main.xml file −

/
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
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>

Following will be the content of res/values/[Link] to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">demo</string>
</resources>

Following is the default content of [Link] −

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="[Link]
package="[Link]" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >

<activity
android:name="[Link]"
android:label="@string/app_name" >

<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>

</activity>
/
</application>
</manifest>

Let's try to run your demo application. I assume you had created your AVD while doing environment setup. To run

the app from Android studio, open one of your project's activity files and click Run icon from the toolbar.
Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it
will display following Emulator window −

Exercise

I will recommend to try above example with different attributes of TextView in Layout XML file as well at
programming time to have different look and feel of the TextView. Try to make it editable, change to font color, font
family, width, textSize etc and see the result. You can also try above example with multiple TextView controls in
one activity.

Common questions

Powered by AI

The 'android:textColor' attribute sets the default color of the text displayed in a TextView, while the 'android:textColorHighlight' attribute specifies the color of the text selection highlight. When text is selected, the highlight color is applied to the background of the selected text, typically changing the foreground text color to increase contrast. This visual feedback can impact user perception by highlighting certain text segments for emphasis and improving readability against different backgrounds .

Assigning a 'monospace' typeface to a TextView via the 'android:typeface' attribute results in each character taking equal horizontal space. This uniformity can improve readability for certain content types, such as code samples or tabular data, where alignment is crucial. However, it might not be suitable for regular text due to its rigid appearance .

To change the fontFamily of a TextView in 'res/layout/activity_main.xml', you would add or modify the attribute `android:fontFamily` with the desired font family name, for example: `android:fontFamily="sans-serif-light"`. Changing the fontFamily can enhance the application's UI by making text more aesthetically aligned with the overall design theme, improving readability, and contributing to consistent brand identity across the app .

The 'android:gravity' attribute specifies the alignment of text relative to the TextView's bounds when the text does not fill the entire view. It affects how text is positioned along the x- or y-axis (e.g., center, left, right, top, bottom). Combining 'android:gravity' with layout attributes like 'layout_width' and 'layout_height' enables precise control over text placement, ensuring that the UI layout adheres to design specifications and improves readability and aesthetics by aligning text in a visually pleasing position .

A developer might choose not to change the default string constants in 'string.xml' initially to maintain consistency and avoid introducing errors in text resources that are already adequately defined. This approach leverages Android Studio’s automation of managing default strings, which helps ensure that labels and text display correctly across different locales without extra manual intervention .

The recommended dimension type for specifying the text size in an Android TextView is 'sp' (scaled pixels). This unit is preferred because it scales based on the user's font size preference settings, ensuring consistent readability across different screen sizes and user settings .

The 'android:hint' attribute provides a hint text that is displayed when the TextView is empty. It serves as a guide or placeholder to indicate to users what input is expected or can be entered. This enhances user experience by offering context and reducing ambiguity when interacting with the text field .

Setting the 'android:textIsSelectable' attribute to true allows the content of a non-editable TextView to be selected by the user. This means users can highlight text for actions like copy, despite the TextView itself not supporting editing directly .

The attribute 'android:capitalize' in an Android TextView provides four options: it can either not capitalize anything (value 0), capitalize the first word of each sentence (value 1), capitalize the first letter of every word (value 2), or capitalize every character (value 3). These settings affect how user input is automatically formatted and displayed when using a textual input method .

Developers use dp (density-independent pixels) for 'android:layout_width' and 'android:layout_height' because dp units provide consistent sizing across various screen densities. This ensures that UI elements maintain their intended size and proportion irrespective of the device's screen resolution, resulting in a more predictable and adaptable interface .

You might also like