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

Working With the TextView in Android

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.

Basics:

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!" />

Key Properties:

  1. Text: The android:text attribute defines the text content to be displayed.

  2. Text Size: android:textSize determines the font size, e.g., 16sp.

  3. Text Color: android:textColor sets the text color, e.g., @color/black or #FF0000.

  4. Text Style and Typeface: You can style the text using android:textStyle (e.g., bold, italic) and android:typeface (e.g., monospace).

  5. Gravity: android:gravity specifies the alignment of text inside the TextView, e.g., center.

  6. Padding and Margins: These can be set to control the spacing inside (android:padding*) and outside (android:layout_margin*) the TextView.

Working Programmatically:

To manipulate a TextView programmatically:

val textView: TextView = findViewById(R.id.textView)
textView.text = "Updated Text"

Advanced Features:

  1. 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.

  2. 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.

  3. Links: To make URLs in your text clickable:

    android:autoLink="web"
    android:linksClickable="true"
    
  4. 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
    
  5. 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.

  6. Shadow: To give your text a shadow:

    android:shadowColor="#000"
    android:shadowDx="4"
    android:shadowDy="4"
    android:shadowRadius="2"
    

Performance Considerations:

  1. PrecomputedText: For large amounts of text, you can use PrecomputedText to measure and lay out text on a background thread for improved performance.

  2. 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.

  1. TextView Example in Android:

    • TextView is a UI component used to display text in your Android app.
    • Example in XML layout:
      <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Hello, TextView!"/>
      
  2. How to Use TextView in Android:

    • In your activity or fragment, obtain reference to TextView and set its text.
    • Example in Kotlin:
      val textView = findViewById<TextView>(R.id.textView)
      textView.text = "Hello, TextView!"
      
  3. Styling TextView in Android:

    • Style TextView using attributes like android:textColor, android:textSize, and android:fontFamily.
    • Example:
      <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"/>
      
  4. Customizing TextView in Android:

    • Customize appearance using attributes such as android:background, android:gravity, and android:drawableLeft.
    • Example:
      <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"/>
      
  5. Android Multiline TextView:

    • Enable multiline text in TextView using the android:inputType attribute.
    • Example:
      <TextView
          android:id="@+id/multilineTextView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Multiline Text"
          android:inputType="textMultiLine"/>
      
  6. TextView Text Formatting in Android:

    • Apply text formatting using SpannableString and SpannableStringBuilder.
    • Example in Kotlin:
      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
      
  7. Changing TextView Text Dynamically in Android:

    • Modify the text of a TextView dynamically in response to user actions or events.
    • Example:
      val textView = findViewById<TextView>(R.id.dynamicTextView)
      textView.text = "Initial Text"
      
      // Later in the code
      textView.text = "Updated Text"