SoFunction
Updated on 2025-04-08

Implement an input bar suspended on a soft keyboard using Android

Preface

We want to implement an input bar (i.e. a floating bar) that is suspended on the soft keyboard. We encountered many problems during the process. We checked some online articles and found that many of them were wrong and took some detours. Let’s record them one by one.

Floating bar

Implementing a floating bar is simple

();
(true);
(true);
();
InputMethodManager inputManager = (InputMethodManager)().getSystemService(Context.INPUT_METHOD_SERVICE);
(chatInputEt, 0);

chatInputPanel is the entire layout of the floating bar, mChatPanelContent is the actual part of the floating bar, chatInputEt is the EditText in it. You can suspend the chatInputPanel on the software disk by making some settings.

Here chatInputPanel is full screen (click outside mChatPanelContent to hide the keyboard), mChatPanelContent is at the bottom of its bottom, and is hidden by default (INVISIBLE).

Full screen soft keyboard when horizontal screen

When screening horizontally, Android will display the soft keyboard in full screen by default, so that the floating bar cannot be realized. So the full screen display needs to be cancelled

Use android:imeOptinos in EditText to set some interfaces on the soft keyboard that comes with Android

  • android:imeOptions="flagNoExtractUi" //Make the soft keyboard not display in full screen, only occupying part of the screen

  • android:imeOptions="actionNone" //There is no prompt on the right side of the input box

  • android:imeOptions="actionGo" //The content of the button in the lower right corner is 'Start'

  • android:imeOptions="actionSearch" //The button in the lower right corner is a magnifying glass picture, search

  • android:imeOptions="actionSend" //The content of the button in the lower right corner is 'Send'

  • android:imeOptions="actionNext" //The content of the button in the lower right corner is 'Next'

  • android:imeOptions="actionDone" //The content of the button in the lower right corner is 'complete'

So we set android:imeOptions="flagNoExtractUi" for EditText to achieve full screen display when screening horizontally. At the same time, EditText may add a corresponding listener to capture the listening event that the user clicked the button in the lower right corner of the soft keyboard for processing.

(new OnEditorActionListener() {   
        @Override  
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {   
            (, "text2", Toast.LENGTH_SHORT).show();   
            return false;   
        }   
    });  

Monitoring soft keyboard (this method is unreliable, abandoned, there are reliable ones below)

Note: This is a wrong method online, so I will give it a special look. If you are not interested, just go and read it (3).

There is no problem with the display, but when the soft keyboard is hidden, the floating bar requires the synchronous hidden position of the floating bar.

The system does not provide a API for listening soft keyboards to close, so we can only implement it ourselves.

().addOnGlobalLayoutListener(new () {
    @Override
    public void onGlobalLayout() {
        if(() > ().getHeight() / 2){
            ();
        }
        else{
            ();
        }
    }
});

Listen to the layout changes of chatInputPanel (the overall layout of the floating bar), hide when the bottom is greater than half the height of the rootview, otherwise it will be displayed.

Because our function is horizontal, when the keyboard pops up, the bottom must be less than half of the rootview height (screen width) because the keyboard is suspended on the keyboard.

When you close the keyboard, the chatInputPanel will return to the bottom (the setting is at the bottom of the parent layout), so the bottom must be greater than half.

This method is not reliable, and redrawing will cause frequent execution of onGlobalLayout. Although it can be added to control it, it is not recommended to use this method to monitor the soft keyboard. Let's take a look at another method.

Reliable method of monitoring soft keyboard

Why don't you consider the above method? It is because the full screen display of FLAG_FULLSCREEN (hidden notification bar) causes the problem

When we need to display the hidden notification bar in full screen, the FLAG_FULLSCREEN attribute will be used

getActivity().getWindow().setFlags(
        .FLAG_FULLSCREEN,
        .FLAG_FULLSCREEN);

However, it will affect the floating bar above, because we found that () has not changed at all times, but we judge that the display and hide depend on this change.

The change is caused by the processing method of Android in full screen FLAG_FULLSCREEN. There will be many problems with the soft keyboard during full screen, and there are many on this website.

How to solve it?

Let's listen to the soft keyboard in another way

getActivity().getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new () {
    @Override
    public void onGlobalLayout() {
        Rect rect = new Rect();
        (rect);
        int rootHeight=().getHeight();

        int displayHeight=();
        int diffHeight=rootHeight-displayHeight;
        if(diffHeight==0){
            //Collapse the keyboard            ();
        }else{
            //The keyboard pops up            ();
        }
    }
});

Judging by monitoring the changes in the content layout of the root layout type, this method is the most reliable at present.

But there is still a small problem, that is, the keyboard will cover a small part of the bottom of the floating bar in full screen state. What should I do?

The ultimate way to levitate

The above solves the monitoring problem of the soft keyboard, but the floating bar will always be blocked in full screen state. What should I do?

In fact, there is another problem here. When the keyboard is displayed, the overall layout in the app is pushed upward, which leads to some components being reduced, etc.

We need to solve this problem first, keep the overall layout of the app still and the keyboard covers it. This needs to be manually set before the keyboard is popped up, as follows:

(new () {
    @Override
    public void onClick(View v) {
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        (-);//Solve the problem of possible pushing up for the first time        
        (true);
        (true);
        ();
        InputMethodManager inputManager = (InputMethodManager)().getSystemService(Context.INPUT_METHOD_SERVICE);
        (chatInputEt, 0);
    }
});

In this way, the floating bar will not be pushed up (guessed that it is related to the keyboard mechanism, because if the keyboard pops up, it is easy to readjust the window if the input component with focus is blocked. We place the floating window at the top, and the keyboard will not block the EditText in the focus, so the window will not be readjust).

But in this way, the floating bar will be invisible, and we can see that the(); code is removed here, so how to display it?

We mentioned above that we used OnGlobalLayoutListener to monitor the keyboard. We can just display it here, and at the same time optimize the display position. Here we calculate how much the window display area is moved upward, so that the chatInputPanel can also move upward, such as:

private int mLastHeight = 0;
getActivity().getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new () {
    @Override
    public void onGlobalLayout() {
        Rect rect = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int height = ();
        int rawHeight = getResources().getDisplayMetrics().heightPixels - ;
        if (height == mLastHeight)
            return;
            
        if (height < rawHeight) {
            (new Runnable() {
                @Override
                public void run() {
                    ();
                    (-(rawHeight - height));
                }
            }, 200);
        } else {
            (new Runnable() {
                @Override
                public void run() {
                    ();
                }
            }, 100);
        }
                
        mLastHeight = height;    
    }
});

You can see that you first get the display height of the current window and the actual height of the screen (window part)

Then first determine whether the display area of ​​the window has changed, and if it has not changed, it will not be processed.

If there is any change, it is judged to be larger or smaller.

If it gets smaller

Indicates that the keyboard pops up, and the chatInputPanel is displayed at this time. At the same time, the translationY is set to -(rawHeight - height)

First of all, the bottom of the initial position of the chatInputPanel is aligned with the bottom of the screen. Although setY is set, setY is actually setTranslationY. The initial position has not changed. Source code:

public void setY(float y) {
    setTranslationY(y - mTop);
}

If you want to display it above the keyboard after bounced up, you need to move the height of the keyboard from the bottom up. The keyboard height is rawHeight - height, so moving upward is to set translationY to -(rawHeight - height).

If it gets bigger

Instructions to close the keyboard and hide the chatInputPanel.

This not only solves the problem of window pushing, but also solves the problem of soft keyboard blocking part of the floating bar, because the position of the floating bar is calculated, and does not change the position by pushing on the soft keyboard, causing layout adjustment.

Final code

Finally, I wanted to form an independent component to directly use it. I found that there were many problems during the writing process. After solving all the problems, I found that it was different from the above code. However, the idea was consistent, but the details were adjusted, such as getting the keyboard height, etc.

Summarize

This is the article about using Android to implement an input bar suspended on a soft keyboard. For more related contents of Android to implement a floating input bar, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!