Ad

How To Fix Soft Buttons Height For Galaxy S10+?

enter image description here

I was having such a problem.

enter image description here

I solved this with the following code block.

   public static void fixSoftButtonsHeightProblem(Context context, View view) {
        int softButtonsHeight = Utils.getSoftButtonsBarHeight(context);
        view.setPadding(0, 0, 0, softButtonsHeight);
    }

    public static int getSoftButtonsBarHeight(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager windowManager = ((Activity) context).getWindowManager();
            windowManager.getDefaultDisplay().getMetrics(metrics);
            int usableHeight = metrics.heightPixels;
            windowManager.getDefaultDisplay().getRealMetrics(metrics);
            int realHeight = metrics.heightPixels;
            if (realHeight > usableHeight)
                return realHeight - usableHeight;
            else
                return 0;
        }
        return 0;
    }

enter image description here

But on some devices this has caused a different problem. For example, for Samsung Galaxy S10 +, such a problem occurs. How can I solve this?

Manifest.xml

 <activity
            android:name=".ui.activity.HomeActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan"/>

My Theme Style;

  <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>


    </style>

This way all over this app. This is the layout I use for HomeActivity.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    android:orientation="vertical"
    tools:context=".ui.activity.MainActivity">


    <com.xxx.app.view.NonSwipeableViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/tablayout"
        android:layout_below="@+id/searchActionBar"
        android:layout_marginTop="-12dp">


    </com.xxx.app.view.NonSwipeableViewPager>


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:tabRippleColor="@null"
        android:background="@drawable/navbar_bg" />

    <ImageView
        android:id="@+id/float_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:src="@drawable/ic_mid_pen"
        android:visibility="visible" />

    <include
        android:id="@+id/searchActionBar"
        layout="@layout/search_actionbar_layout" />


</RelativeLayout>
Ad

Answer

I solved problem. This code block is wrong result for usable screen size;

  WindowManager windowManager = ((Activity) context).getWindowManager();
            windowManager.getDefaultDisplay().getMetrics(metrics);
            int usableScreenHeight = metrics.heightPixels;

change it

public static void fixSoftButtonsHeightProblem(Context context, View view) {
        int softButtonsHeight = Utils.getSoftButtonsBarHeight(context);
        view.setPadding(0, 0, 0, softButtonsHeight);
    }

    public static int getSoftButtonsBarHeight(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager windowManager = ((Activity) context).getWindowManager();
            windowManager.getDefaultDisplay().getRealMetrics(metrics);
            int realHeight = metrics.heightPixels; //this is full screen size of phone
            View decorView = ((Activity) context).getWindow().getDecorView(); //if you use this in a fragment, use getActivity before getWindow()
            Rect r = new Rect();
            decorView.getWindowVisibleDisplayFrame(r);
            int usableScreenHeight = r.bottom; //this is real usable screen size of phone
            if (realHeight > usableScreenHeight)
                return realHeight - usableScreenHeight;
            else
                return 0;
        }
        return 0;
    }
Ad
source: stackoverflow.com
Ad