Android TextInputField Inflator Error - android

Had a crash while trying to use the new TextInputField for Android and wanted to share my solution.
Trying the new TextInputField in the android appcompat library was crashing my app. Here was my layout xml.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="e-mail"
android:inputType="textEmailAddress"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
The error I got:
android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.TextInputLayout.
SOLUTION:
Add the hintTextAppearance attribute to your TextInputLayout, so the lead tag looks like this:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#android:style/TextAppearance.Medium">

Make sure you have the following dependencies in your gradle file:
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
Working example:
<android.support.design.widget.TextInputLayout
android:id="#+id/txtEmail_InpLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/txtEmail"
android:hint="Email Address"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</android.support.design.widget.TextInputLayout>
(Setting hintTextAppearance is not necessary.)
Update:
If you experience issues with the hint text not appearing in newer versions of Android (Marshmallow / Nougat), update library to version 22.2.1 (see TextInputLayout not showing EditText hint before user focus on it).
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'

This happened to me as well, and I came up with a solution that does not require changing the App Theme, but merely changing the Theme of the TextInputLayout:
<android.support.design.widget.TextInputLayout
android:id="#+id/testingInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AppCompat">
<EditText
android:id="#+id/testingEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/testText"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
You will need to add the appCompat library if you have not already:
compile 'com.android.support:appcompat-v7:23.0.1'

in adroidx it worked for me
i have the libraries
implementation 'com.android.support:appcompat-v7:28.0.0'
//design widget
implementation 'com.android.support:design:28.0.0'
i change
<android.support.design.widget.TextInputLayout
for
<com.google.android.material.textfield.TextInputLayout

I got the same issue when inflating XML containing TextInputLayout.
The problem was fixed by setting the correct Style on my application. Just like it says here : android design support library
I've the following issue
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class android.support.design.widget.TextInputLayout
My style.xml was
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
</style>
</resources>
As it said in this post on Design Support Library
Note that as the Design library depends on the Support v4 and AppCompat Support Libraries, those will be included automatically when you add the Design library dependency.
So NO NEED TO ADD the following line inside the gradle file
compile 'com.android.support:appcompat-v7:22.2.0'
I found the link behove explaining that the Design Support Library is part of the AppCompat and it require the AppCompat Theme base to work. So
I've modify my style.xml to be
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
And it worked.

just to be clear as of Android Studio 3.* this is now changed to
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
(if this goes red a squiggly when you add it accept the version Android Studio wants to suggest)
this needs to be inside the
dependencies {}
section of build.gradle (Module:app) so it looks like this
dependencies {
...
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
then hit sync now

Use the following:
View view = LayoutInflator.from(getActivity()).inflate(R.layout.your_layout_file,parent,false);

Make sure the LayoutInflater is created from an AppCompatActivity
ex.
instead of
inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
use:
inflater = LayoutInflater.from(<appCompatActivity>);

None of the solution worked for me. I am using
compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.android.support:design:28.0.0'
Tried all the soluton. My app runs perfectly in devices with lower sdk. But crashes in newer( greater than sdk 26) devices. But after scratching head for 2 days finally got the solution.
my layout editor showed the error of
The following classes could not be instantiated:
- android.support.design.widget.TextInputLayout
But it did not gave me enough information to work with. It was not until I looked at other errors at the layout editor. Under render problem I found that
Couldn't resolve resource #string/path_password_strike_through
I finally got the solution after investigating this render problem. All you need to do is add
app:passwordToggleDrawable="#string/string_name"
in your TextInputLayout xml file.
<android.support.design.widget.TextInputLayout
android:id="#+id/login_input_layout_password"
android:layout_width="match_parent"
app:passwordToggleDrawable="#drawable/ic_lock_outline_grey600_24dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:backgroundTint="#color/colorAccent"
app:boxStrokeColor="#555"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/login_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TextInputLayout>

Finally I got this working...as per as the latest changes in the library itself, change the library from
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_input"
android:layout_margin="#dimen/activity_horizontal_margin"
app:layout_constraintTop_toBottomOf="#+id/edt_first_name"
app:layout_constraintStart_toEndOf="#+id/guideline"
app:hintEnabled="false">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Floating Hint Disabled" />
</android.support.design.widget.TextInputLayout>
to
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_input"
android:layout_marginTop="#dimen/activity_horizontal_margin"
app:layout_constraintTop_toBottomOf="#+id/edt_first_name"
app:layout_constraintStart_toEndOf="#+id/guideline"
app:hintEnabled="false">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Floating Hint Disabled" />
</com.google.android.material.textfield.TextInputLayout>

you might have added it
compile 'com.android.support:appcompat-v7:22.2.0' but you need to add compile 'com.android.support:design:22.2.0'
you can try this dependencies:
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
in your gradle file.

I had the same issue with Android Studio 3.0.1
All you need to add the following line into gradle.properties (Project Propeties):
android.enableAapt2=false

for me work changes from EditText to TextInputEditText
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/date_raffle"/>
</android.support.design.widget.TextInputLayout>

Using implementation 'com.google.android.material:material:1.1.0' all you have to do is use your XML tags like this: <com.google.android.material.textfield.TextInputLayout for it to work...In my case it applies to my <com.google.android.material.textfield.TextInputEditText as well. Happy coding!...

Related

Android studio preview does not show layouts

I have just started to learn android studio. I've encountered a problem:
The layouts won't appear in the preview after I have added them. But if I change the theme or run the app in the emulator they do appear but they overlap.
For API 27:
Add the following dependency:
If you are using targetSdkVersion 27,
in your App Level gradle file build.gradle(Module: app)
implementation 'com.android.support:appcompat-v7:27.1.1'
For API 28:
if you are using targetSdkVersion 28,
Step 1:
Make sure you have the following dependency:
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
UPDATE: For the latest appcompat-v7 library repo version, follow this link
https://mvnrepository.com/artifact/com.android.support/appcompat-v7?repo=google
Step 2:
Goto Values > styles.xml and modify the style as follows,
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
or any other base theme you like.
Here is an example constraint layout for you,
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditText
android:id="#+id/enter_age"
android:layout_width="200dp"
android:layout_height="100dp"
android:ems="10"
android:hint="enter age"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/button1"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="110dp"
android:onClick="butGetAge"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/enter_age"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>

EditText Drawable Right not working when upgrading design support library

I have upgraded support library from 'com.android.support:design:23.0.0' to 'com.android.support:design:25.0.0'. Now drawableright(Image) not working for EditText. Here is my code.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableRight="#drawable/ic_calendar"/>
</android.support.design.widget.TextInputLayout>
Add drawableEnd to your edittext this way
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableRight="#drawable/ic_calendar"
android:drawableEnd="#drawable/ic_calendar"/>
update your support library
compile 'com.android.support:design:25.3.1'

android : TextInputLayout hides drawableRight of child EditText

I am always updated with library updates, and this is what it resulted into.
In build.gradle(Project:xxx):
classpath 'com.android.tools.build:gradle:2.2.0-beta1'
In build.gradle(Module:app)
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
And put password field as below:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
Taadaa...My Password field looks now as below:
You can also go simply with EditText.
But, there are some glitches in this update.
Problem:
If I put any drawableRight in EditText, it displays nothing. TextInputLayout also has property to disable toggle functionality. I did it that too as below, but no success:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="false">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_alarm_on_black_24dp"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
Can anyone help me please.
I reported the issue at https://code.google.com/p/android/issues/detail?id=221002.
It was a bug in the library version 24.2.0 but it has been fixed in version 24.2.1. Simply update as below:
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
You need to upgrade the support library to 24.2.1 (see https://code.google.com/p/android/issues/detail?id=221002).

RuntimeException while inflating InputTextLayout

When i create new sample project with TextInputLayout , Its working good.
But in my exist project if i have added TextInputlayout , its throws runtime exception caused by java.lang.UnsupportedOperationException.
Why its happen? How to resolve this?
compile 'com.android.support:design:22.2.0'
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="#+id/til"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/textDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Why it happen? how to solve this?
Compare the gradle files of your sample project to your real project. Your project might not be setup correctly to use Material Design resources.

Android EditText view Floating Hint in Material Design

Does API 21 provide a method to use the following feature:
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
I'm trying to float the EditText hints.
Thanks!
Floating hint EditText:
Add below dependency in gradle:
compile 'com.android.support:design:22.2.0'
In layout:
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
</android.support.design.widget.TextInputLayout>
Yes, as of May 29, 2015 this functionality is now provided in the Android Design Support Library
This library includes support for
Floating labels
Floating action button
Snackbar
Navigation drawer
and more
The Android support library can be imported within gradle in the dependencies :
compile 'com.android.support:design:22.2.0'
It should be included within GradlePlease! And as an example to use it:
<android.support.design.widget.TextInputLayout
android:id="#+id/to_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextViewTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="To"
android:layout_marginTop="45dp"
/>
</android.support.design.widget.TextInputLayout>
Btw, the editor may not understand that AutoCompleteTextView is allowed within TextInputLayout.
Android hasn't provided a native method. Nor the AppCompat.
Try this library: https://github.com/rengwuxian/MaterialEditText
This might be what you want.
Import the Support Libraries, In your project's build.gradle file, add the following lines in the project's dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
}
Use following TextInputLayout in your UI Layout:
<android.support.design.widget.TextInputLayout
android:id="#+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Username"/>
</android.support.design.widget.TextInputLayout>
Than, call setHint on TextInputLayout just after setContentView call because, to animate the floating label, you just need to set a hint, using the setHint method.
final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);
usernameWrapper.setHint("Username");
You need to add the following to your module build.gradle file:
implementation 'com.google.android.material:material:1.0.0'
And use com.google.android.material.textfield.TextInputLayout in your XML:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/my_hint">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
</com.google.android.material.textfield.TextInputLayout>
#andruboy's suggestion of https://gist.github.com/chrisbanes/11247418 is probably your best bet.
https://github.com/thebnich/FloatingHintEditText kind of works with appcompat-v7 v21.0.0, but since v21.0.0 does not support accent colors with subclasses of EditText, the underline of the FloatingHintEditText will be the default solid black or white. Also the padding is not optimized for the Material style EditText, so you may need to adjust it.
No it doesn't. I would expect this in a future api release, but for now we are stuck with EditText. Another option is this library:
https://github.com/marvinlabs/android-floatinglabel-widgets
For an easier way to use the InputTextLayout, I have created this library that cuts your XML code to less than the half, and also provides you with the ability to set an error message as well as a hint message and an easy way to do your validations.
https://github.com/TeleClinic/SmartEditText
Simply add
compile 'com.github.TeleClinic:SmartEditText:0.1.0'
Then you can do something like this:
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="#+id/emailSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Email"
app:setMandatoryErrorMsg="Mandatory field"
app:setRegexErrorMsg="Wrong email format"
app:setRegexType="EMAIL_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="#+id/passwordSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Password"
app:setMandatoryErrorMsg="Mandatory field"
app:setPasswordField="true"
app:setRegexErrorMsg="Weak password"
app:setRegexType="MEDIUM_PASSWORD_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="#+id/ageSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Age"
app:setMandatory="false"
app:setRegexErrorMsg="Is that really your age :D?"
app:setRegexString=".*\\d.*" />
Use the TextInputLayout provided by the Material Components Library:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>

Categories

Resources