Skip to content Skip to sidebar Skip to footer

CollapsingToolbarLayout Not Collapsing When EditText Get Focused

I am using CollapsingToolBar inside CoordinatorLayout and I've NestScrollView containing some EditText as child views. What i am facing is when edittext get focused and keybaoard a

Solution 1:

I've resolved this by adding OnFocusChangeListener to EditText and if it has focus - just collapse view with setExpanded method:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(final View v, boolean hasFocus) {
        if (hasFocus) {
           mAppBarLayout.setExpanded(false, true);
        }
    }
});

If you want your CollapsingToolbarLayout be collapsed every time when your other EditText's from your layout get focused, then you should set the same OnFocusChangeListener to each of them.


Solution 2:

RecyclerView is a scrollable widget which means, these scrollable widgets won't work with eachother.(But, RecyclerView is a child from NestedScrollingChild)

Check this question for more explanation:

How to use RecyclerView inside NestedScrollView?

But, you can use it with custom LinearLayoutManager

https://stackoverflow.com/a/32736113/4409113


By the way,

You could use that RecyclerView inside the CoordinatorLayout and outside the NestedScrollView.

Hope that helps.


Solution 3:

I think after all researches this is the best solution

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(final View v, boolean hasFocus) {
    if (hasFocus) {
        mAppBarLayout.setExpanded(false, true); // second one for animation
    }
}
});

Post a Comment for "CollapsingToolbarLayout Not Collapsing When EditText Get Focused"