Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompatEditText.getpParent() inside TextInputLayout returns FrameLayout

I am creating simple AppCompatEditText adding OnFocusChangeListener and putting it in the simple TextInputLayout.

When AppCompatEditText loosing focus it's content should be validate by isValidParam method.

It worked till yesterday, when I used rev.23.0.3 But now, when I used rev.24.0.2, it gives error as below on the 1st row of isValidParam method.

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

I checked in debugging mode. AppCompatEditText.getpParent() really returns Framelayout instead TextInputLayout.

LinearLayout llParams = new LinearLayout(context);
llParams.setOrientation(LinearLayout.VERTICAL);

// Create label for param
final TextInputLayout tilParam = new TextInputLayout(context);
// Add label into layout
llParams.addView(tilParam);


// Create Editor for param
final AppCompatEditText etParam = new AppCompatEditText(context);

edParam.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus)
            if (isValidParam(etParam)) {
                do some thing;
            } else {
                do other thing;
            }
    }
});

tilParam.addView(etParam);


// validation method
boolean isValidParam(AppCompatEditText editText) {
    TextInputLayout til = (TextInputLayout) editText.getParent();

    String text = editText.getText().toString().trim();

    if (!text.equls("some criteria") {
        till.setError("Error text")
        return false;
    }

    return true;
}
like image 685
Akmal Umaraliev Avatar asked Aug 26 '16 10:08

Akmal Umaraliev


People also ask

How do I get rid of TextInputLayout padding?

You can remove extra space above AppCompatEditText by setting app:hintEnabled="false" to TextInputLayout but it won't display hint until you re-enable that.

What is the use of TextInputLayout in Android?

Layout which wraps a TextInputEditText , EditText , or descendant to show a floating label when the hint is hidden while the user inputs text.


2 Answers

Update:
Use the widget TextInputEditText instead of EditText inside a TextInputLayout.

old answer

TextInputLayout textInputLayout = (TextInputLayout) editText.getParent().getParent();

That seems to work as a quick fix. Far from ideal.

like image 116
Tom Bevelander Avatar answered Oct 28 '22 07:10

Tom Bevelander


getParentForAccessibility() worked for me

like image 37
Vaisakh N Avatar answered Oct 28 '22 08:10

Vaisakh N