How to programmatically scale a button or view after clicking it
I have several buttons in my application that I want to scale when clicked
for this i created a selector
selector_sms_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_sms_big" android:state_pressed="true"></item>
<item android:drawable="@drawable/ic_sms" android:state_pressed="false"></item>
</selector>
and declared my button like this
<Button
android:id="@+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_sms_button" />
the problem is that the button doesn't grow when you click on all
the ic_sms_big size is equal 300x300dp
and the ic_sms size is72x72dp
I don't want to create an animation file for them unless I absolutely have to. the problem is that the button has to grow at least 3 times in size when clicked, which it doesn't.
What can I do to fix this?
EDIT: my query "I don't want to create an animation file" means I don't want to create an XML file for it, but I don't want to link to it in code at all. I have 6 buttons that should do this
source to share
If you don't want to add the file, you can do it programmatically
Animation anim = new ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(true); // Needed to keep the result of the animation
v.startAnimation(anim);
source to share
To implement it programmatically, you can use the following code:
private void animateView() {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation growAnimation = new ScaleAnimation(1.0f, 3.0f, 1.0f, 3.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ScaleAnimation shrinkAnimation = new ScaleAnimation(3.0f, 1.0f, 3.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(growAnimation);
animationSet.addAnimation(shrinkAnimation);
animationSet.setDuration(200);
viewToAnimate.startAnimation(animationSet);
}
source to share
Try to remove android: state_pressed = "false" on small button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_sms_big" android:state_pressed="true"></item>
<item android:drawable="@drawable/ic_sms" ></item>
</selector>
And also you must take this into account:
- state_selected is used when an item is selected using Keyboard / DPad / trackball / etc ..
- state_activated is used when View.setActivated (true) is called. This is used for "persistent selection" (see, for example, "Settings on the tablet").
- state_pressed is used when the user clicks on an element either by pressing, keyboard or mouse.
- state_focused is used if the element is marked as focusable and it receives focus either via the keyboard user / dpad / trackball / etc. or if the element is focusing in touch mode
source to share