Animation works only on first button click - android

I was wondering how to handle animations correctly. The code below is working fine but the animation starts only for the first click. It does not work again after the first click.
Layout:
<Button
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/speaker" />
Animation File 'anim.xml':
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="#drawable/choose12"
android:duration="100"/>
<item
android:drawable="#drawable/choose12_1"
android:duration="100"/>
<item
android:drawable="#drawable/choose12_2"
android:duration="100"/>
<item
android:drawable="#drawable/choose12"
android:duration="100"/>
</animation-list>
Activity:
final Button speakButton = (Button)findViewById(R.id.play);
speakButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String words = getResources().getString(R.string.select_age);
speakWords(words);
speakButton.setBackgroundResource(R.drawable.anim.xml);
AnimationDrawable AppNameAnimation = (AnimationDrawable) speakButton.getBackground();
AppNameAnimation.start();
}
});
In the code above the animation is working fine only for the first click, but it won't start at the second (or third, or Nth) click.
How can I start the animation each time the button is clicked?

I had a similar problem.
I fixed it by calling stop on the animation first if I had already played it
mAnimationDrawable.stop();
mImageView.setImageDrawable(mAnimationDrawable);
mAnimationDrawable.start();

Try the following way... Maybe it is because you already set the BackgroundResource to R.drawable.anim.xml. So again this line could not be compiled by the compiler. I think AnimationDrawable would not start for the same resource. If you change it dynamically you can get it.
final Button speakButton = (Button)findViewById(R.id.play);
speakButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String words = getResources().getString(R.string.select_age);
speakWords(words);
speakButton.setBackgroundResource(R.drawable.anim.xml);
AnimationDrawable AppNameAnimation = (AnimationDrawable) speakButton.getBackground();
AppNameAnimation.start();
speakButton.post(new Runnable() {
#Override
public void run() {
if(AppNameAnimation.getCurrent() != AppNameAnimation.getFrame(AppNameAnimation.getNumberOfFrames() - 1))
{
speakButton.post(this);
}else
{
speakButton.removeCallbacks(this);
speakButton.setBackgroundResource(R.drawable.speaker);
}
}
});
}
});

Related

java lang outofmemory error while using Animation in android

I have two buttons, one is used to start animation and other is used to stop animation. On one device, app is working fine, but on another device it crashes saying java.lang.outofmemory exception in my LogCat. Here is my xml for animation.
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/one"
android:duration="200"/>
<item
android:drawable="#drawable/two"
android:duration="200"/>
<item
android:drawable="#drawable/three"
android:duration="200"/>
<item
android:drawable="#drawable/four"
android:duration="200"/>
</animation-list>
And here is my activity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ImageView) findViewById(R.id.imageAnimation);
on_button = (Button)findViewById(R.id.on);
off_button = (Button) findViewById(R.id.off);
view.setBackgroundResource(R.drawable.animation_list);
frameAnimation = (AnimationDrawable) view.getBackground();
on_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
frameAnimation.start();
}
});
off_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
frameAnimation.stop();
}
});
}
M actually trying to make a splash screen, and its my initial attempt.

android change button icon when clicked

I want to know if there is a way of changing a button image when it is clicked.
Initially the button has a pause icon. When it is clicked I want the play icon to be displayed. Each time the button is clicked the icon should vary between play and pause.
Is there any way of doing this?
XML Layout file:
<ImageButton
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:src="#drawable/play_btn"
android:background="#null" />
play_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pause" android:state_selected="true" />
<item android:drawable="#drawable/play" />
</selector>
Android's Toggle Button with a custom selector sounds like what you want.
You might use something like this for your button's selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/play_icon" android:state_checked="true" />
<item android:drawable="#drawable/pause_icon" android:state_checked="false" />
</selector>
Button mPlayButton;
boolean isPlay = false;
#Override
public void onCreate(){
mPlayButton = (Button) findViewById(R.id.play);
// Default button, if need set it in xml via background="#drawable/default"
mPlayButton.setBackgroundResource(R.drawable.default);
mPlayButton.setOnClickListener(mTogglePlayButton);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
#Override
public void onClick(View v){
// change your button background
if(isPlay){
v.setBackgroundResource(R.drawable.default);
}else{
v.setBackgroundResource(R.drawable.playing);
}
isPlay = !isPlay; // reverse
}
};
Let's add an onClick field to your button's xml in your layout (requires API level 4 and onwards)
<ImageButton
android:id="#+id/play"
...
android:onClick="buttonPressed" />
Your icons are drawables pause and play.
Keep track of which icon your button has.
private boolean paused = true;
public void buttonPressed(View view) {
ImageButton button = (ImageButton) view;
int icon;
if (paused) {
paused = false;
icon = R.drawable.pause;
else {
paused = true;
icon = R.drawable.play;
}
button.setImageDrawable(
ContextCompat.getDrawable(getApplicationContext(), icon));
}
Checkout this documentation http://developer.android.com/reference/android/widget/ImageButton.html specifically the inherited methods from android.widget.imageview. You'll want to use either setImageBitmap or setImageDrawable. Update it as part of your on click code.
You can use MaterialButton instead of Button and then just use setIcon method for your button:
import com.google.android.material.button.MaterialButton;
MaterialButton mPlayButton;
boolean isPlay = false;
#Override
public void onCreate(){
mPlayButton = (MaterialButton) findViewById(R.id.play);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
#Override
public void onClick(View v){
// change your button background
if(isPlay){
v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource));
}else{
v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource2));
}
isPlay = !isPlay; // reverse
}
};

Animation List simply deosn't start

I'm working on a project which should have a progressdialog. But since i didn't find an easy way to style a progressdialog, i was thinking, that the easiest way is to create a custom dialog class with it's own style, and a frame by frame animation on it, and show it instead. Here is my code:
The xml layout for the dialog:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialog_background"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="#+id/loaddialog_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/loadanimation"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/loaddialog_text"
style="#style/SmallText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my dialog class:
public class LoadDialog extends Dialog
{
private TextView message;
ImageView image;
AnimationDrawable animation;
public LoadDialog(Context context)
{
super(context, R.style.Dialog);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.load_dialog);
message = (TextView) findViewById(R.id.loaddialog_text);
image = (ImageView) findViewById(R.id.loaddialog_animation);
image.setBackgroundResource(R.drawable.loadanimation);
animation = (AnimationDrawable) image.getBackground();
}
public void setText(String msg)
{
message.setText(msg);
}
#Override
public void show()
{
super.show();
animation.start();
}
#Override
public void dismiss()
{
animation.stop();
super.dismiss();
}
}
and the animation resource:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
<item android:drawable="#drawable/loadbar_01" android:duration="50" />
<item android:drawable="#drawable/loadbar_02" android:duration="50" />
<item android:drawable="#drawable/loadbar_03" android:duration="50"/>
</animation-list>
The problem is that the animation doesn't starts, when i try to show it. I know, there are a lots of topics about this problem, but here are the things, that i have tried:
Put the dialogs show() in different parts of my activity: onResume() and onCreate().
onWindowFocusChanged() is not an option, since i want to show a dialog. dialog shown ->focus change, dialog dismissed->focus change ->infinite amount of dialogs shown.
I have tried animation.setCallback(image), animation.setVisible(true, true), animation.invalidateSelf() none of them works.
I have tried image.post() and put a runnable in there as a parameter, and in the run method starting the animation, no luck.
At this point i'm starting to run out of options. The I'm only trying to show 3 images changing until the dialog is dismissed. Please if you know, what am i doing wrong, or any alternative, let me know!
Thanks in advance!
Change android:oneshot="true" to android:oneshot="false" so that it repeats. It should be currently only doing one cycle and since your show time is 50, that is really fast. I would also change that to maybe 200 or so..
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" >
<item android:drawable="#drawable/loadbar_01" android:duration="200" />
<item android:drawable="#drawable/loadbar_02" android:duration="200" />
<item android:drawable="#drawable/loadbar_03" android:duration="200"/>
</animation-list>
Also, you are setting the image in your xml layout, but animated the background, so you need to either change the xml layout to this:
<ImageView android:id="#+id/loaddialog_animation" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/loadanimation"
android:layout_marginRight="10dp" />
or change you code to get the src file rather than the background, like this:
image = (ImageView) findViewById(R.id.loaddialog_animation);
image.set(R.drawable.loadanimation);
animation = (AnimationDrawable) image.getDrawable();
Faced the same issue today. Placing the animation.start() in the onShow() method of OnShowListener did the trick for me.
public class CustomProgressDialog extends Dialog
{
ImageView progressImage;
AnimationDrawable frameAnimation;
public CustomProgressDialog(Context context)
{
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_progress);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
progressImage = (ImageView) findViewById(R.id.custom_progress_image);
progressImage.setBackgroundResource(R.anim.progress_anim);
frameAnimation = (AnimationDrawable) progressImage.getBackground();
OnShowListener osl = new OnShowListener()
{
#Override
public void onShow(DialogInterface dialog)
{
frameAnimation.start();
}
};
setOnShowListener(osl);
OnDismissListener odl = new OnDismissListener()
{
#Override
public void onDismiss(DialogInterface dialog)
{
frameAnimation.stop();
}
};
setOnDismissListener(odl);
}
}

Android animation not playing

I am trying to get a frame by frame animation to run on android 2.2 in a 2.2 vm machine using eclipse and i cannot for the life of me get it to run. It just sits there on the first frame of the animation.
Here is my activity code:
public class SpriteAnimationActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView lView = (ImageView) findViewById(R.id.imageView1);
AnimationDrawable lAnimation = (AnimationDrawable)lView.getBackground();
lAnimation.start();
}
}
Here is my animation list xml file:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="#drawable/a1" android:duration="100" />
<item android:drawable="#drawable/a2" android:duration="100" />
<item android:drawable="#drawable/a3" android:duration="100" />
</animation-list>
I am using the imageView widget on my main screen to display the image by setting the background. I tried to follow the guide here http://developer.android.com/guide/topics/graphics/drawable-animation.html but I can't seem to get it to work
Here you can get help:
Simple tween animation example
Android animation example on Google Code
You have to start animation forcefully...
ImageView lView = (ImageView) findViewById(R.id.imageView1);
lView.setBackgroundResource(R.drawable.my_animation_list);
final AnimationDrawable lAnimation = (AnimationDrawable)lView.getBackground();
// Button btn=
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lAnimation.start();
}
});
You have to start the animation manually through button click listener or use the onWindowFocusChange listener like below
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
lAnimation.start();
}
}

How to set state color of button in android?

I want to custom button ,
If user pressed it will show red color and still show red until user pressed other button
how to do this? thanks.
Try this code:
final Button b1 = (Button)findViewById(R.id.btn_1);
final Button b2 = (Button)findViewById(R.id.btn_2);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setBackgroundColor(Color.RED);
b2.setBackgroundColor(Color.WHITE);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setBackgroundColor(Color.RED);
b1.setBackgroundColor(Color.WHITE);
}
});
Use OnClickListeners to change the background of the Button using setBackgroundColor() or setBackgroundDrawable()
You can use a CheckBox for your button and set the background to a state list drawable that tests for the android:state_checked attribute.
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.RED);
}
});
button2.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.WHITE);
}
});
What you want is a state list. A quick Google found this article: http://blog.androgames.net/40/custom-button-style-and-theme/ that explains them step by step. That way you don't need any code :)
Here's a code that you need to save as an .xml file and place into your drawable folder.
The android:drawable tags point to the drawable resources for each button state. You can shorten this list if you want to.
Then you can use it as a drawable when creating your layout.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/menu_button_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/menu_button_normal" />
<item android:state_pressed="true" android:drawable="#drawable/menu_button_pressed" />
<item android:state_enabled="true" android:drawable="#drawable/menu_button_normal" />
<item android:state_enabled="false" android:drawable="#drawable/menu_button_normal"/>
</selector>

Categories

Resources