When I am using textInputLayout withOut AppCompat theme in the application, I am not able to inflate the layout and my app is crashing. When I tried setting Appcompat theme for TextInputLayout alone, it worked fine for devices with API's above 21, but it is still crashing for pre lollipop devices. Is there a way to use TextInputLayout on PreLollipop devices without using AppCompat theme for the whole application ?
TextInputLayout needs a theme derived from a Design theme. First you need to get a context with such theme.
val themedContext = ContextThemeWrapper(context, R.style.Theme_Design_Light)
In here context
is an activity. Now you have two options:
val inflater = LayoutInflater.from(themedContext)
inflater.inflate(R.layout.my_input_layout, inputParent, true)
// Find the input layout and edit text by ID and work with them.
inputParent
is a view group where the input layout will be attached after inflation.
val inputLayout = TextInputLayout(themedContext)
val editText = EditText(inputLayout.context)
inputLayout.addView(editText)
inputParent.addView(inputLayout)
I recommend option 1 because it's what most people are used to when defining view hierarchies. it may help you avoiding unexpected results.
Or just use the AppCompat theme and AppCompatActivity because why the hell not.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments