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.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
nitesh goel
  • 6,338
  • 2
  • 29
  • 38

9 Answers9

46

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

AZ_
  • 21,688
  • 25
  • 143
  • 191
nitesh goel
  • 6,338
  • 2
  • 29
  • 38
  • 10
    I added the following to my style true true 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 '15 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 '17 at 12:18
7

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

android:fitsSystemWindows="true"
logi-kal
  • 7,107
  • 6
  • 31
  • 43
Matin Ashtiani
  • 758
  • 9
  • 11
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

Muhammad Farhan Habib
  • 1,859
  • 20
  • 23
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));
Digvesh
  • 51
  • 4
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.

Morteza Rastgoo
  • 6,772
  • 7
  • 40
  • 61
  • 1
    For pre-lambda: `.setOnApplyWindowInsetsListener (findViewById(android.R.id.content), new OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { // .. } } );` – Gene Bo Jan 22 '18 at 03: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.

GiridharaSPK
  • 496
  • 7
  • 17
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);
6rchid
  • 1,074
  • 14
  • 19
0

Setting softInputMode of popupWindow to SOFT_INPUT_ADJUST_RESIZE resolves the issue.

Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
Shilpi
  • 498
  • 1
  • 6
  • 13
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);`
Amit Ghosh
  • 63
  • 6