Hello everyone, I am Ling from Mengxin Studio. Recently, when I was helping customers modify Android programs, I asked a button to float above the keyboard. Let’s talk about the implementation method below:
It's actually very simple, in three steps
Step 1 Get the current screen height
Display defaultDisplay = ().getDefaultDisplay(); Point point = new Point(); (point); height = ;
Step 2 Get the height of the visible area of the current screen to determine whether the current keyboard is hidden or displayed.
public void setFloatView(View root,View floatview){ = root; //Root node listener = new () { @Override public void onGlobalLayout() { Rect r = new Rect(); ().getDecorView().getWindowVisibleDisplayFrame(r); int heightDifference = height - ( - ); // The actual height is minus the view height, which is the keyboard height boolean isKeyboardShowing = heightDifference > height / 3; if(isKeyboardShowing){ //Keyboard display }else{ //Hide the keyboard } } }; ().addOnGlobalLayoutListener(listener); }
Step 3: Let the button animation move to the original position when the keyboard is hidden, and let the button animation move to the height of the current keyboard when the current keyboard is displayed.
if(isKeyboardShowing){ //Keyboard display ().translationY(-heightDifference).setDuration(0).start(); }else{ //Hide the keyboard ().translationY(0).start(); }
Then I encapsulated a tool class FloatBtnUtil for convenience, which is very useful, the following is the code
/** * Meng Xinling Implement button floating tool */ public class FloatBtnUtil { private static int height = 0; private Activity mcontext; private listener; private View root; public FloatBtnUtil(Activity mcontext){ = mcontext; if (height == 0){ Display defaultDisplay = ().getDefaultDisplay(); Point point = new Point(); (point); height = ; } } public void setFloatView(View root,View floatview){ = root; //View root node floatview // View component that needs to be displayed on the keyboard listener = new () { @Override public void onGlobalLayout() { Rect r = new Rect(); ().getDecorView().getWindowVisibleDisplayFrame(r); int heightDifference = height - ( - ); boolean isKeyboardShowing = heightDifference > height / 3; if(isKeyboardShowing){ ().translationY(-heightDifference).setDuration(0).start(); }else{ ().translationY(0).start(); } } }; ().addOnGlobalLayoutListener(listener); } public void clearFloatView(){ if (listener != null && root != null) ().removeOnGlobalLayoutListener(listener); } }
Here is the usage code:
private void initFloatBtn() { FloatBtnUtil floatBtnUtil = new FloatBtnUtil(this); LinearLayout lin_bottom = (LinearLayout) (.lin_bottom); LinearLayout lin_root = (LinearLayout)(.lin_root); (lin_root,lin_bottom); }
Summarize
This is the article about the floating button on the keyboard of Android. For more related contents of the floating button on the keyboard of Android, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!