Android Question EditText BackGround Color not at full whidt problem

sdesan

Member
Licensed User
I use this code to set background color for an EditText control
Sub Globals
Dim cdb,cdb1,cdb2 As ColorDrawable
Dim EditText1 As EditText
Dim EditText2 As EditText
Dim EditText3 As EditText
Dim EditText4 As EditText
Dim EditText5 As EditText
Dim EditText6 As EditText
Dim EditText7 As EditText
Dim EditText8 As EditText
Dim EditText9 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
cdb.initialize(Colors.LightGray,5)
cdb1.Initialize(Colors.Green,5)
cdb2.Initialize(Colors.Yellow, 5)
EditText1.Width=20%x
EditText2.Width=20%x
EditText3.Width=20%x
EditText4.Width=20%x
EditText5.Width=20%x
EditText6.Width=20%x
EditText7.Width=20%x
EditText8.Width=20%x
EditText9.Width=20%x
EditText1.Background = cdb
EditText2.Background = cdb
EditText3.Background = cdb
EditText4.Background = cdb1
EditText5.Background = cdb1
EditText6.Background = cdb1
EditText7.Background = cdb2
EditText8.Background = cdb2
EditText9.Background = cdb2
...

End Sub

but the results is this (view attached file)


the colored area of background of 2d and 3d row is smaller (and wrong) than the other in 1st row.
Any suggestion?
 

Attachments

Star-Dust

Expert
Licensed User
Try this

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim EditText1 As EditText
    Dim EditText2 As EditText
    Dim EditText3 As EditText
    Dim EditText4 As EditText
    Dim EditText5 As EditText
    Dim EditText6 As EditText
    Dim EditText7 As EditText
    Dim EditText8 As EditText
    Dim EditText9 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")

    EditText1.Width=20%x
    EditText2.Width=20%x
    EditText3.Width=20%x
    EditText4.Width=20%x
    EditText5.Width=20%x
    EditText6.Width=20%x
    EditText7.Width=20%x
    EditText8.Width=20%x
    EditText9.Width=20%x
    EditText1.Background = BackGround(Colors.LightGray)
    EditText2.Background = BackGround(Colors.LightGray)
    EditText3.Background = BackGround(Colors.LightGray)
    EditText4.Background = BackGround(Colors.Green)
    EditText5.Background = BackGround(Colors.Green)
    EditText6.Background = BackGround(Colors.Green)
    EditText7.Background = BackGround(Colors.Yellow)
    EditText8.Background = BackGround(Colors.Yellow)
    EditText9.Background = BackGround(Colors.Yellow)
  
    BackGround
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

End Sub

Sub BackGround(Color As Int) As ColorDrawable
    Dim cdb As ColorDrawable

    cdb.Initialize(Color, 5dip)
    Return cdb
End Sub
1.png
See the result attached
 

Star-Dust

Expert
Licensed User
It is better not to use the same Background variable for more than one object.

This creates one for each
 
Top