android kotlin - EditText first letter capitalization

Android Kotlin - Capitalizing First Letter in EditText

This code demonstrates two ways to capitalize the first letter of user input in an EditText field within an Android application written in Kotlin.

The code provides a MainActivity class and an associated layout file (activity_main.xml). The MainActivity class handles setting up the UI and defining functionality, while the layout file defines the visual structure of the activity.

Breakdown of the Code

  1. MainActivity.kt:

    • This class inherits from Activity and defines the lifecycle of the main screen.
    • It has a private member variable editText2 of type EditText.
    • In the onCreate method:
      • Sets the layout using setContentView.
      • Finds the EditText with the ID editText2 using findViewById.
      • Calls the firstLetterCapital extension function on editText2.
  2. firstLetterCapital Extension Function:

    • This function is an extension function for the EditText class.
    • It sets the inputType property of the EditText to a combination of TYPE_CLASS_TEXT and TYPE_TEXT_FLAG_CAP_SENTENCES using bitwise OR (or). This combination ensures the EditText behaves as a text field and capitalizes the first letter of sentences.
  3. activity_main.xml:

    • This file defines the layout of the activity using ConstraintLayout.
    • It contains two EditText elements:
      • editText: This EditText has the inputType attribute set to a combination of text, textVisiblePassword (for showing typed characters), and textCapSentences. This approach also capitalizes the first letter of sentences but is achieved directly in the layout XML.
      • editText2: This EditText is referenced in the MainActivity.kt code and has its input type set programmatically using the extension function.

Summary

This code provides two options for capitalizing the first letter in an EditText field:

  • Setting the inputType attribute directly in the layout XML using a combination of flags.
  • Using a custom extension function to set the inputType programmatically in the activity code.

Both approaches achieve the same functionality but offer different levels of control and code organization.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.os.Bundle
import android.text.InputType
import android.widget.EditText


class MainActivity : Activity() {
    private lateinit var editText2:EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        editText2 = findViewById(R.id.editText2)

        // first letter capitalization programmatically
        editText2.firstLetterCapital()
    }
}


// extension function for edit text sentence first letter capitalization
fun EditText.firstLetterCapital(){
    inputType = InputType.TYPE_CLASS_TEXT or
            InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#DCDCDC"
    android:padding="32dp">

    <!-- sentence first letter capitalization in xml -->
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:padding="12dp"
        android:textSize="30sp"
        android:inputType="text|textVisiblePassword|textCapSentences"
        android:fontFamily="sans-serif-condensed-medium"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.12" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="12dp"
        android:fontFamily="sans-serif-condensed-medium"
        android:padding="12dp"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin - EditText first letter capitalization
More android kotlin tutorials