How to Pause an activity with Button Click? - android

I want to pause an animation activity with button click, here i have one ball which is falling from top to bottom animation. I want one button "stop" which pauses the fall in between and record its x,y position.

You can add a condition in your ball animation such that
while(buttonPressed == false && //whatever other code you need for the animation to run)
{
//button animation code here
}
and then have an if statement after it, with the condition being that if the buttonPressed is true, that will record the position of the ball. You can set the buttonPressed boolean to be true by using an action listener added to your stop button.

Put this first:
AnimatorSet set;
img_logo = (ImageView) findViewById(R.id.img_logo);
set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.flipping);
set.setTarget(img_logo);
set.start();
On Button Click:
set.stop();
I think this could also help you--->http://android-er.blogspot.in/2012/01/start-and-stop-frame-animation-with.html

Related

Animate an array of buttons in Xamarin Android

I have many buttons in my MainActivity.xml and i wanted to add an animation to all of them such that when the MainActivity is launched all buttons get a TRANSLATIONX animation to bring them into view. I have succesfully created the Button array using button as a data type and added three buttons to it as a sample...
The problem however is iterating through the button array to add animation property to each...
Here is the trier code i used to reach where i am, Suggestions and Solutions are greatly appreciated...
class Prime : AppCompatActivity
{
//Creating button array
Button[] mybuttons;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.Prime);
//Button definitions
Button button1 = this.FindViewById<Button>(Resource.Id.button1);
Button button2 = this.FindViewById<Button>(Resource.Id.button2);
Button button3 = this.FindViewById<Button>(Resource.Id.button3);
//Adding the buttons to an array
mybuttons = new Button[3] { button1, button2, button3 };
//Defining a view animation
ObjectAnimator animator = ObjectAnimator.OfFloat(button3, "translationX", 130f);
animator.SetDuration(2000);
animator.Start();
//This is where i need the loop code to iterate through the button array and to each of them them the animation object property
I tried replacing the button3 parameter in the ObjectAnimator.OfFloat method with mybuttons but it didnt work. Thanks
The loop foreach works fine to access every button in the array and style it with respect to the Object Animator.
foreach(Button button in mybuttons){
ObjectAnimator animator = ObjectAnimator.OfFloat(button, "translationX", 130f);
animator.SetDuration(2000);
animator.Start();
}
The foreach loop is easy to understand and implement for Button arrays than its counter parts

Accessing image of the last clicked item in listview

Below is the code for the listview adapter which displays list of tracks.Each list item has play and stop icons to play and stop track.When user clicks on play image button it changes to pause button image and track starts playing..When I click on pause button image it changes to play button image and track stops playing.While playing a track if I click on another track play image button previous track stops and the current clicked track start playing but the issue is that previous track still show the pause icon image button while it should change to play image button since currently its not playing.It is because holder.img2 visiblity is still true since we never changed the visiblity of image( holder.img1 is for play icon and holder.img2 is for stop icon).For that i need to access the image icon of the last clicked item in listview..What should i do so that the pause image button should change to play image button if i clicked on another track play button while playing current track..
You can use something like this:
Save position in a global variable lastPosition in onClick every time.
Check if position is not default value, then get its related Row, its images, and set their visibility as per need.
int lastPosition = -1;
holder.img1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(final View v)
{
if (lastPosition != -1) {
View lastRow = listView.getChildAt(lastPosition);
ImageView play = (ImageView) lastRow.findViewById(R.id.play2);
ImageView pause = (ImageView) lastRow.findViewById(R.id.pause);
play.setVisibility(View.GONE);
pause.setVisibility(View.VISIBLE);
}
.. //all other code of onClick for media player
lastPosition = position;
}
}
Hope it helps.
One another way could be by saving last clicked row's ImageView directly to variable of SoundCloudAdapter.
Simple declare a variable global to SoundCloudAdapter
ImageView imgRowLastClicked;
and
holder.img1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(final View v)
{
if(imgRowLastClicked!= null)
{
lastClickedimg= imgRowLastClicked;
lastClickedimg.setVisibility(View.GONE);
}
//your other code
imgRowLastClicked= (ImageView) v.findViewById(R.id.play2);
}
}
Do same for pause ImageView

Stop Blinking of TextView

This is a code for blinking textview on a button click..
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
recordShow.startAnimation(anim);
}
i have to stop blinking on another button click...what to do..??
Another approach could be:
1. Declare the Animation and TextView objects globally (outside any methods) in your Activity.
private Animation mAnim;
private TextView mRecordShow;
2. Setup a class that sets your animation properties and starts it. Let this class expect a TextView widget as its parameter.
protected void setBlinkingText(TextView textView) {
mAnim = new AlphaAnimation(0.0f, 1.0f);
mAnim.setDuration(1000); // Time of the blink
mAnim.setStartOffset(20);
mAnim.setRepeatMode(Animation.REVERSE);
mAnim.setRepeatCount(Animation.INFINITE);
textView.startAnimation(mAnim);
}
3. Setup another class that stops your animation on a given text view. Let this class expect a TextView widget as its parameter as well.
protected void removeBlinkingText(TextView textView) {
textView.clearAnimation();
}
4. Now you can use your classes wherever desired, passing it the appropriate text views.
e.g.
(a) In your onClick() method where you want to start the animation, replace all your animation code with:
setBlinkingText(mRecordShow);
(b) wherever you want to stop the animation on that text view, just call:
removeBlinkingText(mRecordShow);
The following assumes you want to stop the blink by clicking the same button. If you want to stop the click using a different button, you can split the if-else in the onClick() below into separate click handlers.
First, move anim outside onClick() and make it a field of the containing class. You need anim to be stored somewhere so you can cancel it later.
Animation anim = new AlphaAnimation(0.0f, 1.0f)
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
Second, create a boolean field in the containing class to keep track of whether the TextView is currently blinking:
boolean mBlinking = false;
Then:
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
if(!mBlinking){
recordShow.startAnimation(anim);
mBlinking = true;
} else{
recordShow.clearAnimation(anim); // cancel blink animation
recordShow.setAlpha(1.0f); // restore original alpha
mBlinking = false;
}
}

changing visibility of text android

Hi i have two textViews that i initially set its visibility to gone then animate in and become visible. now i want to make the invisible again but for some reason they're still showing on screen does anyone no why?
in my onCreate() i make the view gone
register = (TextView)findViewById(R.id.register);
register.setVisibility(View.GONE);
forgotpassword = (TextView)findViewById(R.id.forgotpw);
forgotpassword.setVisibility(View.GONE);
then later on i make it visible
public void run()
{
animations();
loginForm.setVisibility(View.VISIBLE);
register.setVisibility(View.VISIBLE);
forgotpassword.setVisibility(View.VISIBLE);
}
and then when a user presses a button i want the text views to become invisible so that they retain their layout but they stay visible on screen
signInBtn = (Button) findViewById(R.id.signin);
signInBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
signInProcess();
}
});
public void signInProcess() {
register.setVisibility(View.INVISIBLE);
forgotpassword.setVisibility(View.INVISIBLE);
setuploader.setVisibility(View.VISIBLE);
}
In Android when you animate something, It's just drawn somewhere else. The actual element is not moved. So when you animate signInBtn it's drawn somewhere else, but the actual button is not moved from the original position. So when you click the button the click handler is not called.
To avoid this set fillAfter = True in your animation so the button will actually get moved at the end of your animation.
Also, after animating a view in Android make sure you call View.clearAnimation() before trying to change its visibility.

Animation resetting view state?

i am trying to do some simple animation, i have list with items, those items include also checkboxes
on a checkbox button click i want to show with animation some button from the buttom, something like this:
private int mPosition;
private CheckBox chkBox;
OnItemClickListener(CheckBox mChkBox, View v)
{
chkBox = mChkBox;
chkBox.setClickable(false);
chkBox.setChecked(false);
chkBox.setClickable(true);
}
#Override
public void onClick(View v)
{
if (chkBox.isChecked())
{
animation = AnimationUtils.loadAnimation(context,
R.layout.animation_slide_in);
animation.setDuration(500);
animation.setInterpolator(new AccelerateInterpolator());
btDeleteItms.startAnimation(animation);
btDeleteItms.setVisibility(btDeleteItms.VISIBLE);
}
now the wierd thing, that after i click the checkboxbutton, the animation does work fine,but the state of the triggering checkbox button, is being uncheck unexpectdly?
how come? does animation resetting states somehow?
Thaks,
ray.
If I understood you correctly you need to use animation.setFillAfter(true); after .startAnimation(animation) method.

Categories

Resources