I want to create TexInputLayout by dynamically. I can create TextInputLayout using following code block :
TextInputLayout til = new TextInputLayout(this);
EditText et = new EditText(this);
til.addView(et);
et.setHint("Enter");
information.addView(til);
information is name of my linear layout which I use in the project.
But I want to change style of the TextInputLaout which I created by dynamically, I want to use @style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.
I tried the following code block to change style :
TextInputLayout till= new TextInputLayout(new ContextThemeWrapper(this,
R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox));
EditText et = new EditText(this);
til.addView(et);
et.setHint("Enter");
information.addView(til);
but these code is not working. How can add style to TextInputLayout which I created dynamically ?
The 2nd parameter of the ContextThemeWrapper
is a theme overlay, not a style.
You can define in attrs.xml
a custom attribute:
<attr name="customTextInputStyle" format="reference" />
then in your app theme:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<!-- ..... -->
<item name="customTextInputStyle">@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox</item>
</style>
and finally:
TextInputLayout til = new TextInputLayout(this,null,R.attr.customTextInputStyle);
til.setHint("Label");
TextInputEditText et = new TextInputEditText(til.getContext()); //important: get the themed context from the TextInputLayout
til.addView(et);
buttonsLayout.addView(til);