The app I'm trying to develop has a ViewFlipper and some buttons bellow corresponding to views, you click on the third button and the ViewFlipper changes views to the 3rd image, the problem is that once I click that button the timer doesn't reset to 0, so for example if I was on the second image for 4 secs and then if I change to the 3rd image, after 1 sec the ViewFlipper will change to the 4th image, I wanted the timer to reset back to 0 so that way it would stay on the view that I changed to for 5 secs.
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewFlipper.setDisplayedChild(num);
viewFlipper.setFlipInterval(flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
Ok i found the answer, what i needed to do was use a Handler, the problem at first is that the Handler would overwrite other previous handlers and when i changed to one view and then changed to another both handlers would still be waiting, so all i needed to add was this line handler.removeCallbacks(runnable);
Here's my full current code:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
handler.removeCallbacks(runnable);
} catch(Exception e){}
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
viewFlipper.showNext();
viewFlipper.startFlipping();
}
}; handler.postDelayed(runnable, flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}