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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Nikitas
  • 649
  • 4
  • 9
  • 18

3 Answers3

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.

beetstra
  • 7,942
  • 5
  • 40
  • 44
  • 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. – philipp Feb 29 '12 at 08:43
  • @phlipp is right, I noticed this in testing (after typing this answer) and switched to selected, but forgot to update my answer. – beetstra Feb 29 '12 at 12:30
  • 2
    Thanks beetstra. I noticed a flaw in the `pressed` solution so I used yours. Easier and flawless so far. – Ferran Maylinch May 17 '15 at 11:51
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;
    }
});
Pang
  • 9,564
  • 146
  • 81
  • 122
VenkaReddy
  • 2,871
  • 2
  • 27
  • 29
  • 14
    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)`. – Ridcully Mar 18 '11 at 08:06
  • no need to handle `onClick` stuff separately now. just use `v.performClick();` in `onTouch` – Azhar Bandri Sep 18 '14 at 13:10
  • 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. – Ferran Maylinch May 17 '15 at 11:33
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.

KIDdAe
  • 2,714
  • 2
  • 22
  • 29
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • 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! – Nikitas Jan 20 '11 at 12:55
  • Actually i need normal button kostas.. Thanks for response (ευχαριστώ για την απάντηση!) – Nikitas Jan 20 '11 at 13:03
  • A normal button can't stay in pressed state - it can only stay in focused state. – kgiannakakis Jan 20 '11 at 13:54
  • 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 focused – Nikitas Jan 20 '11 at 14:15
  • 1
    You probably don't know about the touch mode. See my edited answer. – kgiannakakis Jan 20 '11 at 17:48
  • you use just state selected in xml file which presents button states in res/drawable file. – burakim Jun 18 '14 at 08:02