How to make a button bigger when pressed? (Android Studios)

Viewed 2207

I'm working on an app in Android studios and would like my customised button to get bigger when pressed and go back to the default defined state when released. I wrote an xml file to define my button but it didn't work. Here's my xml file for the custom button:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/play_button_pressedhdpi"
        android:state_pressed="true" />
    <item android:drawable="@drawable/play_button_defaulthdpi"
        android:state_focused="true" />
    <item android:drawable="@drawable/play_button_defaulthdpi" />
</selector>

This doesn't seem to work. Any help would be appreciated.

Edit:

I just tried the following code as mentioned in the first answer below:

button1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) v.getLayoutParams();
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                //sendMessage("Key1\n");
                lp.width = 200;
                lp.height = 200;
                button1.setLayoutParams(lp);
            }

            if(event.getAction() == MotionEvent.ACTION_UP) {
                lp.width=90;
                lp.height=90;
                button1.setLayoutParams(lp);
            }
            return false;
        }
    });

This makes sense but didn't work for some reason.

Any help would be appreciated...

2 Answers
Related