Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between TextInputLayout and TextInputEditText

Need to know what actually difference between TextInputEditText and TextInputLayout, When should we use one of them.

like image 686
Ashish Dwivedi Avatar asked Jun 15 '16 11:06

Ashish Dwivedi


People also ask

What is difference between TextInputLayout and EditText?

The main difference between a TextInputLayout and an EditText is that TextInputLayout extends LinearLayout and it must contain TextInputEditText which extends EditText. This is so because TextInputEditText works when enclosed in a TextInputLayout.

What is TextInputEditText?

↳ com.google.android.material.textfield.TextInputEditText. A special sub-class of EditText designed for use as a child of TextInputLayout . Using this class allows us to display a hint in the IME when in 'extract' mode and provides accessibility support for TextInputLayout .

What is TextInputLayout?

Android TexInputLayout extends LinearLayout. The primary use of a TextInputLayout is to act as a wrapper for EditText(or its descendant) and enable floating hint animations. Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.

How do I remove TextInputLayout error?

Now you can simply do input. setError(..) for new error and input. setErrorEnabled(false) to remove it.


2 Answers

They are different layouts that complement each other functionalities.

  • TextInputLayout extends LinearLayout
  • TextInputEditText extends EditText

They were meant to be used together like following:

<TextInputLayout>
   <TextInputEditText/>
</TextInputLayout>

It's all there on the official docs:

TextInputLayout:

https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden due to the user inputting text

TextInputEditText:

https://developer.android.com/reference/android/support/design/widget/TextInputEditText.html

A special sub-class of EditText designed for use as a child of TextInputLayout.

like image 67
Budius Avatar answered Oct 16 '22 17:10

Budius


TextInputLayout vs TextInputEditText

TextInputLayout must contain a TextInputEditText instead of the normal EditText because it designed specially for using inside.

If you add EditText instead of TextInputEditText into TextInputLayout you will get the warning:

EditText added is not a TextInputEditText. Please switch to using that class instead.

For example if you wrap EditText, in Landscape Mode, you will get a big box, but the hint is missing.

TextInputLayout has features as floating hints, error labels, character counter, password visibility, animations and their customization

like image 2
yoAlex5 Avatar answered Oct 16 '22 18:10

yoAlex5