0

I have tries to switch the images on button click..but i failed and got the errors Here is my code....please somebody help me!! please add XML code if required also please post the same also

package com.conn;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher.ViewFactory;

public class image_slider extends Activity
{

        Integer[] imageIDs = { R.drawable.haha,  R.drawable.dte,R.drawable.new_login };
        private ImageSwitcher imageSwitcher;
        private Button nextButton;
        private Button previousButton; 

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.imgslide);
            imageSwitcher.setImageResource(imageIDs[0]);
            nextButton = (Button) findViewById(R.id.next);
            nextButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    final Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);       
                    final Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);     
                    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);      
                    imageSwitcher.setFactory(this);   
                    imageSwitcher.setInAnimation(in);      
                    imageSwitcher.setOutAnimation(out);             
                    imageSwitcher.setImageResource(imageIDs[1]);
//                  imageSwitcher.setImageResource(imageIDs[1]);

                }

            }); 

            previousButton = (Button) findViewById(R.id.previous);
            previousButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    final Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_left);
                    final Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_right);
                    imageSwitcher.setFactory(this);
                    imageSwitcher.setInAnimation(in);
                    imageSwitcher.setOutAnimation(out);
                    imageSwitcher.setImageResource(imageIDs[0]);

                }

            });
        }
public View makeView()
    {
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0xFF000000);
                imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return imageView;
    }       

}
Chirag
  • 56,621
  • 29
  • 151
  • 198
sunny
  • 164
  • 4
  • 17
  • what errors did you get? – John Boker Jun 20 '11 at 12:18
  • loadAnimation of previous button i got The method loadAnimation(Context, int) in the type AnimationUtils is not applicable for the arguments (new View.OnClickListener(){}, int) – sunny Jun 20 '11 at 12:29
  • Also in "slide_out_left" and "slide_in_right" red underline is coming saying "slide_in_right/slide_out_left cannot be resolved or is not a field" – sunny Jun 20 '11 at 12:33
  • thank u again...and wat about the setFactory!!!!!!!!!!!1 – sunny Jun 20 '11 at 12:57

1 Answers1

1

Your problem is that when you refer to this in your OnClickListener your are referring to that particular listener and not to your current Activity. You should change it to

 final Animation out= AnimationUtils.loadAnimation(image_slider.this, android.R.anim.slide_out_right); 
 final Animation in= AnimationUtils.loadAnimation(image_slider.this, android.R.anim.slide_in_left);
 ...
 imageSwitcher.setFactory(image_slider.this);

And use android.R.anim.slide_in_left and android.R.anim.slide_out_right not slide_out_left and "slide_in_right" because these don't exist.

BTW, good Java practice is to start your class name with a capital letter, e.g. ImageSlider

THelper
  • 15,333
  • 6
  • 64
  • 104
  • 06-20 19:03:41.111: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{45148388 com.conn/.image_slider} now i am getting this error while running...huh...plz help.. – sunny Jun 20 '11 at 13:34
  • Check [this post](http://stackoverflow.com/questions/4283449/activity-idle-timeout-for-historyrecord) for a solution, or create a new question for it. – THelper Jun 20 '11 at 13:40