0

I have some issue with bottom margin on different devices. How to get same bottom margin for devices with/without on-screen buttons?

Implementation of PopupWindow:

DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        View viewGroup= activity.getLayoutInflater().inflate(R.layout.my_dialog, null, false);

        popupWindow = new PopupWindow(viewGroup, width, height);

Nexus 6

enter image description here

Samsung 5

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AA000000"
    >


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp"
        android:background="#fff"
        android:layout_weight="1">

        ...


    </LinearLayout>

</LinearLayout>
phnmnn
  • 12,813
  • 11
  • 47
  • 64

2 Answers2

0

Remove match_parent from layout_height And Replace it with wrap_content Like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#AA000000">


<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:orientation="vertical"
    android:background="#fff"
    android:layout_weight="1">

    ...


</LinearLayout>

Malik Abu Qaoud
  • 208
  • 1
  • 9
0

Here is a solution:

if (hasNavBar(activity.getResources())){
    LinearLayout linearLayout = (LinearLayout) popupWindow.getContentView().findViewById(R.id.linear_layout_dialog);
    if( linearLayout.getLayoutParams() instanceof ViewGroup.MarginLayoutParams)
    {
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) linearLayout.getLayoutParams();

        lp.bottomMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            (float) 43, activity.getResources().getDisplayMetrics());

        linearLayout.setLayoutParams(lp);

    }

}

public static boolean hasNavBar (Resources resources)
    {
        int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
        return id > 0 && resources.getBoolean(id);
    }
phnmnn
  • 12,813
  • 11
  • 47
  • 64