Android Question fontsize multiple layout

Discussion in 'Android Questions' started by ivanomonti, May 26, 2015.

  1. ivanomonti

    ivanomonti Well-Known Member Licensed User

    tempo fa dovevo ricorrere al problema sul font ma che non ho mai preso in considerazione, oggi devo lanciare un prodotto e devo risolvere, non ho capito molto delle varie risoluzioni con i fontsize, chiedo aiuto per risolvere il problema

    time ago I had to resort to the problem on the font but I never considered, now I have to launch a product and I have to solve, I did not understand a lot of the different resolutions with fontsize, ask for help to solve the problem

    Public Sub fonsize(size As Float)

    Dim LayoutVal As LayoutValues = GetDeviceLayoutValues 'Preleva dimensione del dispositivo
    Dim Dens As Float = Density 'Preleva la densità del dispositivo
    Dim Scale As Int = LayoutVal.Scale

    If Scale = 1 Then ' for low-density
    size = (size * 2) / Dens
    Else If Scale = 2 Then ' for medium-density
    size = size / Dens
    Else If Scale = 3 Then ' for high-density
    size = (size * 1.5) / Dens
    Else If Scale = 4 Then ' for extra-high-density
    size = (size * 2) / Dens
    Else If Scale = 5 Then ' for extra-extra-high-density
    size = (size * 2.5) / Dens
    Else If Scale = 6 Then ' for extra-extra-extra-high-density (launcher icon only; see note above)
    size = (size * 3) / Dens
    End If

    Return size

    End Sub
  2. Erel

    Erel Administrator Staff Member Licensed User

    This code is wrong. Font size is already measured in DIPs. You shouldn't scale it as the result will be wrong.

    You just need to call AutoScaleAll in the designer script to slightly increase or decrease the font size.
  3. ivanomonti

    ivanomonti Well-Known Member Licensed User

    I do not use design but written the Institute code, AutoScaleAll, in this case as the use!!
  4. Cableguy

    Cableguy Expert Licensed User

  5. Erel

    Erel Administrator Staff Member Licensed User

    It doesn't matter whether you use the designer or not. Scaling the font size based on the device scale is a programming mistake.
  6. RandomCoder

    RandomCoder Well-Known Member Licensed User

    But how should it be done as it's not always possible to use the designer to create views. For instance I'm designing a class that could be used full screen but can just as likely be used on a panel occupying a quarter of the available screen space. Using the MeasureString approach as used here works but for some reason the MeasureStringHeight needs a slight adjustment and applying a fixed scaling value of 1.75 seems to be the only option other than to use the alternative method which is to use the StringUtils and Reflection libraries which then adds a larger overhead to the class which I am attempting to keep as minimal as possible.
  7. Erel

    Erel Administrator Staff Member Licensed User

    This is not really related to this thread but there is no good reason to avoid adding a small library such as StringUtils or Reflection. It will not add any overhead.

    What I'm trying to explain is that the font unit values, unlike pixel values, are measured in DIPs automatically. You can scale the font size in the same way that auto scale works based on the physical screen size. Scaling it based on the screen scale is a mistake. The layout will not work as you expect it to work.

    You can use Klaus Auto Scale module to scale views added at runtime:
    Cableguy and lemonisdead like this.
  8. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Thanks for the explanation and the link (I've not read this before as I mistakenly thought it related only to the designer).
    But for me @Erel the real question remains which is why does Canvas.MeasureStringWidth work to produce the correct size of text but Canvas.MeasureStringHeight does not seem to work?
  9. Erel

    Erel Administrator Staff Member Licensed User

    Canvas.MeasureStringHeight is based on a lower level API that doesn't behave in the same way that a Label or EditText will behave. StringUtils uses the same measurement process as the views.
    RandomCoder likes this.
  10. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Thanks for the clarification.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice