I created a CustomTextInputLayout class and set the style to Resource.Style.Widget_MaterialComponents_TextInputLayout_OutlinedBox and BoxBackgroundMode to BoxBackgroundOutline but the OutlinedBox is still not showing. I'm using Xamarin.Android.
public class CustomTextInputLayout : TextInputLayout
{
public CustomTextInputLayout(Context context) : base(context, null, Resource.Style.Widget_MaterialComponents_TextInputLayout_OutlinedBox)
{
Init(context);
}
public CustomTextInputLayout(Context context, IAttributeSet attrs) : base(context, attrs, Resource.Style.Widget_MaterialComponents_TextInputLayout_OutlinedBox)
{
Init(context);
}
public CustomTextInputLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
Init(context);
}
public void Init(Context context)
{
var textInputEditText = new TextInputEditText(context) { LayoutParameters = new LayoutParams(-1, -2) };
AddView(textInputEditText);
BoxBackgroundMode = BoxBackgroundOutline;
}
}
This is the content_main.xml:
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">
<MaterialTest.CustomTextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
And this is the AppTheme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
This is what it looks like (no OutlinedBox):
Any help would be appreciated.
I modify your code, you can take a look the following code:
public class CustomTextInputLayout : TextInputLayout
{
public CustomTextInputLayout(Context context) : base(context, null, Resource.Style.Widget_MaterialComponents_TextInputLayout_OutlinedBox)
{
Init(context);
}
public CustomTextInputLayout(Context context, IAttributeSet attrs) : base(context, attrs, Resource.Style.Widget_MaterialComponents_TextInputLayout_OutlinedBox)
{
Init(context);
}
public CustomTextInputLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
Init(context);
}
public void Init(Context context)
{
SetBoxBackgroundMode(TextInputLayout.BoxBackgroundOutline);
SetBoxCornerRadii(5, 5, 5, 5);
BoxStrokeColor = Android.Graphics.Color.Red;
Hint = "Please Enter Email Address";
var textInputEditText = new TextInputEditText(context);
AddView(textInputEditText);
}
}
<textinputlayout.CustomTextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
The screenshot:
But I suggest you can use TextInputLayout and TextInputEditText in xml.
<com.google.android.material.textfield.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/userIDTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
app:boxStrokeColor="@color/boxcolor"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/userIDTextInputEditText"
android:layout_width="match_parent"
android:hint="Enter User Name"
android:textColorHint="@color/colorAccent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
You can also add TextInputLayout and TextInputEditText by code behind.
private LinearLayout linearlayout1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
linearlayout1 = (LinearLayout)FindViewById(Resource.Id.linearLayout1);
TextInputLayout emailTextInputLayout = new TextInputLayout(this, null, Resource.Style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
emailTextInputLayout.Hint = "Please Enter Email Address";
emailTextInputLayout.SetBoxBackgroundMode(TextInputLayout.BoxBackgroundOutline);
emailTextInputLayout.SetBoxCornerRadii(5, 5, 5, 5);
TextInputEditText edtEmail = new TextInputEditText(emailTextInputLayout.Context);
emailTextInputLayout.AddView(edtEmail);
linearlayout1.AddView(emailTextInputLayout);
}
You need to add android:id="@+id/linearLayout1"
firstly.
<LinearLayout 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:id="@+id/linearLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
One thread that you can also take a look:
How to create TextInputLayout with OutlineBox programmatically