10

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).

MatiRC
  • 189
  • 1
  • 3
  • 13
  • 3
    You could try calling eT.getParent() and cast it, but as written on TextInputLayout "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)." – yasin Aug 04 '17 at 19:17
  • can you post the layout file? – Mani Aug 05 '17 at 04:57
  • done @Mani i am thinking of create something such as hash with edit text => TIL so i pass the hash to the other module... – MatiRC Aug 07 '17 at 12:54
  • Why not @yasin's approach? Its cleaner approach with the your current layout structure – Mani Aug 08 '17 at 02:19
  • 1
    @Mani because i need the TIL for making the error message (as textInputLayout.setError("ERROR") when i found an error.in the "child" edit text, so i need this relation and if i understand correctly, getParent NOT guaranteed return the TIL. What i do in the other module, is receive an edit text and a list of TILs, so when eT found an error, setError is called, i dont want to declare by findViewById so i can use this method with diferents layouts – MatiRC Aug 08 '17 at 22:50

3 Answers3

6

@Sami-Issa is right

edittext.getParent().getParent() 

returns the TextInputLayout

LutfiTekin
  • 441
  • 1
  • 10
  • 19
3

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;
}
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
0

You can call getParentForAccessibility on a View or EditText to get the TextInputLayout reference for that View if that View is inside a TextInputLayout

zulkarnain shah
  • 971
  • 9
  • 20
  • 2
    Doesn't work. For each TextView, getParentForAccessibility returns a Layout component (FrameLayout or ScrollLayout) – Sami Issa Nov 26 '18 at 09:12
  • @SamiIssa. That's why my answer says "if that View is inside a TextInputLayout". Which means that the direct parent of the view in question has to be a TextInputLayout for this solution to work – zulkarnain shah Nov 26 '18 at 11:51
  • 3
    I have a FrameLayout that contains a TextInputLayout and this contains a TextInputEditText. When I call TextInputLayout.getParentForAccessibility(), it returns the FrameLayout. Finally I solved with a non elegant way, but works. TextInputLayout.getParent().getParent(). I'm not proud for it :) – Sami Issa Nov 27 '18 at 07:59
  • @zulkarnain Please give an example how can get the TextInputLayout with getParentForAccessibility inside an extended EditText! Thank you! – Abigail La'Fay May 27 '19 at 14:25