Scaling given view and screen width behaviour to be same for different screen sizes

I have a requirement to make a particular button view.

As per the requirements, the max width of button text is specified as 48px, when the screen width size is given as 360px. But since the resolution can be different for different devices, to satisfy the requirement, I am using

screenWidth = context.resources.displayMetrics.WidthPixels 

and then using the ratio (screenWidth*48)/360 to set the maxWidth of the button text like so:

button.title.maxWidth = (screenWidth * 48) / 360 

My question is that how can I better satisfy the requirements, without using these ratios? Also is there a way to use dps instead of pixels in this situation?

Add Comment
1 Answer(s)

Use Autosizing TextViews. Read the developers guide for more information.

Answered on August 29, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.