EditText Height

Djembefola

Active Member
Licensed User
Longtime User
I have a table grid and want to display an edittext in a table cell, when the user touches a cell.

My problem is: the height of the Edittext must be more than twice as large as the measured string height of it's text, to display the string properly.

I tried to set the EditText height to the height of my table rows. This should work fine, because i use the same typeface and the row height was calculated before with MeasureStringHeight.

But when i do that, only the lower half of the text is visible in the edittext view. I have already tried all gravity settings, but without success.

How can i reduce the vertical border size of the edittext view?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use the Reflection library to set the padding to a minimum value.
Try the following code:
B4X:
Sub Globals
   Dim txt1 As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
   txt1.Initialize("txt1")
   Dim c As Canvas
   Dim b As Bitmap
   b.InitializeMutable(1, 10)
   c.Initialize2(b)
   txt1.Text = "Hello"
   
   Dim size As Float
   size = c.MeasureStringHeight(txt1.Text, txt1.Typeface, txt1.TextSize)
   txt1.Gravity = Bit.Or(Gravity.TOP, Gravity.LEFT) 'Set gravity to top left
   Activity.AddView(txt1, 10dip, 10dip, 100dip, size + 6dip)

   txt1.Color = Colors.White 'Removes the view edges
   Dim r As Reflector
   r.Target = txt1
   'Set padding to minimum
   r.RunMethod4("setPadding", Array As Object(0, 1dip, 0, 0), _
      Array As String("java.lang.int", "java.lang.int", "java.lang.int", "java.lang.int"))
End Sub
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
This problem occurs when an edittext widget is:
1) Created from the designer AND is smaller than 30 pix in height
2) or 2 created manually at any height (the padding is HUGE)

I don't know why these 2 cases are different, but they are.
It would be nice to have padding set to a minimum to begin with (as default). This way new users won't get frustrated wondering why they can't see the text.

By default if you create a EditText manually, the padding is so big, you would have to make the EditText huge before you will see any difference in the vertical alignment of the text.

Here is a picture of what a 30 pix height EditText looks like when selected. Each edittext has the word "Test" in it, but you can't see it. Also, the orange selection highlight looks strange. A permanent fix would be great! Thanks

ps. look at the buttons, the padding here looks too large as well.
 

Attachments

  • testbox1.png
    testbox1.png
    5.2 KB · Views: 708
Last edited:
Upvote 0

iancasey

Member
Licensed User
Longtime User
I thought I had the full version but no Reflection library, is there a link to it?

Thanks
Ian:sign0104:
 
Upvote 0

junglejet

Active Member
Licensed User
Longtime User
I know this is an old thread, but I cannot find another answer for what's seems to be an ongoing problem.

I have an app that uses about 50 edit text fields. They should be preety big as it is the standard fashion for mobile devices now.

With the huge standard top and bottom margin (or border height or padding) is it really true that I need to apply the sub posted by Erel for every edittext to get rid of thE margins? Seems to me to be a quite complicted way.

Has there been a permanent fix to this problem somewhere else in the meantime?

Is there a library or module with edit text alikes that have more parameters exposed that can be managed in an easier manner?

Thank you
Andy
 
Last edited:
Upvote 0
Top