Resize button when pressed on Android

I tried resizing my button when I click on it, but nothing works.

Button created in main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:layout_alignParentLeft="true" 
android:layout_height="90px" 
android:id="@+id/button1" 
android:layout_marginTop="120px"
android:layout_width="90px"         
android:textSize="45px" 
android:textStyle="bold"
android:background="@drawable/bnumber" 
android:text="1">
</Button>

And the code for the button logic:

button1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                sendMessage("key1\n");
                button1.setWidth(140);
                button1.setHeight(140);
            }
            if(event.getAction() == MotionEvent.ACTION_UP) {
                button1.setWidth(90);
                button1.setHeight(90);
            }
            return false;
        }
    });

From googling, I'm sure this should work, but for some reason this is not the case.

My device uses Android 1.6, can this be a problem?

+3
source share
3 answers

You can set the width and height of the buttons using the following code to set the width and height of the buttons

button1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        RelativeLayout.LayoutParams lp = (LayoutParams) b.getLayoutParams();
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            sendMessage("key1\n");
            lp.width=140;
            lp.height=140;
            button1.setLayoutParams(lp);
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            lp.width=90;
            lp.height=90;
            button1.setLayoutParams(lp);
        }
        return false;
    }
});

Thanks Deepak

+3
source

When you received the ACTION_DOWN event, if you return false, you will not be able to receive the ACTION_UP event. Try to change

return false;

to

return true;
+2
source

onTouchListener . onClickListener.

ACTION_UP ACTION_DOWN (. ), / .

0

All Articles