0

I have button matrix. After button press action I would like to keep that state, after pressing another button I would like to "unpress" first one and keep pressed new one.

I know that I can achieve this with selector and state_pressed but I would like to avoid define my own drawings. Is there any way to reuse default Android drawings for (un)pressed button? What are the names of them?

patseb
  • 651
  • 1
  • 8
  • 20

3 Answers3

1

Try use setPressed(true) on this view

getKonstantin
  • 1,220
  • 9
  • 14
  • setPressed is not working on the button which is being clicked. If I set this to different button then it is setting pressed state correctly, but on the "caller" button it only blinks and not being hold in pressed state – patseb Oct 14 '13 at 14:48
1

In your SDK you can find all Android standard Styles/Drawables. Here is the File you looking for: sdk\platforms\android-17\data\res\drawable\btn_default.xml Content:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_default_normal" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="@drawable/btn_default_normal_disable" android:state_enabled="false" android:state_window_focused="false"/>
    <item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_default_selected" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_default_normal" android:state_enabled="true"/>      
    <item android:drawable="@drawable/btn_default_normal_disable_focused" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_default_normal_disable"/>
</selector>

Also it would maybe more practible to use a radiobox and a corresponding ControlGroup with theese Styles so you can get easy access to the states programatically

chuck258
  • 912
  • 7
  • 16
  • I'm getting Error: No resource found that matches the given name (at 'drawable' with value '@drawable/btn_default_normal'). – patseb Oct 14 '13 at 14:59
  • You have to use the android namespace for default resources like: @android:drawable/btn_default_normal – chuck258 Oct 14 '13 at 20:38
1

It is right that using setPressed(true) does not keep the button which is being clicked in the pressed state, because after the click the button returns to setPressed(false).

You can override onTouch and implement your own behaviour, like this:

button.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    //this is touch
                    v.setPressed(true);
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    //this is click. Do everything triggered by a click here
                    deselectAllButtons();
                    v.setPressed(true);
                }
                return true;
            }
        });

In deselectAllButtons() I set all my buttons' pressed states to false.

Note that the key idea here is that onClick should not be invoked. That's why onTouch should return true, meaning that the touch event is consumed:

onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

Lohengrin
  • 26
  • 3