37

enter image description here

As you can see my "Got It" button is behind the navigation bar.Not able to fix it!!! I have tried

<item name="android:fitsSystemWindows">true</item>  

As well as setting it in layout file.

my theme in value-21 is :

 <style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:fitsSystemWindows">true</item>
    </style>

Its the same case with all the screens throughout the application.

Please Help.

5
  • 1
    Try : View.bringToFront();
    – user863873
    Feb 3, 2015 at 16:03
  • 1
    @user863873:layout should not use this space.It should be above these buttons. Feb 3, 2015 at 16:11
  • Were you able to put "Got it" button in front of navigation bar? I just can't seem to make it happend. Can you help me? What other flags did you add to window? I added all your flags but the nav bar is always in front of my activity. Jun 13, 2016 at 10:09
  • @niteshgoel can you please answer? Jun 13, 2016 at 14:00
  • Yes,It worked that way.Please check my answer how I have solved it. Jun 14, 2016 at 11:05

9 Answers 9

45

Here is the solution.

Most of the layouts get solved by adding these properties in values-v21 style.xml

<item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:fitsSystemWindows">true</item>

for others, I have calculated the hight of navigation bar and add margin to my view .

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;
}

Note: By using the above solutions everything work but I was also using PopupWindow in my app.The layout of the PopupWindow get messed up in android L. Look for the issue and the solution here

3
  • 10
    I added the following to my style <item name="android:windowTranslucentNavigation">true</item> <item name="android:fitsSystemWindows">true</item> It works! No content is being drawn behind the navigation bar. Except now my navigation bar is a dark gray instead of black. Putting TranslucentNavigation to false makes the bar black, but content draws behind it. Think you could explain why these attributes work, and how to keep the navigation bar black?
    – klong15
    Jul 10, 2015 at 15:40
  • You have defined the method but you did not tell us how to use it or calling it. Can you kindly tell us where to call that method from and how to use what its returning.
    – Neri
    Jan 27, 2017 at 12:18
  • This helps but How to tell when the user opens or closes the soft navigation keys bar? So that we could resize the padding? Dec 11, 2017 at 14:27
7

It is so simple, just add this line to your parent layout xml:

android:fitsSystemWindows="true"
2
  • 4
    Not working on pie may be it's not working since android M Apr 25, 2019 at 10:47
  • Worked for me, tested in Android emulator running Android Pie
    – Lins Louis
    May 23, 2019 at 8:32
6

For nice user experience you should not need to block navigation keys area using android:windowTranslucentNavigation

rather here is the better solution, if you are using ResideMenu library then simply add this method in ResideMenu.java

  @Override
protected boolean fitSystemWindows(Rect insets) {
    int bottomPadding = insets.bottom;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            bottomPadding += resources.getDimensionPixelSize(resourceId);
        }
    }
    this.setPadding(viewActivity.getPaddingLeft() + insets.left, viewActivity.getPaddingTop() + insets.top,
            viewActivity.getPaddingRight() + insets.right, viewActivity.getPaddingBottom() + bottomPadding);
    insets.left = insets.top = insets.right = insets.bottom = 0;
    return true;
}

and if you are using SlidingMenu library then change mod to SLIDING_CONTENT by:

menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

Hope it will save your time

2
  • +1 I was facing same problem because of ResideMenu and this answer helped me after wasting so much time. Thanks! Aug 31, 2017 at 15:01
  • I am also using Reside menu. What to do for Android P Navigation Gesture functionality. In that navigation bar is hide then also I am getting navigation bar height instead of zero. Can you please help me.
    – Nik
    Mar 4, 2019 at 14:18
4

Simply set this in your Activity's onCreate(), while setting up content view :

int mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
int mScreenHeight = getWindowManager().getDefaultDisplay().getHeight();
View view = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(view, new ViewGroup.LayoutParams(mScreenWidth, mScreenHeight));
0
3

Use this code in your onCreate method of your Activity(If you have BaseActivity you can do this there so all activities apply this):

ViewCompat.setOnApplyWindowInsetsListener(
            findViewById(android.R.id.content), (v, insets) -> {
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
                params.bottomMargin = insets.getSystemWindowInsetBottom();
                return insets.consumeSystemWindowInsets();
            });

When a layout has this flag, it means that this layout is asking the system to be applied window insets. In our case that would mean, that FrameLayout desires to be applied a padding that is equal to status bar’s height.

1
  • 1
    For pre-lambda: .setOnApplyWindowInsetsListener (findViewById(android.R.id.content), new OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { // .. } } );
    – Gene Bo
    Jan 22, 2018 at 3:33
1

I tried many answers above, but none of them worked for me. I have wrapped my relative layout in which my button (which has to be attached to bottom) in another linear layout. It worked for me magically.

If someone can add the reason for this, it would be more helpful.

1

In most cases and by default, the navigation bar should always be below the app unless you've triggered a setting.

If that's the behavior you want to make sure you're not running this line:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
1
  • In some mobile, This was causing the problem. Nov 30, 2021 at 5:04
0

Setting softInputMode of popupWindow to SOFT_INPUT_ADJUST_RESIZE resolves the issue.

0

Try this before setcontent view of your java, My problem got solved by this`

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.