0

I use custom font:

myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");

Depending on screen size I calculate font size as mySize variable. I use

editText.setTypeface(myTypeface);
editText.setTextSize(mySize);

Size is correct on 1024*600, 640*480 devices, but text height is big on 800*480 device. So calculated mySize does not work. It shows bigger text. I suspect that I can use only certain sizes such as 10,14,20,25,etc And calculated mySize is not supported sometimes. But I don't know which sizes are available for my font. So what should I do to show my page on different devices correctly. Thanks!

Niaz
  • 528
  • 2
  • 10
  • 21

1 Answers1

0

Is mySize calculated dynamically based on screen size or is it a constant?

By default setTextSize sets the size in px so will appear different on different screen densities.

To use density-independent scalable pixels (sp) you should use editText.setTextSize(TypedValue.COMPLEX_UNIT_SP ,mySize);

reactivemobile
  • 485
  • 3
  • 9
  • mySize is px and depends on screen size. I tryed to use the same proportions on any screen. Okay, thanks, I will try your approach – Niaz Oct 21 '14 at 14:48
  • You approach looks okay on emulator and 1024*720, but text is too big again on 800*480 device. Strange, that emulator settings is also 800*480 and the performance is okay there. – Niaz Oct 21 '14 at 15:05
  • You might need to create resource folders for the different screen sizes you support and a dimens.xml with a hardcoded text size to each. You can use that value in your code for setting a bigger text size for tablets etc. See http://stackoverflow.com/questions/9877946/text-size-and-different-android-screen-sizes – reactivemobile Oct 21 '14 at 15:20
  • Hmm. Thanks. But I could not understand the idea and how to use it programmatically. I tryed to use mySize=20 everywhere as SP as you said. But the text is big on small resolutions – Niaz Oct 21 '14 at 16:04
  • My emulator and device have the same 800*480 (my app recognized correctly), but on device the height of text is much bigger. – Niaz Oct 21 '14 at 16:28