Button colour/shape

MFX

Member
Licensed User
Longtime User
I have a grid of buttons that start off in the designer as grey buttons with rounded edges and a small gap between them. However if I then change their colour (Button1.color=colors.green) for example they lose their nice rounded edges and the gap between them disappears so they look like they've merged into one big button. I can get round the latter problem by making them slightly smaller (75 instead of 80 pixels) but you do I get the nice rounded edges back?

Cheers.
Martin.
 

klaus

Expert
Licensed User
Longtime User
In the Designer you have the CornerRadius parameter.
In my Designer when changing the color of the button the round corners remain.
What version of B4A do you use, I remember that in older versions the corners became angles.

Best regards.
 
Upvote 0

MFX

Member
Licensed User
Longtime User
In the Designer you have the CornerRadius parameter.

I didn't notice that but I'll check again, however as I'm changing the colour in the program (I need to switch the colour between red and green, so it has to be done in the program) then surely I need to change the corner radius in the program and not in the designer?

What version of B4A do you use

The latest I guess, I only downloaded it yesterday :)

Martin.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you set in the code Button1.Color = Colors.Green you will get angle corners.

To maintain the rounded corners you must add a StateListDrawable for each color. You have two possibilities, a ColorDrawable or a GradientDrawable StateListDrawable .
And set these to Button1.Backround = StateListDrawableRed.
You need one StateListDrawable for red and another one for green.

Attached you find a small test program that shows both possibilities, ColorDrawable and GradientDrawable.

You can play with the colors, you could also change the colors for the pressed states to the colors of either the current colors or he future colors.
Currently the pressed state has a different color.

I agree it looks somewhat complicated, but once you know how it works it becomes easier.

Best regards.
 

Attachments

  • ButtonRadius.zip
    6.3 KB · Views: 501
Upvote 0

MFX

Member
Licensed User
Longtime User
Hm didn't manage to try it "tomorrow" but I have now and am getting close to what I want, but I seem to have an issue where as soon as the first button is clicked on all the others turn Orange (see attached example code). As you then click on the buttons they toggle red and green as expected but then sometimes as you click around the buttons other buttons will turn orange again.
 

Attachments

  • Button test.zip
    6 KB · Views: 299
Upvote 0

MFX

Member
Licensed User
Longtime User
Ah fixed it !

I changed

cdRedColorPressed.Initialize( Colors.RGB(255,182,18),CornerRadius)
to
cdRedColorPressed.Initialize( Colors.RGB(255,0,0),CornerRadius)

and

cdGreenColorPressed.Initialize( Colors.RGB(255,182,18),CornerRadius)
to
cdGreenColorPressed.Initialize( Colors.RGB(0,255,0),CornerRadius)

:sign0060:
 
Upvote 0

MFX

Member
Licensed User
Longtime User
Well I have another weird problem with it now but it's going to take a while for me to think of how to explain it :) But in simple terms most of the buttons on the screen are 75x75 pixels but one of them is 80x480 pixels and needs to be changed between red and green like the others. When I run it (and single step it) I can see it correctly being initialised to Green the full width of the screen but later it changes so that the green area of that button is only 75x75 even though the Button text stays in the same place and it still reacts if you press it the full width of the screen. A photo will probably illustrate it better than I can explain it!


Martin.
 
Upvote 0
Top