Why won't a ViewProperty animation run more than once? - android

I've got a simple View property animation attached to a button. The animation works perfectly on the first button press. However, on subsequent clicks no animation occurs. The log proves the button is being pressed. Is there something I'm missing? Here is the code:
Button handle = (Button) findViewById(R.id.HandleButton);
Button wheel1 = (Button) findViewById(R.id.Wheel1);
handle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Button Clicked");
ViewPropertyAnimator spinWheel1 = wheel1.animate().rotationX(360*8).setDuration(2000);
}
});

Button handle = (Button) findViewById(R.id.HandleButton);
handle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Button Clicked");
spinWheel1 = wheel1.animate().rotationXBy(360*8).setDuration(2000);
}
});
Try using rotationXBy instead of rorationX, you may also want to try using object animator as you can set start and end rotation degree.

try this:
ObjectAnimator animator = ObjectAnimator.ofFloat(spinWheel1, View.ROTATION, 0, 360*8);
animator.setDuration(2000);
animator.start();
your button already has rotated to 360*8
By adding the 0 your animation will start all over again

Related

How to move a button along x-axis while using ObjectAnimator

I want to move a button(button1) when I click on it threw the x-axis.
Here is my code:
btn=(Button)findViewById(R.id.button1);
currentX=btn.getX();
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX,currentX+10);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
currentX=v.getX();
currentY=v.getY();
}
});
It moves at the first click, but after that it just starts from the beginning, instead of moving 10dp further on the x-axis. what am I missing?
You need to either create a new animator or you need to update the values of the moveX and moveY animations. (Actually, you do not need the moveY animation since you are not changing the Y position.)
Also, Views have a more performant animation capability using .animate() to acquire a ViewPropertyAnimator. I would refactor your code this way:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.animate().xBy(10).start()
}
});
If later you decide you also want to animate the Y position, you can simple add .yBy(value) before the .start().
i don't think the v.getx will change after the anime
try this :
int multi =1;
btn=(Button)findViewById(R.id.button1);
currentX=btn.getX();
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX+10*(multi-1),currentX+10*multi);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
multi++;
currentY=v.getY();
}
});
It is because you are not holding the position of x-axis to which your button has moved,after click you set it again to intial value.
Change these lines
use a static variable.
static int x_axis=10+btn.getX();
currentX=x_axis;
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX,currentX);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
x_axis=x_axis+10;
currentY=v.getY();
}
});

Android can not remove button

I am trying to remove a button when the button itself is tapped, I am trying the following:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
tagsView.removeView(button);
}
};
}
This code is working, but when I add the following line of code:
editText.setText(button.getText());
The code stops working and the button does not get removed. I add it like so:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
tagsView.removeView(button);
}
};
}
What is the problem here?
use this in your OnClick method
button.setVisibility(view.GONE);
Your code will look like this
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
button.setVisibility(view.GONE);
}
};
}
Or Try this
Button mybtn = (Button)findViewById(R.id.mybtn_id);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mybtn.setVisibility(view.GONE); // or (view.INVISIBLE)
}
});
Depending on what you're trying to achieve, something like deejay proposed would work just fine. If want the button to hide, call button.setVisibility(View.INVISIBLE). However, if you are trying to dismiss it completely from the view hierarchy, call button.setVisibility(View.GONE).
just set button visibility to false
Obviously button.setVisibility(View.GONE) comes to mind but if it doesn't work you should look one level above for the source of the bug. Maybe you don't set OnClickListener you created to the button and hence nothing happens?

Is it possible to click on two buttons at the same time, if one covers the other one?

Sorry for the weird question, but is it possible to click on two buttons at the same time in android(having two logs, "clicked on b1" and "clicked on b2"), if one totally covers the other one?
This is not ordinarily possible; the top button will absorb the button click event and not pass it on to the one behind it. It is not clear whether or not you want to obtain this behaviour or avoid it, nonetheless, you can force it by propagating the event manually across the click listeners.
Here is one way (there are a few); assume buttonOne is on top of buttonTwo:
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
buttonTwo.performClick();
}
});
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
});
The click event enters the listener on button one, which then causes a click event on button two.
Here is another way which would work (and could be changed to support long click events easily):
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
final OnClickListener listenerTwo = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
};
final OnClickListener listenerOne = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
listenerTwo.onClick(buttonTwo);
}
};
buttonOne.setOnClickListener(listenerOne);
buttonTwo.setOnClickListener(listenerTwo);
Yes, it is possible. You will need to pass the click event that occurs on the foreground view to the background view. You can do this by checking where the click occurs and if it occurs within the view's bounds.

Button click to change background image of clicked button

I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
}
}//end void onClick
});//end test button on click listener
try
testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));
However ToggleButton might suit your case better.
As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.
I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:
final Drawable first = getResources().getDrawable(
android.R.drawable.arrow_up_float);
final Drawable second = getResources().getDrawable(
android.R.drawable.arrow_down_float);
final Button testButton = (Button) findViewById(R.id.toggleButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (testButton.getBackground().equals(first)) {
testButton.setBackgroundDrawable(second);
} else {
testButton.setBackgroundDrawable(first);
}
}
});
as the other friends answered , it is preferable to use the ToggleButton in Android ,
and in your case, if you want to keep your code , so your method should be like this :
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});//end test button on click listener
You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.
You can use Buttons or Image buttons..
private ImageButton mod1,mod2;
mod1 = (ImageButton) findViewById(R.id.mod1);
mod2 = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);
public void onClick(View v) {
mod1.getDrawable().clearColorFilter();
mod2.getDrawable().clearColorFilter();
switch (v.getId()) {
case R.id.mod1:
mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
case R.id.mod2:
mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
}
}

Button in slidingdrawer!

I'm having a button in a sliding drawer in a Android Application. The problem is it does not seem to react to any clicks as normal buttons do.
I'm guessing the problem is that it's a different view than buttons on the normal view.
If I implement a button the normal way like this
myAgenda = (Button)findViewById(R.id.BtnMyAgenda);
myAgenda.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.BtnMyAgenda:
test.setAnimation(leftLeft);
test.startAnimation(leftLeft);
break;
}
I'm guessing there is something wrong with the above code since the button is in a SlidingDrawer and not in the "normal" view.
Any ideas how to fix the problem?
Here is the code
Register with event listner like below code
button.setOnClickListener(clickButtonListener);
and create this listner for button
private OnClickListener clickButtonListener= new OnClickListener()
{
#Override
public void onClick(View v)
{
if(v == button)
{
}
}
}
I actually found the solution to the problem, I simply created a new view.onclicklistener specific to that button.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});

Categories

Resources