Since the only answer at the time of writing is Kotlin, here is a Java alternative:
import android.content.res.Resources;
import android.support.v4.view.OnApplyWindowInsetsListener;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.WindowInsetsCompat;
import android.view.View;
public class NavigationBar {
final int BOTTOM = 0;
final int RIGHT = 1;
final int LEFT = 2;
final int NONE = 3;
private int LOCATION = NONE;
private View view;
NavigationBar(View view) {
this.view = view;
}
int getNavBarLocation() {
ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
if (insets.getSystemWindowInsetBottom() != 0)
LOCATION = BOTTOM;
else if (insets.getSystemWindowInsetRight() != 0)
LOCATION = RIGHT;
else if (insets.getSystemWindowInsetLeft() != 0)
LOCATION = LEFT;
else
LOCATION = NONE;
return insets;
}
});
return LOCATION;
}
int getNavBarHeight() {
Resources resources = view.getResources();
int resourceId = resources.getIdentifier(
"navigation_bar_height", "dimen", "android");
if (resourceId > 0)
return resources.getDimensionPixelSize(resourceId);
return 0;
}
}
I have also included how to get the height, since that will often be the next step.
In your activity, you would then call the methods using a view-based reference:
NavigationBar navigationBar = new NavigationBar(snackbarLayout);
if (navigationBar.getNavBarLocation() == navigationBar.BOTTOM) {
FrameLayout.LayoutParams parentParams =
(FrameLayout.LayoutParams) snackbarLayout.getLayoutParams();
parentParams.setMargins(0, 0, 0, 0 - navigationBar.getNavBarHeight());
snackbarLayout.setLayoutParams(parentParams);
}
(This example is based on a common issue with inconsistent SnackBar margins)