Android Studio - Looping animation when button is pressed - java

I'm working on making an animation that plays when I press I button. This animation is composed of 3 frames I have made.
As of now, when I press the button, the animation starts, but doesn't stop, it just keeps looping over and over again.
What I would like is the following:
When you press the button, the animation plays once. It doesn't play until you press the button again. HOWEVER, if you press the button while the animation is playing (AKA interrupt the animation before it finishes), the animation will restart.
Here is the code I have that plays the animation when the button is pressed:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.button);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView bun = (ImageView)findViewById(R.id.imageView);
bun.setImageResource(R.drawable.buns);
AnimationDrawable buns = (AnimationDrawable)bun.getDrawable();
buns.start();
}
});
}
}
I'm aware that you can write:
buns.setOneShot(true)
and that will cause the animation to play one time and then stop, but this isn't the solution I'm looking for.
Thanks!

Update:
I figured out the solution to this, in case anyone else was having problems.
It seems if I add the line:
buns.setOneShot(true);
after
buns.start()
this will have the intended affect, since the animation will stop after you click it and won't start until you click it again.
Cheers!

Related

animation on layout with change activity

What I want, when I click on the login button of the first activity the yellow part slides down and the next activity opens. when I click on the signup button of the second screen(login screen) the yellow part of the second screen slides up and the first activity (sign up Activity)opens. I have used slide-down animation on the linear layout on the first screen it works but not working smoothly. Any help??
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mSignUpButton = findViewById(R.id.btnSigUp);
linearLayout=findViewById(R.id.linearLayout1);
mGotoLoginActivityButton=findViewById(R.id.btnLoginSignUpActivity);
slideDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
//listener for Login button
mGotoLoginActivityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//setValidation();
Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
startActivity(intent);
linearLayout.startAnimation(slideDown);
}
});
}
you shouldn't use two separated Activities for this purpose, use one and login and create account views should be packed into Fragments. this way will be way easier to animate between two views/fragments in on Activity, but if you really must use Activity then use Transitions (probably with shared elements), not animations, as these are working in one Activity during its runtime when visible (and you are currently running new Activity, which cover old one)

Camera activity return make button behaviour lost

I'm a total beginner in Android and I tried to make an app that exploit the camera.
In this application a FloatingActionButton is the button that triggered the camera (code below).
This button is hide if the bottomsheet is expand and show if it's collapse.
All of it work perfectly the first time I use the button.
Indeed, once the picture is taken and the mainactivity is back, the button doesn't work anymore and furthermore is behavior is "lost" too (which means that the FloatingActionButton is always shown).
I don't understand what is the source of this bug.
You'll find next to this post 2 screen, one before the button is clicked and one after and the code called in my fragment that make the onclick listener of the button :
The button is hidden when the bottomsheet is expanded (before camera was used):
The button is shown, even if the bottomsheet is expanded (after camera was used):
#Override
public void onStart() {
super.onStart();
mMapsHandler = new MapsHandler(getActivity());
mBottomSheetHandler = new BottomSheetHandler(getActivity());
FloatingActionButton button = (FloatingActionButton) getActivity().findViewById(R.id.cameraButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent); //On change d'activite
}
});

Android - Chronometer setBase() doesn't work properly

I found that if I call start() right after setBase(SystemClock.elapsedRealtime()) it will start counting from 00:00 (which is fine), see below:
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
}
});
However, if I place the setBase(SystemClock.elapsedRealtime()) out of the setOnCLickListener() like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer = findViewById(R.id.main_chronometer);
startButton = findViewById(R.id.main_start_button);
resetButton = findViewById(R.id.main_reset_button);
chronometer.setBase(SystemClock.elapsedRealtime()); //no good here
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
}
});
}
It will start counting from the elapsed time since this app launches. Why?
The behavior you describe is caused by the asynchronous nature of user interaction. Your two examples can be broken down as follows:
Calling setBase inside onClick
App launches
UI is configured; click listener is set on button
Time passes...
User clicks button
Chronometer baseline is set to the current time
Chronometer is started
Notice there is no time passing between steps 5 (chronometer baseline is set) and 6 (chronometer is started), which is why the chronometer correctly starts at 0 when you check its value.
Calling setBase inside onCreate and outside onClick
App launches
UI is configured; click listener is set on button
Chronometer baseline is set to the current time
Time passes...
User clicks button
Chronometer is started
In this version, time passes between steps 3 (chronometer baseline is set) and 6 (chronometer is started), which is why the chronometer does not start at 0 when you check its value.

cant pause mediaplayer after reopen activity

Here in my MainActivity.java I have an object MediaPlayer, which plays a sound when you click playB button and pause by pressing pauseB. Everything is working fine. But if you re-open the app and click pauseB, the sound continues to play. How to fix it? How to catch the current playing MediaPlayer?
public class MainActivity extends ActionBarActivity {
Button playB;
Button pauseB;
Context c;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button playB = (Button) findViewById(R.id.playB);
Button pauseB = (Button) findViewById(R.id.pauseB);
mp = mp.create(this, R.raw.fawaid_1);
playB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
}
});
pauseB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.pause();
}
});
}
}
when you re-open the app, onCreate is called again, hence you have a new MediaPlayer object, and from that moment, play and pause buttons will control that new player object instead of a previous one. That's why your pause button will have no effect on the sound that started BEFORE you minimized the app. And if you press play button now, you will have two sounds playing at the same time
one way to overcome this, is to check if media player has already been initialized:
if(mp==null)
mp = MediaPlayer.create(this, R.raw.fawaid_1);
It is advisable to use Service with AIDL in developing music player.
Reasons.
You can retain the control again once you go back in your activity.
It is easy to perform an inter process communication.
Here is a simple music Player that runs on the Background, Music Player
The song I used in this music player is Owned by SnowFlakes

How to change color of background Android Java in Eclipse ADT

I am very new to mobile development, this isn't homework I am working ahead of my class, I developed a simple app that has a button and when it's pressed it shows a message "Hello Android". I would like to build on this and change the color of the background when the onClickListener is called, I will post my code below, I am asking for the best approach to achieve my goal (change background). I want to iterate that this code below works, and that I am not asking for anything to do with the code I have presented, I want to add to it to change the background color (it's currently white, I'm assuming by default). Oh and I have never worked with Java before (very difficult course teaching android/iOS/WinMobile in 1 class). Thank you.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupMessageButton();
}
private void setupMessageButton() {
// 1. Get a reference to the button
final Button messageButton = (Button) findViewById(R.id.helloDroidButton);
//Set the click listener to run my code.
//Code will run when user clicks button.
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Anonymous class? --> not sure what he means
Log.i("DemoButtonApp", "Hello Android!");
Toast.makeText(
MainActivity.this,
"Hello Android!",
Toast.LENGTH_LONG
).show();
}
});
}
Android support feature called Selector , that help you to change the background of any view in each state of it like pressed , forces and so one , take look on this useful tutorial and feed me back in any not obvious point
http://www.mkyong.com/android/android-imagebutton-selector-example/
hope it help you

Categories

Resources