10

I am new with Xamarin and struggling with my first application. I have an image with an entry view above it. This way I create a custom entry background.

The font size of the entry won't scale with different devices with different resolutions though. When I adjust the font size for device A and now check the same app on device B the font size there is way too big.

I as well tried to set the font size dynamically in the Android code as described here: http://developer.xamarin.com/recipes/android/resources/device_specific/detect_screen_size/

But this still doesn't give the result I want. Neither does a percent definition as in RelativeLayout.

I just want my font size to stay the same relative to the screen size so the entry is always within the background image borders.

I hope anybody can help me with this or give me a hint for a workaround.

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
telandor
  • 869
  • 9
  • 28

1 Answers1

3

I assume you are using Xamarin.Android rather than Xamarin Forms - you should not need to dyamically change the font size in code. Instead specify the fontSize in either a style or in the control itself and use 'sp' rather than px or dp (For example android:fontSize='16sp'.

Using 'sp' (scaled pixels) will scale appropriately depending on the user's device dpi (you can see a more indepth explaination here.

Community
  • 1
  • 1
codechinchilla
  • 2,129
  • 13
  • 23
  • No, I use Xamarin.Forms with custom renderers. Your tip using "sp" really helped. Thank you. The font is now about the same size on small and big screens. Do you as well know how to achieve that the font is smaller on small devices and bigger on big devices? – telandor Jun 01 '15 at 06:19
  • @telandor You're welcome! Glad that helped - if you want varying sizing depending on device size, you will have to set the text size dynamically based on device size (example of getting device size obtained here http://stackoverflow.com/questions/19155559/how-to-get-android-device-screen-size) . I'd still use sp in order to account for different pixel density screens, but just vary the number depending on the physical size you get back. – codechinchilla Jun 01 '15 at 16:36
  • 4
    How did 'sp' help you with Xamarin.Forms? Can you explain how you ended up getting this to do what you wanted? – B-Rad Apr 12 '16 at 21:23
  • I get Unhandled Exception: System.InvalidOperationException: Cannot convert "15sp" into System.Double when emulating to iOS – Welsh King Jul 04 '16 at 08:55