SoFunction
Updated on 2025-04-08

How to solve the problem of virtual keybar occlusion on Android

Recently, Huawei users reported a problem in the company's project. There is a virtual keybar at the bottom of Huawei's mobile phone to block the bottom of the application. Now this problem has been solved. Let's record it and give reference to children's shoes that encounter the same problem.

The solution here is relatively simple. First, determine whether there is a virtual button on the user's phone. If it exists, then obtain the height of the virtual button, and then use the code to set a TextView of the same height, so that the virtual buttons of the phone will not block the content at the bottom.

Handle virtual keybar tool class:

public class ScreenUtils {
  //Get the height of the virtual button  public static int getNavigationBarHeight(Context context) {
    int result = 0;
    if (hasNavBar(context)) {
      Resources res = ();
      int resourceId = ("navigation_bar_height", "dimen", "android");
      if (resourceId > 0) {
        result = (resourceId);
      }
    }
    return result;
  }

  /**
    * Check if there is a virtual keybar
    *
    * @param context
    * @return
    */
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  public static boolean hasNavBar(Context context) {
    Resources res = ();//Read the system resource function    int resourceId = ("config_showNavigationBar", "bool", "android");//Get resource id    if (resourceId != 0) {
      boolean hasNav = (resourceId);
      // check override flag
      String sNavBarOverride = getNavBarOverride();
      if ("1".equals(sNavBarOverride)) {
        hasNav = false;
      } else if ("0".equals(sNavBarOverride)) {
        hasNav = true;
      }
      return hasNav;
    } else { // fallback
      return !(context).hasPermanentMenuKey();
    }
  }

  /**
    * Determine whether the virtual keybar is rewrite
    * @return
    */
  private static String getNavBarOverride() {
    String sNavBarOverride = null;
    if (.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      try {
        Class c = ("");
        Method m = ("get", );
        (true);
        sNavBarOverride = (String) (null, "");
      } catch (Throwable e) {
      }
    }
    return sNavBarOverride;
  }
}

Call the tool-class method to get the virtual key height:

//Processing virtual keys//Distinguish whether the user's mobile phone model has a virtual keybar if((getApplicationContext())){
  setNavigationBar();
  }

 //Processing virtual keys private void setNavigationBar() {
  int barHeight = (getApplicationContext());
   barParams = new (.MATCH_PARENT,.WRAP_CONTENT);
  TextView tv = new TextView(this);
  (barHeight);
  (.MATCH_PARENT);
  ();
  (tv,barParams);
 }

It's over here!

The above is the detailed content of how Android solves the problem of virtual keybar occlusion. For more information about Android virtual keybar occlusion, please follow my other related articles!