Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
This is the code I use to detect when the
Keyboard Height
changes.
Only the problem is that the
Statur Bar
color disappears and turns white when this code runs.
ViewCompat.setOnApplyWindowInsetsListener(this.getWindow().getDecorView(), (v, insets) -> {
int keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
//Do your job here
Log.d("Keyboard height: ", String.valueOf(keyboardHeight));
SharedPreferences preferences = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if (keyboardHeight > 0) {
bottom.getLayoutParams().height = 0;
editor.putInt("keyboard_height", keyboardHeight);
} else {
bottom.getLayoutParams().height = preferences.getInt("keyboard_height", 500);
editor.apply();
return insets;
Any alternative code that doesn't alter the Status Bar
color?
Or any way to programmatically re-add the Status Bar
color after this code runs?
You can solve this by consuming the insets dispatch event by returning WindowInsetsCompat.CONSUMED
instead of insets
Per documentation:
This can be used during insets dispatch in the view hierarchy by returning this value from View.onApplyWindowInsets(WindowInsets) or onApplyWindowInsets to stop dispatch the insets to its children to avoid traversing the entire view hierarchy.
The application should return this instance once it has taken care of all insets on a certain level in the view hierarchy, and doesn't need to dispatch to its children anymore for better performance.
UPDATE:
This does work, seems like it just pushes the action bar up to the top of the screen, leaving the action bar higher than normally. But I guess I can just add some height and padding to the top of the action bar to prevent this.
Well, that does seem to make the activity full screen, even intersects with the navigation bar as well.
Now to fix that return the following instead:
ViewCompat.onApplyWindowInsets(v, insets)
ViewCompat.setOnApplyWindowInsetsListener(this.getWindow().getDecorView(), (v, insets) -> {
int keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
//Do your job here
Log.d("Keyboard height: ", String.valueOf(keyboardHeight));
SharedPreferences preferences = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if (keyboardHeight > 0) {
bottom.getLayoutParams().height = 0;
editor.putInt("keyboard_height", keyboardHeight);
} else {
bottom.getLayoutParams().height = preferences.getInt("keyboard_height", 500);
editor.apply();
return ViewCompat.onApplyWindowInsets(v, insets);
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.