Compatibility of Android Bug 5497 Workaround

Keywords: Android Mobile

background

Recently, when adding an immersive status bar to the project, we encountered a strange problem. The chat and comment boxes at the bottom of the Android 4.4 system can not be topped by the system input method. Figure 1 below (pictures quoted from Two other ways of realizing android immersion status bar More detailed problem descriptions can also be accessed from this link.

Figure 1.png


The reason is that android:fitsSystemWindows="true" is not set after the status bar is transparent. My solution is to use AndroidBug5497Workaround Class dynamically calculates the height of the layout. The original code of this method has a high degree of computational inaccuracy compatibility in Huawei and other mobile phones. I have made some modifications on the basis of the source code. At present, tests on Huawei, Millet, Samsung and other mobile phones are normal. The revised code is as follows.

 public class AndroidBug5497Workaround {    
   public static void assistActivity(Activity activity) {      
   new AndroidBug5497Workaround(activity);   
   }  
  private View mChildOfContent;   
  private int usableHeightPrevious;  
  private FrameLayout.LayoutParams frameLayoutParams;  
  private int contentHeight;  
  private   boolean isfirst = true;  
  private   Activity activity; 
  private  int statusBarHeight;  

  private AndroidBug5497Workaround(Activity activity) {      
  //Get the height of the status bar        
  int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");        
  statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);      
  this.activity = activity;
  FrameLayout content = (FrameLayout)activity.findViewById(android.R.id.content);        
  mChildOfContent = content.getChildAt(0);   

  //This listening event is invoked when the interface changes        
  mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {           
  public void onGlobalLayout() {              
      if (isfirst) { 
            contentHeight = mChildOfContent.getHeight();//Compatible with Huawei and other models                    
            isfirst = false;  
       }               
       possiblyResizeChildOfContent();         
    }      
   });     

   frameLayoutParams = (FrameLayout.LayoutParams)     
   mChildOfContent.getLayoutParams();    
 } 

  //Re-adjust height with layout 
 private void possiblyResizeChildOfContent() {  

  int usableHeightNow = computeUsableHeight();       

  //Inconsistent layout changes between current and last visible heights      
  if (usableHeightNow != usableHeightPrevious) {            
    //Int usable Height Sans Keyboard 2 = mChildOfContent. getHeight (); //compatible with Huawei and other models
    int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();    
    int heightDifference = usableHeightSansKeyboard - usableHeightNow;          
    if (heightDifference > (usableHeightSansKeyboard / 4)) {          
     // keyboard probably just became visible        
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){                    
    //frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;                    
    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;                
     } else {                
     frameLayoutParams.height = usableHeightSansKeyboard -heightDifference;                
     }            
   } else {     
   frameLayoutParams.height = contentHeight;        
   }   

   mChildOfContent.requestLayout();       
   usableHeightPrevious = usableHeightNow;    
   }  
 }   
 /**     * Calculate mChildOfContent Visible Height **@return     */    
 private int computeUsableHeight() {  
   Rect r = new Rect();     
   mChildOfContent.getWindowVisibleDisplayFrame(r);       
   return (r.bottom - r.top);  
   }
}

Matters needing attention:
The limitation of using this method is that the layout must contain adjustable sizes such as ListView,ScrollView, etc., and the maximum height (using weight) must be at least greater than the sum of the height of the soft keyboard and the input box. The normal chat interface itself is a ListView, so don't worry about it.

Usage method

Call AndroidBug5497 Workaround. assistActivity (this); in your Activity's oncreate() method. Note: Called after setContentView(R.layout.xxx).

A more detailed introduction to Android Bug 5497 Workaround can be found here. Layout Computing of android Soft Keyboard in Full Screen

Posted by ibelimb on Sun, 14 Jul 2019 15:10:34 -0700