Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust layout when soft keyboard is on

Tags:

android

I've seen in some applications the layout shifts when soft keyboard is shown. This is certainly not adjustPan because the whole layout (probably inner layout) shifts, not only the current EditText. This is for instance in Evernote login screen. Can you advice how this made?

like image 886
Eugene Avatar asked Sep 04 '11 16:09

Eugene


People also ask

What is window soft input mode?

The Android system shows an on-screen keyboard, known as a soft input method, when a text field in your UI receives focus.

What is soft keyboard in Android?

The soft keyboard (also called the onscreen keyboard) is the main input method on Android devices, and almost every Android developer needs to work with this component at some point.


2 Answers

Here's a solution that works like the Evernote login screen:

First, define a class that will be your special LinearLayout like this:

public class MyLayout extends LinearLayout {  public MyLayout(Context context, AttributeSet attrs) {     super(context, attrs); }  public MyLayout(Context context) {     super(context); }  private OnSoftKeyboardListener onSoftKeyboardListener;  @Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {     if (onSoftKeyboardListener != null) {         final int newSpec = MeasureSpec.getSize(heightMeasureSpec);          final int oldSpec = getMeasuredHeight();         if (oldSpec > newSpec){             onSoftKeyboardListener.onShown();         } else {             onSoftKeyboardListener.onHidden();         }     }     super.onMeasure(widthMeasureSpec, heightMeasureSpec); }  public final void setOnSoftKeyboardListener(final OnSoftKeyboardListener listener) {     this.onSoftKeyboardListener = listener; }  public interface OnSoftKeyboardListener {     public void onShown();     public void onHidden(); }  } 

This layout listens to measure changes, and if new measurements are < than the old ones, that means part of the screen is eaten by soft keyboard.

Though, for it to work, in your manifest you need to set android:windowSoftInputMode="adjustResize" so the content will be resized and not just shifted.

And the whole system works as follows: You have your layout:

<MyLayout id="layout">   <SomeImage id="image"/>   <SomeText>   <SomeInput> </MyLayout> 

It's like evernotes login screen. Then, in your activity:

((MyLayout)findViewById(R.id.layout)).setOnSoftKeyboardListener(new OnSoftKeyboardListener() {         @Override         public void onShown() {             findViewById(R.id.image).setVisibility(View.GONE);         }         @Override         public void onHidden() {             findViewById(R.id.image).setVisibility(View.VISIBLE);         }     }); 

Then go to manifest.xml and set

android:windowSoftInputMode="adjustResize" 

What will happen, is when soft keyboard is shown, it'll hide the image and will resize the rest of content. (You can actually see how text is resized in Evernote)

Image hide is, of course, one of the many things you can do. But you must be careful, since different layout changes will also call onMeasure.

Of course it's a dirty variant. You need to check for orientation changes, and the right time when actually take the measurements, and maybe some more logic when comparing the new specs with the old ones. But i think this is the only way to do it.

like image 107
Alex Orlov Avatar answered Sep 19 '22 11:09

Alex Orlov


Use "adjustResize" instead of "adjustPan" in AndroidManifest.xml

<activity     ...     android:windowSoftInputMode="adjustResize" /> 

From the documentation:

"adjustResize"
The activity's main window is always resized to make room for the soft keyboard on screen.

"adjustPan"
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

like image 31
naXa Avatar answered Sep 19 '22 11:09

naXa