Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Android 11 two methods are deprecated #276

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

akaSourav
Copy link

'getter for defaultDisplay: Display!' is deprecated.
and
'getMetrics(DisplayMetrics!): Unit' is deprecated.

'getter for defaultDisplay: Display!' is deprecated.
and
'getMetrics(DisplayMetrics!): Unit' is deprecated.
@stowy
Copy link
Contributor

stowy commented Dec 8, 2020

Hi @Sourav-21 thanks for contributing. I think the correct fix is to use getContext().getResources().getConfiguration().densityDpi see docs.

We should also change this for the other samples.

If you would like to make and verify those changes I can approve and merge.

@akaSourav
Copy link
Author

Hi @stowy
getContext().getResources().getConfiguration().densityDpi method will not work in this case. getContext().getResources().getConfiguration().densityDpi will return corresponding to density resource qualifier like ldpi, mdpi etc. Please look at this stackoverflow answer.

val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val outMetrics = resources.displayMetrics

val density = outMetrics.density
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline this to resources.displayMetrics.density

val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val outMetrics = resources.displayMetrics
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line can be removed, no need for outMetrics to be a variable.

@stowy
Copy link
Contributor

stowy commented Dec 8, 2020

Thanks @Sourav-21 point taken. Added some comments but we still need to update for other samples.

@akaSourav
Copy link
Author

@stowy, resources.displayMetrics method isn't working on Android R too. I found this bug in AOSP sources.

I think proper implementation will be something like this-

DisplayMetrics outMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        Display display = getContext().getDisplay();
        display.getRealMetrics(outMetrics);
} else {
        Display display = getWindowManager().getDefaultDisplay();
        display.getMetrics(outMetrics);
}
return outMetrics.density;

@akaSourav
Copy link
Author

@stowy , I also found in AOSP DisplayInfo source that density=densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE

So an alternative better solution is getContext().getResources().getConfiguration().densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE

@stowy
Copy link
Contributor

stowy commented Dec 9, 2020 via email

@akaSourav
Copy link
Author

I was wrong. To convert pixels into dp, we should divide pixels by a scaling factor and that scaling factor is dpi / 160.

getContext().getResources().getConfiguration().densityDpi method returns the screen dpi and after dividing dpi by 160 we get the scaling factor which is exactly same as outMetrics.density

And configuration().screenWidthDp method returns the screen width directly in dp.

I have tested these methods and work perfectly.

@tillchen
Copy link

tillchen commented Dec 5, 2021

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants