Android TextView Tutorial: Display and Style Text Easily The TextView is the foundational building block for displaying text in Android applications. Whether you are building a simple login screen or a complex data dashboard, mastering the TextView is essential.
This tutorial covers everything you need to know to implement, style, and optimize text in modern Android development using XML layout files and Kotlin. 1. Adding a TextView to Your Layout
To display text, you must declare a TextView inside your XML layout file (usually activity_main.xml). At a minimum, every view requires a defined width, height, and the text you want to show.
Use code with caution. Best Practice: Use String Resources
Hardcoding text directly into android:text is discouraged. Instead, store your strings in the res/values/strings.xml file to easily support multiple languages (localization). In strings.xml: Use code with caution. In your layout XML: android:text=“@string/welcome_back” Use code with caution. 2. Basic Text Styling
Android provides built-in attributes to change the appearance of your text directly within the XML layout.
Change the size of your text using the android:textSize attribute. Always use sp (scale-independent pixels) for font sizes so the text scales properly based on user accessibility settings. android:textSize=“18sp” Use code with caution. Text Color
Modify the color of your text using hex color codes or color resources from res/values/colors.xml.
android:textColor=“#FF5733” android:textColor=“@color/purple_500” Use code with caution. Text Style and Typeface
You can make text bold, italic, or a combination of both using android:textStyle. You can also change the font family.
android:textStyle=“bold|italic” android:fontFamily=“sans-serif-condensed” Use code with caution. 3. Advanced Text Customization
Once you master the basics, you can apply advanced attributes to improve user experience and interface design. Text Alignment and Gravity
To position the text inside its allocated layout bounds, use android:gravity.
android:gravity=“center” Use code with caution. Handling Long Text (Ellipsize and Max Lines)
If a string is too long for the screen, it can break your layout. Prevent this by limiting the lines and adding an ellipsis (…). android:maxLines=“2” android:ellipsize=“end” Use code with caution. Text Modification
You can automatically alter the casing of the string without changing the underlying data source. android:textAllCaps=“true” Use code with caution. 4. Manipulating TextView in Kotlin
To change your text dynamically based on user interaction, control the TextView inside your Kotlin Activity or Fragment file.
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Find the TextView by its ID val welcomeTextView = findViewById Use code with caution. 5. Styling Single Sentences with Spanners
If you want to style individual words inside a single TextView differently (e.g., making only one word bold or red), use a SpannableString.
val statusText = “Order Status: Delivered” val spannable = SpannableString(statusText) // Make “Delivered” green spannable.setSpan( ForegroundColorSpan(Color.GREEN), 14, // Start index statusText.length, // End index Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ) welcomeTextView.text = spannable Use code with caution. Summary Checklist for Clean Code Use sp for all text sizes to keep layouts accessible. Store all user-facing strings in strings.xml.
Use ellipsize on dynamic text fields to prevent broken layouts.
Use SpannableString instead of stacking multiple TextViews side-by-side for mixed styles.
Leave a Reply