12

I am implementing a functionality to change the case of textInputlayout Hint text to upper case when the hint floats up and vice versa.

For that I am using OnFocusChangeListener on its child textInputEditText. To make it easy to implement I am implementing View.OnFocusChangeListener on my activity like:

public class LoginActivity extends BaseActivity implements View.OnFocusChangeListener

and overriding the method in the activity like:

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(findViewById(v.getId()) instanceof TextInputEditText){
        TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent();
        if(hasFocus){
            textInputLayout.setHint(textInputLayout.getHint().toString().toUpperCase());
        }else{
            textInputLayout.setHint(Utility.modifiedLowerCase(textInputLayout.getHint().toString()));
        }
    }
}

In the above method I am trying to get the view of parent textInputLayout using the line

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent();

The above line of code throws a fatal error

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout

and it is very obvious because it returns Framelayout which cannot be casted in textInputLayout

And if I use

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getRootView();

it again throws a fatal error because getRootView() returns DecorView which cannot be casted in textInputLayout

My question is how to get parent textInputLayout from child textInputEditText?

Please guide.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
  • Why not assign a id to TextInputLayout in xml layout like TextInputEditText? – Daniel RL Jul 03 '17 at 10:46
  • @DanielRL I have already assigned id in xml but it won't work here as harcoding id in my overridden method will only work for that `textinputlayout` not for others and I want it to work for my every `textinputlayout` in the layout – Rahul Sharma Jul 03 '17 at 10:50

3 Answers3

17

I solved the problem with the below line of code:

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent().getParent();

It returns textInputlayout as required.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
3

Instead of using TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent(); use the instance of view like TextInputEditText et = (TextInputEditText) v;and then TextInputLayout lay = (TextInputLayout)et.getParent().getParent();

0

You can add id in TextInputLayout like this:

<android.support.design.widget.TextInputLayout
    android:id="@+id/nameLayout"
    style="@style/LightInputLayoutNoBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/create_contact_account_data_name"
    app:hintTextAppearance="@style/TextSmall.Bold">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/name"
        style="@style/LightInputFieldNoBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableEnd="@drawable/ic_book_contacts" />

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

And call findViewById to TextInputLayout in activity

Daniel RL
  • 353
  • 1
  • 12
  • I have already assigned id in xml but it won't work here as harcoding id in my overridden method will only work for that `textinputlayout` not for others and I want it to work for my every `textinputlayout` in the layout. – Rahul Sharma Jul 03 '17 at 10:54
  • In android documentation says you can not do that: https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html Thus you must be asign a listener by textinputedittext and use findviewbyid. – Daniel RL Jul 03 '17 at 12:02