My input text is cut off halfway on some Android devices!! PLS HELP!

Thanks for the response Rob.
Btw, I am using your “Ultimate Config.lua” code. And I am building it on Nexus 7, so I can tweak it into different resolutions.
Here is a part of my code: 

yearIF = native.newTextField( (display.contentWidth / 2), 180, (display.contentWidth\*0.45), 35) yearIF.font = native.newFont( native.systemFont, \_G.fontSize ) yearIF.size = \_G.fontSize yearIF.text = "" yearIF:setTextColor( 81, 81, 81 ) yearIF.inputType = "number"

my _G.fontSize is like this: 
 

\_G.fontSize = 12 / display.contentScaleY

 

Is that working for you?

Too bad, it is still not working well for me. I tried to change the font size formula to 10 / display.contentScaleY , but still the text is cut off halfway… please help!
 

All of these “fixed” are workarounds for broken textfield components. This is a rather well-known problem I heard about before I even downloaded the sdk.

This is a problem with Android and if you search on Google you will find a 100 different hacks in native code to try and deal with it. I do not know if any of them deal with it correctly or how much research Corona devs have put into testing them to get a good Corona’sized solution. I do know handling it now in Corona is a ridiculous pain or at least my initial work into it was.

In the end I had to use a brute force method. By testing and retesting on every device size and dpi available to me I came up with a number to adjust the text size to fit the textbox’s height on whichever device it is currently loaded on. It’s a horrible hack and has not been tested in a released app yet.

What blows my mind is that the OS doesn’t handle this on it’s own somehow. If the text is too tall for the textbox, the textbox should resize to fit, regardless of what the programmer has set the size to be. Also if no font size is put at all it should auto-size the text to fit the padding of the textbox on it’s own. I can think of no circumstance where anyone would want to put text in a textbox that is cut off.

It worked for me:

local fontSize = 12 / display.contentScaleY print(fontSize) print(display.contentScaleY)     local yearIF = native.newTextField( (display.contentWidth / 2), 180, (display.contentWidth\*0.45), 35)     yearIF.font = native.newFont( native.systemFont, \_G.fontSize )     yearIF.size = fontSize     yearIF.text = ""     yearIF:setTextColor( 81, 81, 81 )     yearIF.inputType = "number"

It printed:

I/Corona  (28615): 25.457746386707
I/Corona  (28615): 0.47136929631233

and here is what I got:

android.png

This was on my Google Nexus 7.  The config.lua for this test was:

    application =     {         content =         {             width = 320,             height = 568,             scale = "letterBox",             --xAlign = "center",             --yAlign = "center",             imageSuffix =             {                 ["@2x"] = 1.5,                 ["@4x"] = 3.0,             },         },     }

For fun, I did the test with the config.lua in the Ultimate Config.lua and it produced the same thing.

@Anderoth yeah it is a big problem for us … 

@Rob hmm I am currently having a problem for those devices that have a resolution of 320x480 below ( especially 240x320 ) . In what resolution did you tested it on?

@09.SemperFidelis, what is your config.lua like?  What is your code where you are creating the native text field?  What device are you using?  What version of Corona SDK?

Hi Rob! I already posted my config.lua, textfield code, and test-device in this thread. Hmm my Corona version is 2013.1076. Is that the correct version code?
I found a little solution for my problem: 

in my main.lua I have this little code:

\_G.fontSize = 12 / display.contentScaleY if display.pixelWidth \<= 240 and display.pixelHeight \<= 320 then \_G.fieldSize = 40 elseif display.pixelWidth \<= 320 and display.pixelHeight \<= 480 then \_G.fieldSize = 37 elseif display.pixelWidth \<= 480 and display.pixelHeight \<= 800 then \_G.fieldSize = 34 else \_G.fieldSize = 30 end

and for my native text field(s):
 

 yearIF = native.newTextField( (display.contentWidth / 2), 190, (display.contentWidth\*0.45), \_G.fieldSize) yearIF.font = native.newFont( native.systemFont, \_G.fontSize ) yearIF.size = \_G.fontSize yearIF.text = "" yearIF:setTextColor( 81, 81, 81 ) yearIF.inputType = "number" yearIF:addEventListener( "userInput", fieldHandler )

– But I still have a little problem on the 240x320 device resolutions…

For Android devices why don’t you do something like the following:

[lua]

    local textBoxScale = 1

    local textBoxFontSize = 16

    local widthIn = system.getInfo( “androidDisplayWidthInInches” ) 

    local heightIn = system.getInfo( “androidDisplayHeightInInches” ) 

    --Make sure its not nil… e.g. the simulator will return nil.

    if widthIn ~= nil and heightIn ~= nil then 

        local diagIn = math.sqrt((widthIn*widthIn)+(heightIn*heightIn))  --Diagonal in inches

        textBoxScale = diagIn/3.5   --3.5 is the iphones diagonal in inches…

    else

        textBoxScale = 4/3.5  --Default to a 4 inch diagonal.

    end

    textBoxFontSize = textBoxFontSize*textBoxScale

[/lua]

Some Android devices don’t actually give you the correct width and height, but you can run extra checks against the  device name to do those manually. I haven’t actually tested the exact code above, but if your using letterBox and 480x320 in config then i don’t see why it wouldn’t work :slight_smile:

[Edit]

ps. I forgot to mention that changing the font size based off the contentScaleY variable will not work in a lot of cases if your using a plain config with scaling and a size of 480x320. For example without any changes to the font size it will fit on both the iPhone3GS and the iPhone4 perfectly despite the latter having twice the pixel resolution. If your using the contentScale to base your font size off and not the diagonal in inches, your iPhone4 text would be twice the size as a iPhone3GS. Therefore using the actual inches of the device is the way to go in my opinion. I’ve just started setting up a more advanced version of the above in my code so i’ll test it on multiple devices to see how it goes!

If you want, I’ve got few devices with different resolutions I could test that for you [both tablets and phones], just send me links to your test apks in private messages.

@TandG – thanks a lot bro! I’ll try your code!
@krystian6 – thanks! but I can test my apk into different resolutions with our Nexus 7 :slight_smile:

Are you changing the nexus 7 resolution some how?

I have the same problem on Galaxy, Xperia, Optimus LG. The text is cut off.

It works perfectly on my Nexus 7.

Any help?

my code

local fontSize = 12 / display.contentScaleY myField = native.newTextField( 215, 48, 141, 27) myField.hasBackground=false myField.font = native.newFont( "GROBOLD", \_G.fontSize ) myField.size = \_G.fontSize myField:setTextColor( 67, 25, 24 ) myField.align = "center"

my config.lua:

application = { content = { width = 320, height = 480, scale = "zoomStretch", imageSuffix = { ["@1-5"] = 1.5, ["@2x"] = 2, ["@3x"] = 3, ["@4x"] = 4, }, }, license = { google = { key = "Your key here" }, }, }

I have the exact same issue on Android handsets. Here is how I fixed the problem.

if (system.getInfo(“platformName”) == “Android”) then

  local userIdInput = native.newTextField(5, 5, 150, 40) 

  userIdInput.font = native.newFont(native.systemFont, 14)    

  userIdInput.size = 14

else

  – create a similar set of specs for iOS devices

end

The key is the specification for the “size” field, and make sure that it matches with the “font” field. You can play with different height specifications/combinations inside the newTextField() function and size specifications in the “font” & “size” fields to get the optimum effect you desired. This technique should work for both systemFont and other fonts. 

I think TandG basically got it, for android.

To be specific, “point size” is defined as equal to 0.376mm (almost universal standard). This is a real size - based on the device your app is running on. This means a *native* point size of about 67 should be one inch tall (giant, from an input field standpoint) on any device, if implemented properly.

Simply mapping the contentScale does not take into account the devices physical size, as TandG pointed out. As T said (may I call you that, T?), one device (4 inches tall) could be 320x480, and another (also 4 inches tall) could be 640x960 ) as is the case with the 3gs and 4S on the iOS side - which causes a factor of 2 to be needed there…)

On the android side, at least for some OS versions, there are the system info values he pointed out. (But I think there’s an error in his calc, see below code). It would be helpful to know what Droid OS levels will give back that info (and to get it on iOS somehow, even if it be through hard wiring it into the SDK for known iOS platforms, since they are so few).

One important change to TandG’s code is to remove his triangle code and just make it a straight up a font height scaler, which from my first tests seems to work well.

[lua]

    if( utils.isAndroid == true ) then      – try and get the real width / height and deal with it            
        print(" – -- Android font size calc…")
        local fontScale = 1
        local widthIn = system.getInfo( “androidDisplayWidthInInches” )
        local heightIn = system.getInfo( “androidDisplayHeightInInches” )
     
        --Make sure its not nil… e.g. the simulator will return nil.
        if widthIn ~= nil and heightIn ~= nil then

            – heightIn is height of actual droid app is running on

            fontScale =  heightIn / 3.5        – 3.5 is height of original iOS device, we are mapping to based on our original asset sizes / font sizes
            print(" – widthIn = “, widthIn)
            print(” – heightIn = ", heightIn)

            print(" fontScale == ", fontScale)
       else

           print(" – unable to retrieve screen width, height … defaulting to deprecated scale algorithm")

       end

        inputFontSize = targetFontSize*fontScale  – targetFontSize is something like 20, a typical iPhone type point size

        – inputFontSize is the native font size to use for Corona native text .creation / .size field
    end
[/lua]

Well, that’s my two cents anyways.

T is fine by me :slight_smile:

Luckily adding iOS devices into the above is nice and easy as theres only 4 different screen sizes. Android on the other hand still has some problems…

From doing some testing it seems that some Android phones don’t send back accurate width and height variables. Therefore we need to somehow get around that issue… One way would be to run specific device checks to override diagIn e.g:

[lua]

if device == “GT-I8160” then diagIn = 5.2

elseif device == “GT-I8190” or device == “GT-I8730” then diagIn = 4      --S3 MINI.[/lua]

The problem with that is theres probably a large amount of devices out there that will send back inaccurate information, and finding which devices they are will be hard. Maybe we can run checks with the expected dpi against the actual dpi to see if theres too big a difference? I’m not sure yet but i’ll look into it. 

When I was using cocos2d I ran into similar issues. Text fields would differ on Android and iOS.

What I did was:

  • I’ve created a text input field

  • Covered it with my own custom text input field

When user tapped on my custom text input field, the native one took over and displayed keyboard etc. When typing I took over the input and displayed it in my custom text field.

I know it sounds like a lot of hassle, but good luck creating if statement for over 2000 android devices [because the fact that a device sends correct values now, doesn’t mean it won’t stop with next os update/on a different rom].

The only downside is that text selection is a bitch and it would be easier to just drop support for it than to try to handle it.

I’m not entirely sure this is 100% possible with Corona, like I’ve said I did it with cocos and I could access any native functionality I wanted, but it seems like a workaround.

@Rob sir here is my screenshot:

Please help us find some workaround for this! thanks

T, it’s just the device screen height in inches that’s needed, not DPI.

The reason is that when we set a point size, say 20, we are asking for a font that is 7.52 mm tall – for the device we are targeting (what our contentScale is set for, and all our assets are sized for). If your content scale is 320x480 or 640x960, you are likely targeting a standard iphone, whos screen is 3.5 inches tall.

I’m curious as to how bad the height reporting is on android. Is it just lesser brands, or, as is hinted in your post, major brands like the Samsung S3 (hinted at in your post).

Is it like 5% of android phones report the wrong screen height, or more like 25%? Off brand, or mainstream phones? Any idea?

Also, I started putting together code to deal with this, starting with cleaning up my iOS side (the old methods use display.contentScaleX and have problems dealing with 3gs vs 4, etc)… At this point, my iOS side looks like this (stole a little detection code from a BeyondtheTech forum post).

[lua]

        print(" – -- iOS font size calc…")
        local fontScale = 1
        local heightIn = 3.5        – default to stnadard iPhone size        
        
        if( sysType == “iPhone2,1” ) then       – 3GS        
            heightIn = 3.5
            print(" – iPhone 3GS detected")        
        elseif( string.match(sysType, “iPhone3”) or string.match(sysType, “iPhone4”) ) then       – iPhone 4, 4S        
            heightIn = 3.5
            print(" – iPhone 4 detected")        
        elseif sysType == “iPad2,5” or sysType == “iPad2,6” or sysType == “iPad2,7” or sysType == “iPad2,8” then    – 2,8 is unknown, just a guess at next mini #
            heightIn = 6.25                    – mini
            print(" – iPad mini detected")        
        elseif( string.match(sysType, “iPad”) ) then       – standard iPad
            heightIn = 7.75                
            print(" – iPad detected")        
        else
            print(" – unrecognized iOS device, using default 3.5 inches tall") – iPods will fall to here, they are 3.5 anyways…
        end

        fontScale =  heightIn / 3.5 – 3.5 is actual iPhone height
        inputFontSize = targetFontSize*fontScale            – inputFontSize is point size to use in native call
 

[/lua]

Only got to test it on my iPad (retina) so far, but the calculated font size appears to be dead on. Also, note that the heightIn used is the straight up and down (NOT diagonal) height of the screen - as this is what the point size relates to (direct, vertical height, not diagonal).

Edit: oh snap, the actual height of 3gs, 4, 4s, etc is 3 inches… Seems to work with diag height of 3.5, but the numbers aren’t quite perfect… Hmmm.

OK, I think I understand the issue (3 inches vs. 3.5…). I think 3 is the correct value to calc the proper point size - however, there is an additional source of error I realized that needs to be considered.

When you use content scaling, the SDK will try and fill the device space as best it can with your app. This can leave empty space above or below (or to the sides, which aren’t a concern for the font calc).

What this means is that the app might be mapped onto just 3 inches of the device. Not a problem on the 3gs, 4, or 4s. But the iPhone 5 is taller.

To deal with it, corona chops off a little on the top and bottom, making the effective area for your app 3 inches tall. Using the straight up real height of the iphone 5 would throw the calc off (as the sdk has already chopped the effective height being mapped onto).

This same issue, the space the content scaling throws away above/below the app would also need to be factored in on the droid side as well.