Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the TextInputLayout related to edit text?

I have many edit texts inside differents TextInputLayouts, How can i get the TextInputLayout where is the edit text i am working with?

Example :

@BindView(R.id.eT)
EditText eT;

@BindView(R.id.eT2)
EditText eT2;

@BindView(R.id.text_input_layout)
TextInputLayout textInputLayout;

@BindView(R.id.text_input_layout2)
TextInputLayout textInputLayout2;

i want something like eT.getTextInputLayout and get textInputLayout

extract of xml :

 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">

                <Spinner
                    android:id="@+id/spinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" />

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/text_input_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
                    app:errorTextAppearance="@style/error_appearance"
                    app:hintAnimationEnabled="true"
                    app:errorEnabled="true">

                <EditText
                    android:id="@+id/eT"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="number" />

                </android.support.design.widget.TextInputLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="0dp">

                <TextView
                    android:id="@+id/tV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:labelFor="@+id/spinne2"
                    android:text="@string/tarjeta" />

                <Spinner
                    android:id="@+id/spinner2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" />

            </LinearLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/text_input_layout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
                app:errorTextAppearance="@style/error_appearance"
                app:hintAnimationEnabled="true"
                app:errorEnabled="true">

                <EditText
                    android:id="@+id/eT2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:ems="10"
                    android:inputType="number" />
            </android.support.design.widget.TextInputLayout>
    </LinearLayout>

I want to get the for example the text_input_layout, through edit text eT for handling errors in the TIL (i do this in other module, so i don't want to pass edit text and TIL to this module).

like image 897
MatiRC Avatar asked Aug 04 '17 18:08

MatiRC


2 Answers

@Sami-Issa is right

edittext.getParent().getParent() 

returns the TextInputLayout

like image 114
LutfiTekin Avatar answered Sep 28 '22 05:09

LutfiTekin


Note: The actual view hierarchy present under TextInputLayout is NOT guaranteed to match the view hierarchy as written in XML. As a result, calls to getParent() on children of the TextInputLayout -- such as an TextInputEditText -- may not return the TextInputLayout itself, but rather an intermediate View. If you need to access a View directly, set an android:id and use findViewById(int). https://developer.android.com/reference/android/support/design/widget/TextInputLayout

but if you still want to do it, you can do it by using a method like this:

private static TextInputLayout findTextInputLayoutParent(View view) {
    return findTextInputLayoutParent(view.getParent(), 3);
}

private static TextInputLayout findTextInputLayoutParent(ViewParent view, int maxDepth) {
    if (view.getParent() instanceof TextInputLayout) {
        return (TextInputLayout) view.getParent();
    } else if (maxDepth > 0){
        return findTextInputLayoutParent(view.getParent(), maxDepth -1);
    }
    return null;
}
like image 36
Alécio Carvalho Avatar answered Sep 28 '22 04:09

Alécio Carvalho