How can i anchor a Snackbar to the android soft navigation bar?

Thank you in advance!

I decided to implement a Snackbar with this method (which will later be called in onClick method):

public void showSnackbar(int selector, int textSelector){

    String sOne = (String) getResources().getText(R.string.charPart1);
    String sTwo = String.valueOf(selector);
    String sThree = (String) getResources().getText(R.string.charPart2);
    String sFour = sOne.concat(sTwo.concat(sThree));
    String sFive = (String) getResources().getText(R.string.charPart4);
    String sSix = sFour.concat(sFive);

    Snackbar notification = Snackbar.make(getWindow().getDecorView().getRootView(), "PLACEHOLDER", Snackbar.LENGTH_SHORT);

    if (textSelector==1){

        notification.setText(sFour);
        notification.show();

    }

    else if (textSelector==2){

        notification.setText(sSix);
        notification.show();

    }
}

I am facing this problem:

The Snackbar is displayed behind the system navigation bar:

enter image description here

My first idea was, that i could use the method Snackbar.setAnchorView(View anchorView) to position the Snackbar above the system navigation bar / soft navigation bar.
Description of setAnchorView(View anchorView):

By default, Snackbars will be anchored to the bottom edge of their parent view. However, you can use the setAnchorView method to make a Snackbar appear above a specific view within your layout, e.g. a FloatingActionButton. This is especially helpful if you would like to place a Snackbar above navigational elements at the bottom of the screen, such as a BottomAppBar or BottomNavigationView.

I would like to position the Snackbar above the system navigation bar, but i don´t know how to query the view id of the system navigation bar.

 Snackbar notification = Snackbar.make(getWindow().getDecorView().getRootView(), "PLACEHOLDER", Snackbar.LENGTH_SHORT);

 // View systemNavigationView = findViewById(int id) ;

 notification.setAnchorView(systemNavigationView);
 notification.show();

Is it at all possible to assume that the android system navigation bar has an assigned id that can be retrieved?

I only found this method for detecting if it exists, but no approach for getting the View or the id of the view:

public boolean hasNavBar (Resources resources)
{
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    return id > 0 && resources.getBoolean(id);
}

Upvotes: 0

Views: 850

Answers (1)

Amjad Alwareh
Amjad Alwareh

Reputation: 3281

You should place your snackbar inside a CoordinatorLayout view.

Upvotes: 0

Related Questions