Method of Android adapting bottom virtual keys

Recently, when adapting the project, it was found that some of the mobile phones (such as Huawei Mobile Phone) had virtual keys at the bottom. Because of the existence of virtual keys, some of the interfaces were blocked. Because of the need for full screen display, the virtual keys hiding method was called to hide them. However, the following problems were found:

  • Manual operation hides virtual keys and long white stripes appear
  • Do not hide automatically
  • After sliding out of the status bar, the virtual keys also come out. After the status bar is hidden, the virtual keys are not hidden.
  • Influenced SurfaceView Full Screen Display on devices without virtual keys (original Full Screen Display turned into a small screen display when it was cut out and re-entered)

Through many methods of google and try to solve this problem finally, achieve the following results:

  • Virtual keys are automatically hidden each time they enter the interface
  • Manual sliding out of the virtual button, automatically hiding when the screen is not operated
  • The virtual keys will follow when the status bar is sliding out, and it should be handled to hide them automatically when the status bar is not operating.

Specific code as follows:

public class TestActivity extends AppCompatActivity {

    private View decorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        //Get the top view
        decorView = getWindow().getDecorView();
    }

    @Override
    protected void onStart() {
        //Call configuration
        init();
        super.onStart();
    }

    private void init(){
        int flag = 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
                | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        //Judge that the current version is above 4.0 and there are virtual keys, otherwise do not operate
        if (Build.VERSION.SDK_INT < 19 || !checkDeviceHasNavigationBar()) {
        //Be sure to determine whether there are keystrokes or not, otherwise calls on mobile phones without keystrokes will affect other functions. If not taken into account before, resulting in full-screen image transmission into a small screen display.
            return;
        } else {
            // get attribute
            decorView.setSystemUiVisibility(flag);
        }
    }

    /**
     * Judging whether there are virtual keys
     * @return
     */
    public boolean checkDeviceHasNavigationBar() {
        boolean hasNavigationBar = false;
        Resources rs = getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class<?> systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {

        }
        return hasNavigationBar;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

Note: Sometimes you need to manually call the init() configuration method in the above code to hide the keys. For example, when dialog pops up, the virtual keys will come out. At this time, the hidden keyboard will be called manually, and the keyboard will pop up.

Record it for reference when you encounter this kind of problem in the future.

Keywords: Mobile Android SurfaceView Google

Added by mahakmx on Wed, 10 Jul 2019 23:20:46 +0300