Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.Hello Readers! In this post, we are going to learn about android TextInputLayout using kotlin in any android application. We will also learn about different attributes of android TextInputLayout that can be used to customise this widget.
Output

Android TextInputLayout Output
Getting Started
Android TextInputLayout can be defined as below –
TextInputLayout is a layout that wraps EditText (or descendant) to show floating level when the hint is hidden due to user inputting text.
You can also show error message (if any) or character count using TextInputLayout.
TextInputEditText is used as child of this layout. It allows greater control over visual aspects of any text input.
Example of Android TextInputLayout Using Kotlin
At first, we will create android application. Then, we will use TextInputLayout using kotlin in the application. In this example, we will create a basic login screen using textInputLayout and textInputEditText.
1. Creating New Project in Kotlin
Follow steps below to create new project. Please ignore the steps if you have already created the project.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as TextInputLayout. Then, check Include Kotlin Support and click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | You will get a newly created project successfully if you have followed steps properly. |
Since we have a project now, we will modify xml and other files to use TextInputLayout using kotlin in the application.
2. Modify values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">TextInputLayout</string> <string name="username">Enter Username</string> <string name="password">Enter Password</string> <string name="submit">Submit</string> </resources>
3. Use TextInputLayout Widget in xml file
Open src/main/res/layout/activity_main.xml file and add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="30dp"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/username"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:text="@string/submit"/> </LinearLayout>
Here, we have defined two textInputLayout with textInputEditText that are used as username and password fields. Then, there is a button that are used to get the texts entered in username and password fields. Now, we will access these fields in kotlin file.
4. Access TextInputLayout Widget in Kotlin file
Open src/main/java/com.tutorialwing.textinputlayout/MainActivity.kt file and add below code into it.
package com.tutorialwing.textinputlayout import android.os.Bundle import android.support.design.widget.TextInputEditText import android.support.v7.app.AppCompatActivity import android.view.View import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val etUsername = findViewById<TextInputEditText>(R.id.username) val etPassword = findViewById<TextInputEditText>(R.id.password) val submit = findViewById<Button>(R.id.submit) if (submit != null && etPassword != null && etUsername != null) { submit.setOnClickListener { val username = etUsername.text.toString() val password = etPassword.text.toString() Toast.makeText(applicationContext, "Username: $username, Password: $password", Toast.LENGTH_SHORT).show() } } } }
In MainActivity.kt file, we have accessed username and password fields. Then, we have set a click listener on button to get the texts entered in username and password fields. Then, show it as toast message to the user.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml file
Code inside main/AndroidManifest.xml file is as below.
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.textinputlayout" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When we run the program, we will get output as shown above.
That’s end of our tutorial on Android TextInputLayout using Kotlin.