Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Android, TextView
is a widely-used UI component for displaying text to the user. It's one of the fundamental building blocks for UI design in Android applications. Let's go through some of the basics and then delve into some advanced features and customizations.
A simple TextView
defined in an XML layout might look like:
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, Android!" />
Text: The android:text
attribute defines the text content to be displayed.
Text Size: android:textSize
determines the font size, e.g., 16sp
.
Text Color: android:textColor
sets the text color, e.g., @color/black
or #FF0000
.
Text Style and Typeface: You can style the text using android:textStyle
(e.g., bold
, italic
) and android:typeface
(e.g., monospace
).
Gravity: android:gravity
specifies the alignment of text inside the TextView, e.g., center
.
Padding and Margins: These can be set to control the spacing inside (android:padding*
) and outside (android:layout_margin*
) the TextView
.
To manipulate a TextView
programmatically:
val textView: TextView = findViewById(R.id.textView) textView.text = "Updated Text"
Ellipsize: If the text content is too long to fit into its container:
android:ellipsize="end" android:maxLines="1"
This will truncate the text and show an ellipsis at the end if it exceeds one line.
HTML Content: If you need to display a small subset of HTML in a TextView
:
textView.text = Html.fromHtml("<b>Bold</b> and <i>Italic</i> text")
This will display "Bold" in bold and "Italic" in italic.
Links: To make URLs in your text clickable:
android:autoLink="web" android:linksClickable="true"
Spannable: To style parts of your text differently within the same TextView
, you can use SpannableString
:
val spannable = SpannableString("Bold and Italic text") spannable.setSpan(StyleSpan(Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spannable.setSpan(StyleSpan(Typeface.ITALIC), 9, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) textView.text = spannable
Drawable: To display drawables (icons/images) alongside text:
android:drawableLeft="@drawable/icon" android:drawablePadding="8dp"
This will place the specified drawable to the left of the text content.
Shadow: To give your text a shadow:
android:shadowColor="#000" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2"
PrecomputedText: For large amounts of text, you can use PrecomputedText
to measure and lay out text on a background thread for improved performance.
Avoid Overdraw: If a TextView
has a background color and is placed over another solid color, it causes overdraw. Use tools like "Show Overdraw Areas" in Developer Options to identify and reduce overdraw.
In conclusion, while TextView
might seem simple initially, it's a powerful and flexible widget in Android that offers a wide range of possibilities. Proper understanding and usage can significantly enhance the text display and user experience in your app.
TextView Example in Android:
TextView
is a UI component used to display text in your Android app.<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, TextView!"/>
How to Use TextView in Android:
TextView
and set its text.val textView = findViewById<TextView>(R.id.textView) textView.text = "Hello, TextView!"
Styling TextView in Android:
TextView
using attributes like android:textColor
, android:textSize
, and android:fontFamily
.<TextView android:id="@+id/styledTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Styled TextView" android:textSize="18sp" android:textColor="#FF5733" android:fontFamily="sans-serif-medium"/>
Customizing TextView in Android:
android:background
, android:gravity
, and android:drawableLeft
.<TextView android:id="@+id/customTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom TextView" android:background="#EDEDED" android:gravity="center" android:drawableLeft="@drawable/ic_icon"/>
Android Multiline TextView:
TextView
using the android:inputType
attribute.<TextView android:id="@+id/multilineTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Multiline Text" android:inputType="textMultiLine"/>
TextView Text Formatting in Android:
SpannableString
and SpannableStringBuilder
.val spannable = SpannableStringBuilder("Bold and Italic") spannable.setSpan(StyleSpan(Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spannable.setSpan(StyleSpan(Typeface.ITALIC), 5, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) textView.text = spannable
Changing TextView Text Dynamically in Android:
TextView
dynamically in response to user actions or events.val textView = findViewById<TextView>(R.id.dynamicTextView) textView.text = "Initial Text" // Later in the code textView.text = "Updated Text"