How to Solve the Occlusion Problem of Virtual Key Bar by Android

  • 2021-12-11 19:12:15
  • OfStack

Recently, in the company's project, Huawei users reported a problem. There is a virtual button bar at the bottom of Huawei's mobile phone to block the bottom content of the application. Now this problem has been solved. Record it for reference for children's shoes who encounter the same problem.

The solution here is relatively simple. First, judge whether there are virtual keys on the user's mobile phone. If there are, get the height of the virtual keys, and then set TextView with the same height with code, so that the virtual keys on the mobile phone will not block the contents at the bottom.

Handle virtual keypad tool classes:


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

  /**
   *  Check whether there is a virtual keypad bar 
   *
   * @param context
   * @return
   */
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  public static boolean hasNavBar(Context context) {
    Resources res = context.getResources();// Read system resource function 
    int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");// Getting resources id
    if (resourceId != 0) {
      boolean hasNav = res.getBoolean(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 !ViewConfiguration.get(context).hasPermanentMenuKey();
    }
  }

  /**
   *  Determine whether the virtual key bar is rewritten 
   * @return
   */
  private static String getNavBarOverride() {
    String sNavBarOverride = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      try {
        Class c = Class.forName("android.os.SystemProperties");
        Method m = c.getDeclaredMethod("get", String.class);
        m.setAccessible(true);
        sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
      } catch (Throwable e) {
      }
    }
    return sNavBarOverride;
  }
}

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


// Handle virtual keys 
// Judge whether the user's mobile phone model has a virtual key bar        
 if(ScreenUtils.hasNavBar(getApplicationContext())){
  setNavigationBar();
  }

 // Handle virtual keys 
 private void setNavigationBar() {
  int barHeight = ScreenUtils.getNavigationBarHeight(getApplicationContext());
  LinearLayout.LayoutParams barParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  TextView tv = new TextView(this);
  tv.setHeight(barHeight);
  tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
  tv.setBackgroundColor(Color.BLACK);
  llNavigationBar.addView(tv,barParams);
 }

It's over here!

The above is Android how to solve the problem of virtual key bar occlusion details, more information about Android virtual key bar occlusion please pay attention to other related articles on this site!


Related articles: