Outlined Text Field for TextInputLayout

Viewed 2420

I'm trying to create a TextInputLayout with Outline Boarder. But whenever I use the,following style, it malfunction(crash) the app.
Before posting I have tried every solution that listed in and outside the stackoverflow, but nothing fixed the issue so far.

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

Current layout

<ScrollView...

    <LinearLayout
      android: layout_width = "200dp"
      android: layout_height = "wrap_content"
      android: orientation = "vertical"
      android: layout_marginTop = "2dp" >

    <com.google.android.material.textfield.TextInputLayout
       android: id = "@+id/outlinedTextField"
       android: layout_width = "match_parent"
       android: layout_height = "wrap_content"
       android: hint = "@string/label"
       style = "@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
       app: boxStrokeWidthFocused = "2dp"
       app: boxStrokeColor = "@color/border_primary" >

    <com.google.android.material.textfield.TextInputEditText
       android: layout_width = "match_parent"
       android: layout_height = "wrap_content" />
    
  </LinearLayout>

</ScrollView>

Complete Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".user.userprofile.UserProfileFragment">
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <include layout="@layout/user_profile_header"/>
    
            <LinearLayout
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/outlinedTextField"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android: hint = "@string/label"
                    app:hintTextColor="@color/text_primary"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    app:boxStrokeWidthFocused="2dp"
                    app:hintTextColor="@color/text_primary"
                    app:boxStrokeColor="@color/border_primary">
    
                    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
    
            </LinearLayout>
    
        </LinearLayout>
    
    </ScrollView>

More Info : when style is added hint goes out of focus. Also border doesn't appear.

2 Answers

You need to check a couple of things:

  1. Make sure that your AppTheme is a child of Theme.MaterialComponents (or a descendant).

So, your style.xml should be something like

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents">
         <!-- rest of your style items-->
    </style>
</resources>
  1. You add the material components library in module gradle file

implementation 'com.google.android.material:material:1.2.0'

TextInputLayout is not a self closing tag. Edit your code and enclose TextInputEditText within TextInputLayout..

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/outlinedTextField"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android: hint = "@string/label"
                    app:hintTextColor="@color/text_primary"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    app:boxStrokeWidthFocused="2dp"
                    app:hintTextColor="@color/text_primary"
                    app:boxStrokeColor="@color/border_primary">
    
                    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>
Related