Android Method for Hiding Virtual Keys at the Bottom of Mobile Phone

  • 2021-10-15 11:26:50
  • OfStack

Nowadays, there are many virtual buttons on the bottom of Android mobile phones, such as Huawei, nexus, Meizu, etc. Under normal circumstances, it has no effect on APP, but sometimes it must be forced to hide.

For example, when playing games and taking pictures.

Next, add a few methods to OK, and the code is as follows:


/** 
 *  Hide virtual keys and set them to full screen  
 */ 
 protected void hideBottomUIMenu(){ 
  if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api 
   View v = this.getWindow().getDecorView(); 
   v.setSystemUiVisibility(View.GONE); 
  } else if (Build.VERSION.SDK_INT >= 19) { 
   //for new api versions. 
   View decorView = getWindow().getDecorView(); 
   int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
     | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
     | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
     | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar 
      | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar 
     | View.SYSTEM_UI_FLAG_IMMERSIVE; 
   decorView.setSystemUiVisibility(uiOptions); 
   getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 
  } 
} 

Decompile framework-res. apk (available in the system/framework/ folder on your phone) and open res/values/bools. xml


/** 
 *  Check whether there is a virtual keypad bar  
 * @param context 
 * @return 
 */ 
  public static boolean hasNavBar(Context context) { 
   Resources res = context.getResources(); 
 // This way 1 Be careful to write correctly, and the interior should be called through reflection. 
   int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android"); 
   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; 
} 


Related articles: