1

I want to create a material edittext field like this :

enter image description here

This is how I am creating the editext field:

TextInputLayout textInputLayout = new TextInputLayout(MainActivity.this);
textInputLayout.setHint("name:");

TextInputEditText editText = new TextInputEditText(MainActivity.this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
editText.setTypeface(Typeface.create("sans-serif-smallcaps", Typeface.NORMAL));
editText.setScaleX(0.99f);

textInputLayout.addView(editText);

textInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
textInputLayout.setBoxCornerRadii(10, 10, 10, 10);
textInputLayout.setPadding(10, 10, 10, 10);
textInputLayout.setBoxStrokeWidth(2);

layout.addView(textInputLayout);

This is my output: enter image description here

As you can see, the edittext field I get doesn't have any borders. How do I fix this?

I have tried textInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_FILLED);, but the result is still the same.

  • you need to set the style as `Widget.MaterialComponents.TextInputLayout.OutlinedBox` . Setting style at runtime may not work i never tried it . So i suggest you create a layout file and inflate it at runtime .. – ADM May 03 '23 at 05:59
  • I recommend to create the `TextInputLayout` and the `TextInputEditText` in the xml and just manage the visibility of the layout in the Activity/Fragment. Therefor set the visibility to `gone` in the xml and when it should be visible to `visible`. – Fah May 03 '23 at 06:38
  • @Fah The `TextInputLayout` is being created at runtime based on a certain parameter. Like if the parameter is `1` then only a single `TextInputLayout` is created, if `2` then two, and so on –  May 03 '23 at 06:40
  • @ADM How do I add the style `Widget.MaterialComponents.TextInputLayout.OutlinedBox` at runtime? I can add it in the `xml` code, but it doesn't work in runtime –  May 03 '23 at 06:53
  • @naive_6198 i m not sure . try doing [This way](https://stackoverflow.com/a/48446167/4168607) or u can look for a similar thread. – ADM May 03 '23 at 07:01
  • @naive_6198 You can use a `RecyclerView` to create a list of `TextInputEditText` then you can create as much of them as you want during runtime. – Fah May 03 '23 at 07:24

1 Answers1

0

Same question has been answered here in the below link,

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" implementation 'com.android.support:design:28.0.0-alpha1'

<android.support.design.widget.TextInputLayout
android:id="@+id/name_text_input"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<android.support.design.widget.TextInputEditText
   android:id="@+id/name_edit_text"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="@string/label_name" />
</android.support.design.widget.TextInputLayout>

Outlined Edit Text from Material Design material-design

  • unfortunately this does not answered the question . OP wants to create TextInputLayout at runtime . – ADM May 03 '23 at 06:21