1

I would like to put black background overlay (with ~60% alpha) on my Activity layout when software keyboard is being shown. When keyboard disappears - layout overlay also disappears. Basically, I want to achieve similar effect like in gmail app (see pictures bellow).

Keyboard shown (you can see black overlay with some alpha):

enter image description here

Keyboard dismissed (overlay disappeared also):

enter image description here

What is the best way to achieve such effect?

user2999943
  • 2,419
  • 3
  • 22
  • 34

2 Answers2

0

Get the background drawable of your "Activity layout" then set the alpha once the keyboard is displayed.

View backgroundView = findViewById(R.id.background); //id of your layout
Drawable background = backgroundView.getBackground();
background.setAlpha(153); //153 is 60% of 255 (completely opaque)
airowe
  • 794
  • 2
  • 9
  • 29
0

You can have a view on top of everything in your layout, that has 0 alpha when keyboard is hidden or 0.6 alpha when keyboard is visible, or you can research ViewOverlay and use it on top of the root view with the same logic.
To know when to make your overlay visible, use this

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        //Show overlay
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        //Hide overlay
    }
}

More on this keyboard changed code: link
More on ViewOverlay: link

Community
  • 1
  • 1
Andrei Tudor Diaconu
  • 2,157
  • 21
  • 26