3

I want to have my CustomTextInputLayout to have Widget.MaterialComponents.TextInputLayout.OutlinedBox as default style without defining it anywhere in the XML.

I tried this

class CustomTextInputLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox), attrs, defStyleAttr) {

}

and this

class CustomTextInputLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox)

but it's not working. I've tried the default XML way

<com.custom.CustomTextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    ...>
    <com.google.android.material.textfield.TextInputEditText
        ...
        android:hint="Sample Hint" />
</com.custom.CustomTextInputLayout>

and it's working.

  • What am I missing here?
  • How can I set a default style for custom TextInputLayout without using XML?
theapache64
  • 10,926
  • 9
  • 65
  • 108
  • That's a style, not a theme, which is why it doesn't work as expected with the `ContextThemeWrapper`. `TextInputLayout` now has publicly available methods to set that background, however, as described in the first part of [this answer](https://stackoverflow.com/a/53001705). You could just do that instead, in your custom `View`'s initial setup. – Mike M. Jun 19 '20 at 04:40
  • @MikeM. I've tried that, but as shown in the [output image](https://i.stack.imgur.com/t2stI.png), the top left corners getting damaged. The XML way would work, but my requirement is to do it without XML. – theapache64 Jun 19 '20 at 04:49
  • I can't reproduce that: https://i.stack.imgur.com/j3T2G.png. Which version of Material Components are you using? – Mike M. Jun 19 '20 at 05:15
  • I am using `v1.3.0-alpha01`, and here's the [reproducible link](https://github.com/theapache64/52989087) – theapache64 Jun 19 '20 at 05:34
  • Well, in the Kotlin class, apparently that won't work in an `init()` block, even with the `post()`. You'll have to do it from explicit constructors. The Java one works if you get rid of the `ContextThemeWrapper`s, and add the `setBoxBackgroundMode()` where needed. Keep in mind that `View`s inflated from layout XML are instantiated with the two-parameter constructor, so make sure that you call it there, at least, since you're not chaining your constructors. – Mike M. Jun 19 '20 at 06:11
  • Yeah. I believe the `@JvmOverload` would generate each constructor. Thanks for looking into it @Mike – theapache64 Jun 19 '20 at 06:26
  • Yeah, it does, but you can't add any of your own code in the shorthand form, so you'll need to spell out the necessary constructors. – Mike M. Jun 19 '20 at 06:28
  • Got it. Maybe you can add an with all these findings,so that it'd help someone else. – theapache64 Jun 19 '20 at 06:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216254/discussion-between-theapache64-and-mike-m). – theapache64 Jun 19 '20 at 06:40

1 Answers1

-1

It is not exactly what you are looking for. You can define:

public class CustomTextInputLayout @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = R.attr.textInputStyle
) : TextInputLayout(ContextThemeWrapper(context, R.style.Outlined_Theme), attrs, defStyleAttr) { ...  }

with:

<style name="Outlined.Theme" parent="">
      <item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox</item>
</style>

Then in your layout juse use:

 <com.example.quicksample.CustomTextInputLayout
       ....
       android:hint="Sample">

       <com.google.android.material.textfield.TextInputEditText../>

 </com.example.quicksample.CustomTextInputLayout>

enter image description here

Your code doesn't work because ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox) the 2nd parameter has to be an attribute theme and not a style (it is the same for TextInputLayout(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox))

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841