In TextInputLayout I want to add TextInputEditText via Custom class Here is the code for custom class I have created :
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(@NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
}
}
Here is the Xml code :
<com.example.myapplication.CustomTextInputEditText
android:id="@+id/custom_view"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:boxStrokeColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"/>
It doesn't show any preview of the custom view
Just use: TextInputLayout textInputLayout = findViewById(R. id. custom_end_icon); String text = textInputLayout.
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(@NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
//removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
addView(editText);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With