Android Question button added by code isn't activating pressed statelist

leitor79

Active Member
Licensed User
Longtime User
Hi,

I've added a button into a panel, and added some states to it. the state_enabled is working, but when I press the button, the pressed state is not showing. The button is pressed because the click event is fired.

the code goes like this:

B4X:
Dim btnFechaNacimiento As Button
btnFechaNacimiento.Initialize("btnFechaNacimiento")
btnFechaNacimiento.TextColor = Colors.Gray
btnFechaNacimiento.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL)
btnFechaNacimiento.Typeface = Typeface.DEFAULT

Dim d As ColorDrawable
Dim d1 As ColorDrawable
Dim d3 As ColorDrawable
Dim sd As StateListDrawable

d.Initialize(Colors.magenta, 4)
d1.Initialize(Colors.blue, 4)
d3.Initialize(Colors.red, 4)

sd.Initialize
sd.AddState(sd.State_Enabled, d1)
sd.AddState(sd.State_Pressed, d)
sd.AddCatchAllState(d3)
btnFechaNacimiento.Enabled=True

btnFechaNacimiento.Background = sd

P.AddView(btnFechaNacimiento, x, y, w, h)
lstPreferencias.Add(P, height,Null)

the button is added into a panel that is added later into lstpreferencias, a customlistview. I also add edittexts and spinners that works fine with their backgrounds (not statelists).

the button shows blue (statelist enabled) but never turns magenta.

I appreciate your help!

Regards!
 

leitor79

Active Member
Licensed User
Longtime User
thank you very much, Erel!

I've missed the dips, thanks.

I've very missed the state prevalence.

And I'm not using the designer for this because it's a customlistview with 3 panels (or more in the future), each one distinct from another, so each one has different heights. From code, I can calculate the height of each panel after "building it" and use that height to add the panel to the customlistview. Otherwise, from the designer, I can't get the panel height before adding it to the customlisview (right?). I should have that heights hardcoded somewhere and I don't like it. From code, at least, I can define some constants corresponding to items sizes (editboxes, spinner, imageview, etc) and the height is calculated.

If you have a better approach, it's always welcome!

Regards!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
And I'm not using the designer for this because it's a customlistview with 3 panels (or more in the future), each one distinct from another, so each one has different heights. From code, I can calculate the height of each panel after "building it" and use that height to add the panel to the customlistview. Otherwise, from the designer, I can't get the panel height before adding it to the customlisview (right?)

I don't know how your custom view should look, but I think you can add directly the 3 panels in it and use Anchors. without adding other views after, by code.
"Today" the Designer is so powerful that you can create a custom view with a single layout.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Today" the Designer is so powerful that you can create a custom view with a single layout

I am a little bit surprised to see that post comming from u @LucaMs :)
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi Luca, thanks for your answer!

Maybe I'm missing something. The designer is very nice and a very powerful tool. But I have, for example, this case I don't know how to solve with a layout made with the designer:

I'm loading items into a customlistview. Suppose one of them is an autosizelabel, which changes it size according to the font's size. So, the panel holding the label would be taller or smaller according to that.

When I add items into a customlistview I must specify the height of the item (the panel I'm adding). I can't get the panel height until I add into the customlistview (throws an exception). Maybe there is a way to achieve this (-1 height for auto-height?) or some other form of getting the panel's height before adding into my customlistview. I appreciate any advice, I'm still a noob here.

Thak you very much!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Are you creating:

a) a your own custom list view;
b) a custom view which should contain a CustomListView.

If a): why don't use instead CustomListView (or CheckList or UltimateListView)?

If b) to create an Item you should use the same method used by the example in the thread CustomListview.



mmm... it's late and I'm tired but... reading again this thread I think you are simply trying to create items to add to a CustomListView (lstpreferencias, which I would name clvPreferencias, because lst could be a List object ;)).

If it is so, you could create an invisible panel (or only the label), add it to the Activity, "get" (derive?) its height then remove it from the Activity.



[I have noticed :rolleyes: that we are off topic; you can open a new thread, if needed ;)]
 
Last edited:
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi Luca, thanks again for your answer and for your concern.

I'm not sure if it's needed a new topic for this and one or two more answers?

I'll try your suggestion about an invisible panel, thanks!

The case is I'm making a preferences screen, with sections, thing I could do (I did do, actually) with one layout in the designer but if it results to be too "tall" for a small screen (which it does for the first version in a small screen I've tested) I had no scroll to access the last options.

Putting section by section in a customlistview allows the user to scroll to se the sections that stays off the screen.

Thank you very much for your answer!


PS: is lstPreferencias to me because I'm and old visual basic developer! haha!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The case is I'm making a preferences screen, with sections
Please, don't do... what I did! Do not waste time. Probably there is already a similar "view" on site (I'm not sure) and certainly you can obtain what you need more easily using CheckList (moreover, it is a class, then you can modify it according to your needs).


PS: is lstPreferencias to me because I'm and old visual basic developer! haha!
but I'm older than you :):( and I know that "tomorrow", when you will read your code again, lstPreferencias will make you think of an object of type List :)
 
Upvote 0
Top