Android Question Adding StatelistDrawable button in code

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I am using StatelistDrawable buttons, setup in the designer, but how would I add these buttons in code, not using the designer?

StatelistDrawable
ColorDrawable
Color: #FFFFFFFF
Corner radius: 6
Border color: #00FFFFFF
BorderWidth: 0

Pressed Drawable:
Color: Default
Corner radius: 0

RBS
 

DonManfred

Expert
Licensed User
Longtime User
Is the forumsearch defect?

 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Is the forumsearch defect?

No, it is working fine and just found the same.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
No, it is working fine and just found the same.

RBS
Trying to add the button in code and make it look exactly the same as the one created in the designer, but so far not succeeded yet.
Main difference is that that the text is not centered horizontally.

B4X:
    Dim b As Button
    b.Initialize("KeyboardButton")
    b.TextColor = Colors.Black
    b.TextSize = 18
    b.SingleLine = True
    b.Typeface = Typeface.DEFAULT
    b.Text = "h"
    b.Tag = "xxx"
    b.Gravity = Gravity.CENTER_HORIZONTAL
    b.Gravity = Gravity.CENTER_VERTICAL
    b.Padding = Array As Int(0,0,0,0)
    
    Dim UnPressed, Pressed As ColorDrawable
    UnPressed.Initialize2(0xFFFFFFFF, 6dip, 0, 0)
    Pressed.Initialize2(0x00FFFFFF, 6dip, 0, 0)
    'b.Background = UnPressed

    Dim sld As StateListDrawable
    sld.Initialize
    sld.AddState(sld.State_Enabled, UnPressed)
    sld.AddState(sld.State_Pressed, Pressed)
    b.Background = sld

    pnlKeys.AddView(b, 0, 72dip, 28dip, 36dip)

It seems I don't actually need the StatelistDrawable as I can achieve the same by doing just:

B4X:
b.Background = UnPressed

What am I doing wrong regarding centering the text?

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Trying to add the button in code and make it look exactly the same as the one created in the designer, but so far not succeeded yet.
Main difference is that that the text is not centered horizontally.

B4X:
    Dim b As Button
    b.Initialize("KeyboardButton")
    b.TextColor = Colors.Black
    b.TextSize = 18
    b.SingleLine = True
    b.Typeface = Typeface.DEFAULT
    b.Text = "h"
    b.Tag = "xxx"
    b.Gravity = Gravity.CENTER_HORIZONTAL
    b.Gravity = Gravity.CENTER_VERTICAL
    b.Padding = Array As Int(0,0,0,0)
   
    Dim UnPressed, Pressed As ColorDrawable
    UnPressed.Initialize2(0xFFFFFFFF, 6dip, 0, 0)
    Pressed.Initialize2(0x00FFFFFF, 6dip, 0, 0)
    'b.Background = UnPressed

    Dim sld As StateListDrawable
    sld.Initialize
    sld.AddState(sld.State_Enabled, UnPressed)
    sld.AddState(sld.State_Pressed, Pressed)
    b.Background = sld

    pnlKeys.AddView(b, 0, 72dip, 28dip, 36dip)

It seems I don't actually need the StatelistDrawable as I can achieve the same by doing just:

B4X:
b.Background = UnPressed

What am I doing wrong regarding centering the text?

RBS
Simply doing instead:

[/CODE]
b.Gravity = Gravity.CENTER
B4X:
Solved this problem.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Simply doing instead:

[/CODE]
b.Gravity = Gravity.CENTER
B4X:
Solved this problem.

RBS
One thing that I still got wrong is that setting the size of the buttons in code gives me different results compared to setting the button size in the designer.
For the button width in the designer I have 28 and I need to make this 32dip to get the same width.
No idea yet why this is.

RBS
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
For the button width in the designer I have 28 and I need to make this 32dip to get the same width.
No idea yet why this is.
It depends on the device's scale. To convert the 28 to dips, you can use either of these 2 lines which are equivalent forms:
B4X:
Log(GetDeviceLayoutValues.Scale * 28)
Log(DipToCurrent(28))
To get the scale of your device:
B4X:
Log(GetDeviceLayoutValues.Scale)
I am not sure if I am addressing your question
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
It depends on the device's scale. To convert the 28 to dips, you can use either of these 2 lines which are equivalent forms:
B4X:
Log(GetDeviceLayoutValues.Scale * 28)
Log(DipToCurrent(28))
To get the scale of your device:
B4X:
Log(GetDeviceLayoutValues.Scale)
I am not sure if I am addressing your question
It doesn't address the question, but I wasn't aware of these 2 functions and useful to know.
As I now add buttons via code the question has become kind of irrelevant as the button width is now calculated and not fixed anymore.
All this has to do with my custom keyboard.

RBS
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
It doesn't address the question, but I wasn't aware of these 2 functions and useful to know.
I was reluctant to answer your question at first because of your past questions that I found hard to decipher, but one should always be appreciative when someone attempts to help.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I was reluctant to answer your question at first because of your past questions that I found hard to decipher, but one should always be appreciative when someone attempts to help.
Yes, it is appreciated. Didn't know those 2 functions.

RBS
 
Upvote 0

Serge Bertet

Active Member
Licensed User
For the button width in the designer I have 28 and I need to make this 32dip to get the same width.
Maybe a matter of scale, I ran into that kind of problem solved using:
B4X:
Dim size As Float = buttonsSize / xui.Scale
 
Upvote 0
Top