📜 ⬆️ ⬇️

Translucent to Android and adjustResize


The ability to use translucent in android appeared in KITKAT . And it looks in the right design is pretty impressive. And with the advent of fashion for devices with “Bangs”, it seems to be just a necessary thing in any warm tube startups.


Let’s imagine a situation: you’ve written down a stunningly beautiful design that scrolls under StatusBar and NavigationBar. And here you add an input field and understand that adjustResize does not work for you.


You can:


  1. To score, - the user will close the keyboard himself to see what is under it.
  2. Abandon translucent , style is not yours, everything will be old, reliable, like everyone else.
  3. As soon as the user starts scrolling, hide the keyboard. You are well done! All right, skype even under ios does it! And there is no transparency.
  4. Put all input fields on separate screens. As a bonus - every extra transition to the screen, minus convenience and conversion.
  5. Cut down your adjustResize .

All translucent complexity


There are several difficulties with translucent implementation:


  1. It is necessary to take into account the design for devices less KITKAT (I think that in 2019, this is almost irrelevant, especially for new projects). Perhaps, for devices less than LOLLIPOP can a game of candle is not worth it? You can’t get transparent NavigationBar and “Man” on such devices either.
  2. If you are thinking about translucent , then you most likely want something more complicated than a static background, something scrolling under the StatusBar and NavigationBar, something that can not be done with fitsSystemWindows , which means that the height of the StatusBar and NavigationBar will have to be dynamically substituted.
  3. And finally, the main thing: adjustResize works only without translucent or for those View which fitsSystemWindows .

What it is about and how it should work



Solution to the problem


It seems to be a simple solution: adjustResize does not work, it means that you have to respond to the opening of the claviarot yourself. But the trouble android does not provide not only the height of the keyboard (and it may be different even for different EditText fields), but even the fact of its opening or closing. There is no official way. How so google?


Of course there is a solution! And when you have information about the current height of the keyboard, you can do with it what you want: add padding or margin to the bottom for those View, which should respond to the keyboard and now custom adjustResize is ready.


Code


To understand that the keyboard has been opened, use OnGlobalLayoutListener . Embed pieces of code in the post does not see the point. Why write 100 lines about what is expressed in code over 50?
The solution to the problem is presented in the repository .


Take a closer look at KeyboardHeightProvider and BaseTranslucentActivity .


The project is as simple as possible, understandable, there is nothing superfluous in it that is not related to the problem.
There is an example for a simple TextView, RecyclerView and ScrollView. The same technique was tested and works fine for NestedScrollView, NestedFragments, CoordinatorLayout, ViewPager, etc.


Ps: https://developer.android.com/reference/android/view/WindowInsets solves the problem with the keyboard as well.



Source: https://habr.com/ru/post/436646/