android Implementation Button Floating Above the Keyboard Instance Code

  • 2021-11-29 08:32:27
  • OfStack

Hello everyone, I am the spirit of Mengxin Studio. Recently, when helping customers modify Android programs, it is required that a button should float above the keyboard. The following is about 1 implementation method:

In fact, it is very simple. It is divided into three steps

Step 1 Get the height of the current screen


 Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
   Point point = new Point();
   defaultDisplay.getSize(point);
   height = point.y;

Step 2: Obtain the height of the visible area of the current screen, which is used to judge whether the current keyboard is hidden or displayed


public void setFloatView(View root,View floatview){
  this.root = root; // Root node 
  listener = new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
    Rect r = new Rect();
    mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
    int heightDifference = height - (r.bottom - r.top); //  The actual height minus the visible height is the keyboard height 
    boolean isKeyboardShowing = heightDifference > height / 3;
    if(isKeyboardShowing){
     // Keyboard display  
    }else{
     // Keyboard hiding  
    }
   }
  };
  root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
 }

Step 3 When the keyboard is hidden, let the button animation move to the original position, and when the current keyboard is displayed, let the button animation move to the top of the current keyboard height


 if(isKeyboardShowing){
     // Keyboard display 
     floatview.animate().translationY(-heightDifference).setDuration(0).start();
    }else{
     // Keyboard hiding 
     floatview.animate().translationY(0).start();
    }

Then I encapsulated a tool class FloatBtnUtil for convenience, which is very easy to use. The following is the code


/**
 *  Meng Xin Ling   Implement Button Floating Tool 
 */
public class FloatBtnUtil {

 private static int height = 0;
 private Activity mcontext;
 private ViewTreeObserver.OnGlobalLayoutListener listener;
 private View root;

 public FloatBtnUtil(Activity mcontext){
  this.mcontext = mcontext;
  if (height == 0){
   Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
   Point point = new Point();
   defaultDisplay.getSize(point);
   height = point.y;
  }
 }

 public void setFloatView(View root,View floatview){
  this.root = root; // View root node  floatview //  Object that needs to be displayed on the keyboard View Component 
  listener = new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
    Rect r = new Rect();
    mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
    int heightDifference = height - (r.bottom - r.top);
    boolean isKeyboardShowing = heightDifference > height / 3;
    if(isKeyboardShowing){
     floatview.animate().translationY(-heightDifference).setDuration(0).start();
    }else{
     floatview.animate().translationY(0).start();
    }
   }
  };
  root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
 }

 public void clearFloatView(){
  if (listener != null && root != null)
  root.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
 }

}

Here's the code to use:


 private void initFloatBtn() {
  FloatBtnUtil floatBtnUtil = new FloatBtnUtil(this);
  LinearLayout lin_bottom = (LinearLayout) this.findViewById(R.id.lin_bottom);
  LinearLayout lin_root = (LinearLayout)this.findViewById(R.id.lin_root);
  floatBtnUtil.setFloatView(lin_root,lin_bottom);
 }

Summarize


Related articles: