How to keep the toolbar visible with adjustPan?
Asked Answered
S

4

51

I have a layout like the one below and I would like to have the following behavior when the user taps on the EditText:

  1. Keyboard should overlay the "LinearLayout aligned to the bottom";
  2. EditText should be visible on the screen, not overlaid by the keyboard;
  3. The toolbar should be visible on the screen;

My layout

enter image description here

Expected behaviour

enter image description here

Actual result with windowSoftInputMode = adjustPan

Requirements not met:

  1. Toolbar should be visible on screen;

enter image description here

Actual result with windowSoftInputMode = adjustResize

Requirements not met:

  1. Keyboard should overlay the "LinearLayout aligned to the bottom";

enter image description here

Actual result with windowSoftInputMode = adjustNothing

Requirements not met:

  1. EditText should be visible on screen, not overlaid by the keyboard;

enter image description here

Has anyone faced the same issue and achieved the requirements?

Shipment answered 4/3, 2016 at 12:57 Comment(1)
Is this in full screen mode or have status bar ?Neutrality
A
1

I am not sure that there is any out of box solution to make it works as you expect.

However, you can achieve the specified behavior by using windowSoftInputMode = adjustNothing and setting focus listener to edit text, and scroll the scroll view, when edit text gets focus.

If it is not clear how to implement scrolling I can add an example.

Ariew answered 21/3, 2016 at 17:40 Comment(0)
G
0

Change the Parent layout to Relative layout

public class SizeNotifierRelativeLayout extends RelativeLayout {

private Rect rect = new Rect();
public SizeNotifierRelativeLayoutDelegate delegate;

public abstract interface SizeNotifierRelativeLayoutDelegate {
    public abstract void onSizeChanged(int keyboardHeight);
}

public SizeNotifierRelativeLayout(Context context) {
    super(context);
}

public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs) {
    super(context, attrs);
}

public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}


/**
 * Calculate the soft keyboard height and report back to listener
 * @param changed
 * @param l
 * @param t
 * @param r
 * @param b
 */
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    if (delegate != null) {
        View rootView = this.getRootView();
        int usableViewHeight = rootView.getHeight() - AndroidUtilities.statusBarHeight - AndroidUtilities.getViewInset(rootView);
        this.getWindowVisibleDisplayFrame(rect);
        int keyboardHeight = usableViewHeight - (rect.bottom - rect.top);
        delegate.onSizeChanged(keyboardHeight);
    }
}

Next on your Activity or Fragment include this codes

 private SizeNotifierRelativeLayout sizeNotifierRelativeLayout;
  View rootView;


       @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

          rootView = view.findViewById(R.id.root);
          sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout) 
                                        view.findViewById(R.id.chat_layout);
                sizeNotifierRelativeLayout.delegate = this;


  rootView.getViewTreeObserver().addOnGlobalLayoutListener(new 
  ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    rootView.getWindowVisibleDisplayFrame(r);
                    int screenHeight = rootView.getRootView().getHeight();


                    int keypadHeight = screenHeight - r.bottom;

                    Log.d("Height", "keypadHeight = " + keypadHeight);

                    if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                        // keyboard is opened
                        if (sizeNotifierRelativeLayout != null) {
                            sizeNotifierRelativeLayout.setPadding(0, 0, 0, keypadHeight - 200);
                        }
                    } else {
                        // keyboard is closed
                        if (sizeNotifierRelativeLayout != null) {
                            sizeNotifierRelativeLayout.post(new Runnable() {
                                public void run() {
                                    if (sizeNotifierRelativeLayout != null) {
                                        sizeNotifierRelativeLayout.setPadding(0, 0, 0, 0);
                                    }
                                }
                            });
                        }
                        try {

                            }
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                            System.out.print(e.getMessage());
                        }
                    }
                }
            });


            }
Gymnosperm answered 8/11, 2017 at 8:17 Comment(0)
N
0

I did the following to resolve this issue.

AndroidManifest file activity config:

<activity
        android:name=".chat.app.activity.ChatPageActivity"
        android:configChanges="orientation|screenLayout"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
Needle answered 13/8, 2020 at 11:8 Comment(0)
V
0

try this. it will scroll your edittext to focus mode and open the keyboard for it.

EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) 
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Valuation answered 17/7, 2021 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.