make a toggle button "greyed out"

Search This thread

smaskell

Senior Member
Jan 31, 2010
479
44
San Francisco
Hey guys,
Is there anyway to disable a toggle button and make it "greyed out"? I could just make it invisible but I think it would make more sense to the user if they could see that the button was not available rather than just having it disappear. I figured this would be pretty standard but I don't see it anywhere. Any help would be greatly appreciated.

Thanks,
Samuel Maskell
 
R

Rootstonian

Guest
What's the purpose of the button?

You could just leave it there and not process based on code in the Listener.
 

smaskell

Senior Member
Jan 31, 2010
479
44
San Francisco
Yeah, I was thinking of just making it display a toast message or something
but it would really be nice if I could change the appearance of the button
maybe I can play around with changing the background a bit..
 

smaskell

Senior Member
Jan 31, 2010
479
44
San Francisco
so I tried just using setBackgroundColor(Color.DKGRAY) but that failed miserably..
I can use getBackground() to get a Drawable object
so I should be able to take that object and make it darker
and then use setBackgroundDrawable, right?
I'm just not too sure how to go about making that drawable darker..
 

smaskell

Senior Member
Jan 31, 2010
479
44
San Francisco
Why not just setEnabled(false); ?

Didn't see your post last time. I am using setEnabled(false), but this does not change the appearance of the button. How are users supposed to know that the button is disabled? They'll just press it and think that the application froze or something because nothing will happen. If I could make the button darker and disable it, I think people would get the idea that it is disabled and not just broken.

Thanks,
Sam
 

freekyfrogy

Senior Member
Aug 10, 2009
369
21
33
Virginia
have you tried toggle.setColorFilter(0xAARRGGBB, Mode.SRC_ATOP); ?

you'd need to find the correct #AARRGGBB code for a greyish color, but it should work.

EDIT: Ok, so after playing around with the settings a bit I found that this works pretty nicely for greying something out toggle.setColorFilter(0xA6A6A6A6, Mode.SRC_ATOP);
 
Last edited:
  • Like
Reactions: smaskell

Gene Poole

Senior Member
Jul 15, 2010
1,700
567
Brighton IL
Didn't see your post last time. I am using setEnabled(false), but this does not change the appearance of the button. How are users supposed to know that the button is disabled? They'll just press it and think that the application froze or something because nothing will happen. If I could make the button darker and disable it, I think people would get the idea that it is disabled and not just broken.

Thanks,
Sam

Are you doing some custom painting or something weird? I've not tried this with a toggle button specifically, but other controls like check boxes and regular buttons show a grayed-out appearance when I've used this method.
 
  • Like
Reactions: smaskell

smaskell

Senior Member
Jan 31, 2010
479
44
San Francisco
alright, that was stupid..
for some reason I was using setClickable instead of setEnabled
I think I accidentally clicked in eclipse's autocomplete menu..
setEnabled(false) works perfectly
thank you so much
 
Last edited:

freekyfrogy

Senior Member
Aug 10, 2009
369
21
33
Virginia
Are you doing some custom painting or something weird? I've not tried this with a toggle button specifically, but other controls like check boxes and regular buttons show a grayed-out appearance when I've used this method.

Yeah you're right, I just tried it out in one of my apps and the whole button faded out and is see-through
 

Lakers16

Senior Member
Nov 19, 2010
82
10
The correct way to do what youre doing is to make an xml file for your buttons:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE selector>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/bar_gray_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/bar_gray_clicked" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/bar_gray" />
<item
android:state_enabled="true"
android:drawable="@drawable/bar_gray" />
</selector>


You can pretty much just copy and paste that into an xml file and put it in your drawable folder.

If you create "myCustomButton.xml", then you'll set your buttons background to be "@drawable/myCustomButton"

To make this work you need to also get the 3 drawables for your different button states:
Normal (Mine is called bar_gray
Clicked (Mine is called bar_gray_clicked)
Disabled (Mine is called bar_gray_disabled)

Android will handle the switching of the images as you click the button or when you do setEnabled(false)
 
Last edited:
R

Rootstonian

Guest
Down and dirty, like you said before:

Toast.makeText(this, "Button disabled", Toast.LENGTH_SHORT).show();
 

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    have you tried toggle.setColorFilter(0xAARRGGBB, Mode.SRC_ATOP); ?

    you'd need to find the correct #AARRGGBB code for a greyish color, but it should work.

    EDIT: Ok, so after playing around with the settings a bit I found that this works pretty nicely for greying something out toggle.setColorFilter(0xA6A6A6A6, Mode.SRC_ATOP);
    1
    Didn't see your post last time. I am using setEnabled(false), but this does not change the appearance of the button. How are users supposed to know that the button is disabled? They'll just press it and think that the application froze or something because nothing will happen. If I could make the button darker and disable it, I think people would get the idea that it is disabled and not just broken.

    Thanks,
    Sam

    Are you doing some custom painting or something weird? I've not tried this with a toggle button specifically, but other controls like check boxes and regular buttons show a grayed-out appearance when I've used this method.