Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Custom class for TextInputLayout

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

like image 370
nikhil lohar Avatar asked Dec 09 '19 12:12

nikhil lohar


People also ask

How do I get TextInputLayout from text?

Just use: TextInputLayout textInputLayout = findViewById(R. id. custom_end_icon); String text = textInputLayout.


Video Answer


1 Answers

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);
    }
}
like image 143
Sina Avatar answered Nov 02 '22 19:11

Sina