Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change back button to arrow-down in Navigation Bar

I'm using an AppCompatDialogFragment to show a BottomSheetDialog. Clicking the back button on the Navigation Bar closes the BottomSheetDialog.

I want to change the icon on the Navigation Bar from the back button to the 'arrow-down' icon. This is by default done when the keyboard is shown and I want to replicate it for the Bottom Sheet.

To be clear, here what I have:

Current

And here is what I need:

This is what I want

Note that the back button is an "arrow-down".

The navigation-bar is a system-ui component and I don't see the way to change it's appearance to look like the navigation-bar displayed when the keyboard is visible.

like image 549
Vlad Avatar asked Aug 16 '17 19:08

Vlad


2 Answers

In android O the concept of changing the icon is introduced but it is still through the 3rd party app. Custom Navigation Bar that uses WRITE_SECURE_SETTINGS to change the icons. In Android O you can change the display of the bar i.e Light or Dark theme.

Solution 2 can be more of a help to you. You can create a popup window on navigation Bar with the desired layout i.e 3 buttons back, Recent Apps and home button. In this way you can change the back button icon accordingly. Make sure the pop up window is of same height as the navigation Bar and you can then make your own functions for home and recent apps and in back function you can close your BottomSheetDialog and remove that popup window.

The below is the code for home key as well as recent Apps. For back button do accordingly what you want to achieve with your own icon.

For Home Button.

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

For Recent Apps.

Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClass.getMethod("getService", String.class);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerClass, "statusbar");
Class statusBarClass = Class.forName(retbinder.getInterfaceDescriptor());
Object statusBarObject = statusBarClass.getClasses()[0].getMethod("asInterface", IBinder.class).invoke(null, new Object[] { retbinder });
Method clearAll = statusBarClass.getMethod("toggleRecentApps");
clearAll.setAccessible(true);
clearAll.invoke(statusBarObject);

For Back Button // use your icon and function of closing the BottomSheetDialog.

For calculating the height of the navigationBar

public static int getSoftButtonsBarSizePort(Activity activity) {
    // getRealMetrics is only available with API 17 and +
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight)
            return realHeight - usableHeight;
        else
            return 0;
    }
    return 0;
}

you can also do it by adb commands but make sure it can mess up your navigationBar and you cannot get back your original navigationBar.

I hope it helps.

like image 94
Sahil Avatar answered Nov 13 '22 03:11

Sahil


As already answered, it will be introduced in Android O.

For prior versions it's only possible for Android 3.x, or 4.4+ using flags as SYSTEM_UI_FLAG_IMMERSIVE, SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, SYSTEM_UI_FLAG_HIDE_NAVIGATION, etc. But still very limited, putting a lot of effort you get a result not very friendly or even glitchy.

The reason because it isn't possible, it because is a security issue. Apps could prevent to users to exit of the app.

Look at these links :

  • Android overlay on top of software buttons
  • Hide System Bar in Tablets
  • http://room-15.github.io/blog/2015/03/17/overlaying-the-system-navigation-bar/
like image 34
crgarridos Avatar answered Nov 13 '22 03:11

crgarridos