Android TextInputLayout Using Kotlin With Example

In this article, we will learn about android TextInputLayout using Kotlin. We will go through various example that demonstrates how to use different attributes of TextInputLayout. For example,

In this article, we will get answer to questions like –

  • What is TextInputLayout?
  • Why should we consider TextInputLayout while designing ui for any app?
  • What are possibilities using TextInputLayout while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Android TextInputLayout Output of textinputlayout using kotlin example

Android TextInputLayout Output

Getting Started

We can define android TextInputLayout widget 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.

Now, how do we use TextInputLayout in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as TextInputLayout. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android TextInputLayout Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using TextInputLayout in Kotlin

Follow steps below to use TextInputLayout in newly created project –

  • 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>
    
  • Open res/layout/activity_main.xml file. Then, add below code in 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">
    
        <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/username" />
    
        </com.google.android.material.textfield.TextInputLayout>
    
        <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password"
                android:inputType="textPassword" />
    
        </com.google.android.material.textfield.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.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.textinputlayout
    
    import android.os.Bundle
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import com.tutorialwing.textinputlayout.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		setContentView(binding.root)
    
    		setupTextInputLayout()
    	}
    
    	private fun setupTextInputLayout() {
    		binding.submit.setOnClickListener {
    			val username = binding.username.text.toString()
    			val password = binding.password.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.

Now, run the application. We will get output as below –

Tutorialwing Android TextInputLayout Output of textinputlayout using kotlin example

Android TextInputLayout Output

Different Attributes of TextInputLayout in XML

Now, we will see how to use different attributes of Android TextInputLayout using Kotlin to customise it –

Set Id of TextInputLayout

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of TextInputLayout using android:id attribute like below –

    <TextInputLayout
        android:id="@+id/textInputLayout_ID"
        />

Here, we have set id of TextInputLayout as textInputLayout_ID using android:id=”” attribute. So, if we need to reference this TextInputLayout, we need to use this id – textInputLayout_ID.
Learn to Set ID of TextInputLayout Dynamically

Set Width of TextInputLayout

We use android:layout_width=”” attribute to set width of TextInputLayout.
We can do it as below –

    <TextInputLayout
        android:id="@+id/textInputLayout_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of TextInputLayout Dynamically

Set Height of TextInputLayout

We use android:layout_height=”” attribute to set height of TextInputLayout.
We can do it as below –

    <TextInputLayout
        android:id="@+id/textInputLayout_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of TextInputLayout Dynamically

Set Padding of TextInputLayout

We use android:padding=”” attribute to set padding of TextInputLayout.
We can do it as below –

    <TextInputLayout
        android:id="@+id/textInputLayout_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in TextInputLayout using android:padding=”” attribute.
Learn to Set Padding of TextInputLayout Dynamically

Set Margin of TextInputLayout

We use android:layout_margin=”” attribute to set margin of TextInputLayout.
We can do it as below –

    <TextInputLayout
        android:id="@+id/textInputLayout_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in TextInputLayout using android:layout_margin=”” attribute.
Learn to Set Margin of TextInputLayout Dynamically

Set Background of TextInputLayout

We use android:background=”” attribute to set background of TextInputLayout.
We can do it as below –

    <TextInputLayout
        android:id="@+id/textInputLayout_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in TextInputLayout using android:background=”” attribute.
Learn to Set Background of TextInputLayout Dynamically

Set Visibility of TextInputLayout

We use android:visibility=”” attribute to set visibility of TextInputLayout.
We can do it as below –

    <TextInputLayout
        android:id="@+id/textInputLayout_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of TextInputLayout using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of TextInputLayout Dynamically

Till now, we have see how to use android TextInputLayout using Kotlin. We have also gone through different attributes of TextInputLayout to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Thus, we have seen what is TextInputLayout, how can we use android TextInputLayout using Kotlin ? etc. We also went through different attributes of android TextInputLayout.

Leave a Reply