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
MainActivity.kt:
- This class inherits from
Activityand defines the lifecycle of the main screen. - It has a private member variable
editText2of typeEditText. - In the
onCreatemethod:- Sets the layout using
setContentView. - Finds the
EditTextwith the IDeditText2usingfindViewById. - Calls the
firstLetterCapitalextension function oneditText2.
- Sets the layout using
- This class inherits from
firstLetterCapital Extension Function:
- This function is an extension function for the
EditTextclass. - It sets the
inputTypeproperty of theEditTextto a combination ofTYPE_CLASS_TEXTandTYPE_TEXT_FLAG_CAP_SENTENCESusing bitwise OR (or). This combination ensures the EditText behaves as a text field and capitalizes the first letter of sentences.
- This function is an extension function for the
activity_main.xml:
- This file defines the layout of the activity using ConstraintLayout.
- It contains two
EditTextelements:editText: This EditText has theinputTypeattribute set to a combination oftext,textVisiblePassword(for showing typed characters), andtextCapSentences. This approach also capitalizes the first letter of sentences but is achieved directly in the layout XML.editText2: This EditText is referenced in theMainActivity.ktcode 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
inputTypeattribute directly in the layout XML using a combination of flags. - Using a custom extension function to set the
inputTypeprogrammatically 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 space validation
- android kotlin - EditText email validation
- android kotlin - EditText select all on focus
- android kotlin - EditText live characters count
- android kotlin - EditText allow only characters and numbers
- android kotlin - EditText numbers only programmatically
- android kotlin - EditText min length
- android kotlin - EditText change underline color programmatically
- android kotlin - EditText focus change listener
- android kotlin - EditText hide keyboard on lost focus
- android kotlin - Get raw resource uri
- android kotlin - Button with icon
- android kotlin - Resize ImageView programmatically
- android kotlin - ImageView add border programmatically
- android kotlin - ImageView add border