Android Question Tri-State ToggleButton

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Anyone create a Tri-State ToggleButton?

I have a button that is Checked or Not-Checked

Not Checked means that it does not apply
Checked means then user did it and all is OK

I would like to add a 3 option that the user did it and it Failed

So I would like to show a no check, Check, and Red X as see below


checkbox_unchecked_box30x30.png
checkbox_checked_box30x30.png
checkbox_checked_box30x30-x.png


I know I can do this with two buttons but would like to save screen space and do it with one
 

adrianstanescu85

Active Member
Licensed User
Longtime User
You can do it with an ImageView object which would load 3 (or more, if you wish) images each time for its Click event. You could also implement other actions if you wish under the LongClick event (as an idea). Anyway, this really works if you'll plan to use it.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Right now I am saving the state inside the Tag and changing the Statelist to be Unchecked / Checked or Unchecked / Red X

Now this make me change the check state.

User clicks check, Check mark is selected. User clicks check mark (truly we are suppose to show the unchecked box) but I check the tag state (which shows checked and make it redx) then if we should display a red x, I change the statelist to the redx statelist and check the box.

Next time user clicks I check tag again (should be marked as redx, I change to unselected and change (change the statelist back to the normal one))

Was hoping there was an actual control that handled tri-state, but what I am doing appears to work
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
You're right, it's always the best way to find the answer that fits your needs, but in this case I thought of recommending a solution that would do the job and loop through the 3 states. Your way of implementing it matters since it solves the problem, so if anyone else has an actual solution with a 3-states control I'd be also quite interested, let's hope we'll get to that too.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
What you mean by failed?
In electronics the third state is high impedance (open circuit) so it's neither connected to vcc nor to gnd.
Please clarify a little more.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
What you mean by failed?
In electronics the third state is high impedance (open circuit) so it's neither connected to vcc nor to gnd.
Please clarify a little more.

OK, let me be more specific.

This is for my Golf Scoring APP

The Button is called Sand Save -
Unchecked means you were not in the Sand [no sand save shot or attempt]
Checked (check mark) Means you were in a Green Side Bunker and chipped out (and got into the cup in 2 or less strokes - counting the bunker shot) [Sand Save]
Checked (red X) Means you were in a Green Side Bunker and chipped out but did not get in the cup in 2 or less strokes - [Sand Save Attempt]

Doing it this way to save screen space with multiple buttons. So with one button I can represent if you attempted or had a sand save.

BobVal

Beside have you never heard of : Yes, No, Maybe :) or Give Me, Buy Me, I want
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Truly what I was looking for was a way to make a Statelist handle 3 clicks (Enabled (Unselected), Selected, Deselected) so that a ToggleButton would automatically handle the 3 pushes

Is this better?
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
I would like to add a 3 option that the user did it and it Failed

I see this is more about notification than a button. means the state is a result of failure of another state (the checked).
If this is true then you will need to track and trap the checked button actions and build on their feedback the third state. (I think).
(check and listen)
 
Upvote 0

CidTek

Active Member
Licensed User
Longtime User
I see this is more about notification than a button. means the state is a result of failure of another state (the checked).
If this is true then you will need to track and trap the checked button actions and build on their feedback the third state. (I think).
(check and listen)

I still see it as a button but I would describe it as having N/A, Yes, No states.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Yes me too, I see it as a button.. say for example a button has 3 possible colors like a single multi-color traffic light. .
1- push it first to set the color to green (go)
2- push it again to set the color to red (stop)
3- if #1 failed the color is yellow
4- if #2 failed the color is yellow (but better you set it to orange to distinguish between the two failures)!! so
you can now if the last attempt was to set it on or off.

All this, to me, depends on a feedback from the outside.. I don't know your system but this is the logic my
crazy mind came up with.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I'd like to offer my own solution to the problem and at the same time share with you an analogy I use to describe coding/programming.

I often say to colleagues and friends that programming is like handwriting... you can give several people a paragraph to copy, some people will choose to write in capitals, others may write joined-up (cursive), or some may chose to write it in their native language. Some will be neat and others barely legible, and some will undoubtedly contain spelling errors. But at the end of the day they will all say roughly the same thing.
That's why programming is like handwriting, we all have our own style and way of doing things. Yes I agree that it is good to try and stick to certain standards and conventions to make it easier for others to follow your code, but at the end of the day, if it works and you're not expecting someone else to have to continue the project then its up to you how its done. And hopefully you'll have fun doing it ;)

Anyway, I'm surprised that this thread has not had more responses as there must be loads of ways of doing this and I don't know which is the best. However I would like to offer my own solution and challenge others to come up with a simpler/better method. Please see the attached example. It basically just uses the tag of a button to hold the state of the switch. Better still, any number of states can be added with no extra lines of code, just add an image file and name it the next value. I've also included a button to read the states of the three multi-state buttons in my example.
B4X:
Sub btn_Click
    Dim btn As Button = Sender
    ' Increment to next state and limit to number of states
    btn.tag = btn.Tag + 1
    If btn.Tag > 4 Then btn.Tag = 0
    btnUpdate(btn, btn.Tag)
End Sub

Sub btn_LongClick
    Dim btn As Button = Sender
    ' Reset to initial state on long click
    btn.Tag = 0
    btnUpdate(btn, btn.Tag)
End Sub

Sub btnUpdate(btn As Button, btnState As Int)
    ' 0=Every direction, 1=Up, 2=Right, 3=Down, 4=Left
    btn.SetBackgroundImage(LoadBitmapSample(File.DirAssets, btnState & ".png", 50, 50))
End Sub

Regards,
RandomCoder
 

Attachments

  • MultiState Example.zip
    13.5 KB · Views: 206
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi randomcoder. The state of falure, as I understood depends on external events.. you can't control it. So how are you going to hardcode it? You can only set the switch, gge button, on or off, then if the target device failed to do what is supposed to be done, then it will send notification to the App and the App will chanfe the button color accordingly.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
...You can only set the switch, gge button, on or off, then if the target device failed to do what is supposed to be done, then it will send notification to the App and the App will chanfe the button color accordingly.
All that is needed is to call the btnUpdate routine and pass it the button you wish to change and the state you want. eg...
B4X:
BtnUpdate(btn1, 3)
'Or 
CallSub3(Main, "btnUpdate", Main.btn1, 3) 'If called outside of the activity
To make it more readable I would advise to use variables for the states. But my understanding of the request for a tri-state switch was that Robert wanted to use a button in his app for the user to select?

Ps. Code lines are untested as sent from my phone.
Regards,
RandomCoder
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
But my understanding of the request for a tri-state switch was that Robert wanted to use a button in his app for the user to select?
If I correctly understood what you said, I can't imagine how a user can select a failure state of a button before knowing whether the job failed or went through,
which is totally out of their hand.
The failure (or success) information comes from the target device, not from the device where the App is installed. and this means Robert needs to write another
App for the target device.. that App will process the on/off command and if it couldn't carry it out then will send back the failure information, the current App then
use that failure information to color the button with the third state.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
It is easier to think of this as a button acting like a checkbox, but a checkbox that has several states.
State 1 - initial state, not clicked. (no mark)
State 2 - clicked once (tick mark).
State 3 - clicked again (x mark).

With each click the next state is selected. Clicking a third time will in this example reset back to being unchecked (no mark).

Regards,
RandomCoder
 
Upvote 0
Top