To increase a button's size when it is pressed in Android
Asked Answered
T

5

5

How can I make my button's size to increase slightly when it is pressed and decrease again when it is released? This is for highlighting the pressed button, by using size in addition to a different color.

Regards, Kiki

Theca answered 23/9, 2010 at 12:35 Comment(0)
J
3

Make your Button a field, and modify its size in the OnTouchListener associated with it. Using an OnTouchListener you can listen for different MotionEvents, such as ACTION_DOWN and ACTION_UP.

Jute answered 23/9, 2010 at 12:37 Comment(0)
R
2

Since API 21 (Lollipop) an xml-only solution is available based on the stateListAnimator attribute. Even though its primary goal to animate view properties based on state changes you can disable the animation if it's required by setting duration="0". For example:

<!-- res/layout/my_layout.xml -->

<LinearLayout ...
  android:stateListAnimator="@animator/zoom_animator">...</layout>
<!-- res/animator/zoom_animator -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <set>
      <objectAnimator android:propertyName="scaleX" android:valueTo="1.0" android:duration="0" />
      <objectAnimator android:propertyName="scaleY" android:valueTo="1.0" android:duration="0" />
    </set>
  </item>
  <item android:state_pressed="true">
    <set>
      <objectAnimator android:propertyName="scaleX" android:valueTo="1.1" android:duration="0" />
      <objectAnimator android:propertyName="scaleY" android:valueTo="1.1" android:duration="0" />
    </set>
  </item>
</selector>

There's also two similar questions about animated scale:

Ridgway answered 19/11, 2020 at 11:55 Comment(3)
this first link has the best solutionCorpuscle
github.com/material-components/…Corpuscle
This answer does not work at my project until I add android:state_pressed="false" in the default item.Lowther
S
1
button.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();

        //Code to convert height and width in dp.
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, getResources().getDisplayMetrics());
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, getResources().getDisplayMetrics());

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            layoutParams.width = width + 5;
            layoutParams.height = height + 5;
            SearchButton.setLayoutParams(layoutParams);
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            layoutParams.width = width;
            layoutParams.height = height;
            SearchButton.setLayoutParams(layoutParams);
        }

        return false;
    }
});
Shaylyn answered 19/5, 2016 at 10:48 Comment(0)
L
-1

In your activity

Private Button myButton;

//...

protected void onCreate(Bundle savedInstanceState) {

//...

myButton = (Button) findViewById(R.id.my_button);
    _button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        myButton.setWidth(50);
        myButton.setHeight(50);
        }
      });
    }
Lizarraga answered 23/9, 2010 at 15:59 Comment(2)
But my issue is to increase button size when pressed and get back to normal size when button is released.Theca
use an ontouch listener then. in that u need to define Motionevent.On actiion up. On action down. etc.Alexandro
F
-1
//Try it

Private Button myButton;

//...

protected void onCreate(Bundle savedInstanceState) {

//...

myButton = (Button) findViewById(R.id.my_button);
    _button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        myButton.setWidth(myButton.getWidth()+5);
        myButton.setHeight(myButton.getHeight()+5);
        }
      });
    }
Fernandefernandel answered 31/8, 2020 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.