Created
September 28, 2023 11:23
-
-
Save YohannesTz/8947871c453e5d81526b4e18d8678615 to your computer and use it in GitHub Desktop.
Android API 29+ Get Screen Size Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.Resources; | |
import android.os.Build; | |
import android.util.DisplayMetrics; | |
import android.util.Size; | |
import android.view.Display; | |
import android.view.WindowManager; | |
import android.view.WindowMetrics; | |
import androidx.annotation.RequiresApi; | |
public class ScreenSizeCompat { | |
private static Api api = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ? new ApiLevel30() : new Api(); | |
public static Size getScreenSize(Context context) { | |
return api.getScreenSize(context); | |
} | |
@SuppressWarnings("deprecation") | |
private static class Api { | |
public Size getScreenSize(Context context) { | |
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); | |
DisplayMetrics metrics = display != null ? new DisplayMetrics() : Resources.getSystem().getDisplayMetrics(); | |
if (display != null) { | |
display.getRealMetrics(metrics); | |
} | |
return new Size(metrics.widthPixels, metrics.heightPixels); | |
} | |
} | |
@RequiresApi(api = Build.VERSION_CODES.R) | |
private static class ApiLevel30 extends Api { | |
@Override | |
public Size getScreenSize(Context context) { | |
WindowMetrics metrics = context.getSystemService(WindowManager.class).getCurrentWindowMetrics(); | |
return new Size(metrics.getBounds().width(), metrics.getBounds().height()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.res.Resources; | |
import android.util.DisplayMetrics; | |
public class Utils { | |
public static int dpToPx(final float dp) { | |
return Math.round(dp * (Resources.getSystem().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment