How can I keep a button as pressed after clicking on it? [duplicate]
Asked Answered
M

3

36

Possible Duplicate:
Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?

I have a button, and I want that when I press it, it stays as pressed (with the green color on Froyo).

Any help?

mycodes_Button = (Button) findViewById(R.id.mycodes);
...
if (saved_Button.isPressed())
{
    saved_Button.setFocusable(true);
}

Something like this?

Meldon answered 20/1, 2011 at 12:50 Comment(2)
Can you pls tell, what problem you had face by this code?Hillman
After i click the button, it doesn't stay as pressed (i mean with green color), it returns to the original state, gray one.Meldon
W
41

Use the following code. It's useful.

mycodes_Button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mycodes_Button.setPressed(true);
        return true;
    }
});
Wellheeled answered 20/1, 2011 at 13:18 Comment(3)
This basically works, but you should mention, that you lose the normal button functionality. Because you return true here, the onClick stuff is never executed. So actually you have to trigger the stuff, you want the button to do from within this onTouch method. Btw: You should use v.setPressed(true).Regret
no need to handle onClick stuff separately now. just use v.performClick(); in onTouchClincher
There's a little problem with this. I think that when the activity is paused the buttons get unpressed. It's a bummer for me because I display a progress dialog and also navigate to other activities and expect the buttons to keep pressed in these situations.Emeraldemerge
K
49

I had this issue with a button with a custom background, and ended up using the selected state for this. That state is available for all views.

To use this you have to define a custom button background as a state list:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="false" android:state_focused="false" 
     android:state_pressed="false"><bitmap ... /></item>
  <item android:state_selected="true"><bitmap ... /></item>
  <item android:state_focused="true"><bitmap ... /></item>
  <item android:state_pressed="true"><bitmap ... /></item>
</selector>

Then to use that background, let's say it is in /res/drawable/button_bg.xml in your layout file, you use:

...
<Button android:background="@drawable/button_bg" ... />
...

In your code you can switch to the (de-)selected state in your onClick listener:

myButton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    v.setSelected(true);
    // normal click action here
  }
});

The activated state matches the intended meaning better, but is only available from Android 3.x and higher.

Kattiekatuscha answered 5/1, 2012 at 18:55 Comment(3)
The isActivated() methode is only available from API level 11 and higher. I do not recommend using this for comparability reasons with old android OS version. API level 11 is equals to android 3.x what no one is using on their phones.Pennyworth
@phlipp is right, I noticed this in testing (after typing this answer) and switched to selected, but forgot to update my answer.Kattiekatuscha
Thanks beetstra. I noticed a flaw in the pressed solution so I used yours. Easier and flawless so far.Emeraldemerge
W
41

Use the following code. It's useful.

mycodes_Button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mycodes_Button.setPressed(true);
        return true;
    }
});
Wellheeled answered 20/1, 2011 at 13:18 Comment(3)
This basically works, but you should mention, that you lose the normal button functionality. Because you return true here, the onClick stuff is never executed. So actually you have to trigger the stuff, you want the button to do from within this onTouch method. Btw: You should use v.setPressed(true).Regret
no need to handle onClick stuff separately now. just use v.performClick(); in onTouchClincher
There's a little problem with this. I think that when the activity is paused the buttons get unpressed. It's a bummer for me because I display a progress dialog and also navigate to other activities and expect the buttons to keep pressed in these situations.Emeraldemerge
S
8

Would a ToggleButton suit your needs?

Judging from your comments it seems that you aren't aware of the Touch Mode.In this mode, which is the default for most of the things, there is no focus (which is what you are trying to achieve) and no selected items.

You could try to exit the touch mode programmatically, but I wouldn't recommend it. After a small period of getting used to it, the touch mode will give a much better experience to the users.

Spearman answered 20/1, 2011 at 12:53 Comment(6)
After i click the button, it doesn't stay as pressed (i mean with green color), it returns to the original state, gray one. I need normal button and not a ToggleButton.. thanks for your answers!Meldon
Actually i need normal button kostas.. Thanks for response (ευχαριστώ για την απάντηση!)Meldon
A normal button can't stay in pressed state - it can only stay in focused state.Spearman
Well,i think it's the same thing...i mean that's what i want..i want when one button pressed then keep as pressed or focusedMeldon
You probably don't know about the touch mode. See my edited answer.Spearman
you use just state selected in xml file which presents button states in res/drawable file.Terrel

© 2022 - 2024 — McMap. All rights reserved.