Widget for text input...

Humor me and change it from   native.font(native.systemFontBold)  to just native.systemFontBold

native.systemFont and native.systemFontBold are userdata objects.  display.newText() is smart enough to look at the font passed in and determine if it’s a string or one of the userdata based native.whatever font.  native.newFont() is supposed to also not care if you pass it a string or a userdata object, but native.font spits out a userdata object so you should be able to bypass calling that and go straight to the native.systemFont or native.systemFontBold.

I’ve not tried it (and I don’t have a device to demonstrate it anyway), so give that a try.

Rob

Sure. It wasn’t native.font(native.systemFontBold) anyways. Line previously read 

opt.font = customOptions.font or native.systemFont

I changed it to the following just to make sure. 

opt.font = native.systemFontBold

Still the same result. Thanks for the idea though. Any other thoughts?

Thanks Ksan, I will clean up the code and send it your way

Sure thing. Also feel free to send me your apk if you don’t wish to share proprietary code at this point. Just build for Android and send it to me. I can load it via USB and test. Will send you a PM with my email. Thanks

I wasn’t thinking about the options defaults Kerem, but later in the code after it creates the native.newTextField, there is a line that looks something like:

    field.textField.font = native.newFont( opt.font )

Maybe try:

    if type(opt.font) == “string” then

         field.textField.font = native.newFont( opt.font )

    else

         field.textField.font = opt.font

    end

or something like that. 

Rob, thanks for that. I found & changed

field.textField.font = native.newFont( opt.font )

to

field.textField.font = native.systemFontBold 

just to be direct. When I build and run this I get the following error after clicking on my inputField

widget_newTextField.lua:100: bad argument #3 to ‘?’ (native.Font expected, got userdata)

message

This connects back to what you wrote earlier. Should I find the exact name of a font that I know exists on my Android device and try passing the actual font name and see what happens?

Hi Ksan,

Sent you a sample project with the sources - its a real early beta that I am working on, so be patient :). It will probably not work on your Android as it is using Rob’s scaling method

Features -

– display.newEditField creates edit fields that can be preserved when a graphic object is displayed on top (fixes the issue of newTextFields being always on top).

– allows a text label to be attached to the edit field

– allows small icons to be attached to the field - left or right aligned

– allows buttons with events to be attached to the field - left or right aligned

– supports all newTextField features, like isSecure, inputType, returnKey, setting the font etc.

– supports custom theming with a 3-slice image (if you do not have a custom theme installed will revert to the theming of newSearchField)

I will post a little later some screenshots of my screens implementation with this newEditField to give everyone heads up on what is possible. If there is interest I might open a closed beta to iron out the issues.

Cheers,

  Atanas

Yes, try a font you know to exist.  The font thing I suggested was worth a shot.  

Here’ something that might help.  In my apps that are multi-platform, I usually use my device.lua to determine if I’m a Kindle Fire, Nook, Android or iOS device.   Then I do something like:

mydata.serif = "Georgia" mydata.sans = "Helvetica" mydata.sansbold = "Helvetica-Bold" mydata.isAndroid = device.isAndroid mydata.isKindleFire = device.isKindleFire mydata.isNook = device.isNook mydata.kindlePadding = 0 if mydata.isAndroid then         mydata.serif = "DroidSerif"     mydata.sans = "DroidSans"     mydata.sansbold = "DroidSans-Bold"     if mydata.isNook then         mydata.serif = "Georgia"         mydata.sans = "AscenderSans"         mydata.sansbold = "AscenderSans"       end     if mydata.isKindleFire then         mydata.serif = "georgia"         mydata.sans = "arial"         mydata.sansbold = "arial-Bold"     end end

Then I just use the mydata.sans where I want a normal font, etc.  However I just was verifying the font names and it seems that with Ice Cream Sandwich (I really wish Google would use numbers… so that would 4.4 - KitKat, 4.3 Jelly Bean, 4.2 Ice Cream Sandwich), since 4.2 they are using a font called Roboto.  (See: http://developer.android.com/design/style/typography.html)

I suspect they added these to the Droid family but they do encourage using Roboto now, so you might be interested in putting in an OS test to see if you’re on 4.2 or later and use Roboto instead of Droid.

Rob

Hi Rob, 

Thanks for your input. I tried “DroidSans” and that did not change anything either. 

I then tried running a piece of code I had written few months ago to run on Android devices to collect their screen specifications and loaded fonts list etc. This code used to run nicely on this particular Android device. Now when I run it the font list comes back empty. The number of fonts installed is still shown correctly as 12 but somehow the font names are not showing at all. They still show on IOS devices and my Mac.

The code is using native.getFontNames() to get the font details so maybe there is something wrong with the newer builds that is breaking something like this on an older Android 2.2 device. Not sure how best to troubleshoot this. 

Just to rule out recent changes I went back to an older build, 2013.1232 to be specific and the font names not showing issue is still there. Can’t recall the build I used this code with last but I am sure it was working at some point. Oh well. Need to look it over and see what is wrong with it now.

Edit : Just printing out the fonts on device give me a nice list once again. The following is the list I get :

DroidSansFallback
DroidSansThai
DroidSansHebrew
DroidSerif-Regular
DroidSansArabic
DroidSans-Bold
DroidSerif-Bold
Clockopia
DroidSerif-Italic
DroidSans
DroidSerif-BoldItalic
DroidSansMono

So nothing wrong with the device or the core Corona SDK functions relating to accessing the fonts. The scaling is somehow not working.

Try one more thing for me:

Change:

field.textField.font = native.newFont( opt.font  )

to:

field.textField.font = native.newFont( opt.font, opt.fontSize * deviceScale )

native.newFont can take a size passed to it.  So it’s worth a shot.   Also I just went back and looked at your screen shots and it looks like the font is the right size, it’s just showing a few pixels from the bottom of it.  Are you seeing a tiny font or a cut off font?

Thanks

Rob

Hi Rob, 

Tried this code variation. Same result. 

You are right. The font size is probably not too far off. It just seems to display the lower 1/5th of the text or so. It appears that the whole text is offset vertically with the top of text being cut off at the natural border of the native.newTextField.

Hope this helps.

Rob, just to try something else… I built and loaded the Corona Labs sample called NativeDisplayObjects and loaded it onto this same device. The text field displays beautifully. Font, placement and all. Need to see what is different there. 

Interesting.   In that sample app is this code:

local isAndroid = “Android” == system.getInfo(“platformName”)

            local tHeight = 30
            if isAndroid then tHeight = 40 end        – adjust for Android
            textField = native.newTextField( 15, 80, 280, tHeight )

So they are adding 10 pixels to the height of the box.  Now I’m not sure if they are adding 25% or 10px. It could be either.  Perhaps you could change this line:

    field.textField = native.newTextField(0, 0, opt.width - opt.cornerRadius, opt.height - opt.strokeWidth * 2)

to:

    local tHeight = opt.height - opt.strokeWidth * 2

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

         tHeight = tHeight * 1.25

    end

    field.textField = native.newTextField(0, 0, opt.width - opt.cornerRadius, tHeight )

This is a 25% increase.  I’m going to guess that it’s 25% and not 10px.

Rob

Ok. This is definitely helping. Now I get about %60 of the text visible. See attached screenshot.

Instead of a percent, make it 10 and see if that does the trick. 

Do you mean something like this?

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

tHeight = tHeight + 10

end

Yes!!! tHeight = tHeight + 10 did the trick. I can now see all of the text entered.

So the question is why this issue did not affect the newer Android that you tested on and affected this particular one and how can the inputText tutorial be improved to cater for all. 

Thanks for your continued efforts to give us something we can rely on.

I seem to remember something from a couple of years ago that had to do with the fact that Android’s “chrome” around native widgets was different and needed compensated for.  I guess as more people move to a 4.x version, this is less important.    I’ll go update the blog post to add this adjustment for people grabbing it in the future.

Rob

So can we live with one version that runs on all Android devices and does tHeight = tHeight + 10  or do we need to determine if Android version is < 4.x and then do tHeight = tHeight + 10? If Android 4.x and later will not have the same problem the code needs to be selective right?