How to get the TextInputLayout related to edit text?
Asked Answered
P

3

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

Pendleton answered 4/8, 2017 at 18:34 Comment(5)
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)."Bushore
can you post the layout file?Biosynthesis
done @Biosynthesis i am thinking of create something such as hash with edit text => TIL so i pass the hash to the other module...Pendleton
Why not @yasin's approach? Its cleaner approach with the your current layout structureBiosynthesis
@Biosynthesis 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 layoutsPendleton
H
7

@Sami-Issa is right

edittext.getParent().getParent() 

returns the TextInputLayout

Halibut answered 7/1, 2020 at 17:45 Comment(0)
O
4

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;
}
Opuntia answered 21/2, 2020 at 12:56 Comment(0)
C
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

Clearway answered 20/7, 2018 at 6:27 Comment(4)
Doesn't work. For each TextView, getParentForAccessibility returns a Layout component (FrameLayout or ScrollLayout)Annunciation
@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 workClearway
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 :)Annunciation
@zulkarnain Please give an example how can get the TextInputLayout with getParentForAccessibility inside an extended EditText! Thank you!Utimer

© 2022 - 2024 — McMap. All rights reserved.