Search code examples
androidxmlandroid-animationseekbarandroid-seekbar

how to use prev button and next button change seekbar


In my TopRated.java file, I have an image view which changes when the seekbar is used. It also changes when the next and previous buttons are used. however, i want both buttons and the seeker to be in sync, that is, when the next button is pressed, i want it to also move the seeker up by one, and when the previous button is pressed, i want it to also move the seeker down by one. please help

This is my TopRated.java file

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;


public class TopRated extends Activity {
SeekBar imgseekbar;
ImageView iconimageview;
Button prevbutton;
Button nextbutton;
int progress=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.toprated);
    iconimageview=(ImageView)findViewById(R.id.imageView);
    imgseekbar=(SeekBar)findViewById(R.id.seekBar);
    prevbutton=(Button)findViewById(R.id.button8);
    nextbutton=(Button)findViewById(R.id.button9);
    iconimageview.setImageResource(R.drawable.diablo);

    imgseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            updateImageResource(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    prevbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progress--;
            updateImageResource(progress);
        }
    });
    nextbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progress++;
            updateImageResource(progress);
        }
    });
}

private void updateImageResource(int progress) {
    if (progress==2) {
        iconimageview.setImageResource(R.drawable.eye);
    }else if (progress==3) {
        iconimageview.setImageResource(R.drawable.facebook);
    }else if (progress==4) {
        iconimageview.setImageResource(R.drawable.google);
    }else if (progress==5) {
        iconimageview.setImageResource(R.drawable.house);
    }else if (progress==6) {
        iconimageview.setImageResource(R.drawable.mail);
    }else if (progress==7) {
        iconimageview.setImageResource(R.drawable.pen);
    }else if (progress==8) {
        iconimageview.setImageResource(R.drawable.photos);
    }else if (progress==9) {
        iconimageview.setImageResource(R.drawable.skype);
    }else if (progress==10)
    iconimageview.setImageResource(R.drawable.twitter);

}
}

This is my toprated.xml file

<?xml version="1.0" encoding="utf-8"?>

<SeekBar
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:max="10"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_below="@+id/seekBar"
    android:layout_centerHorizontal="true"
    android:minHeight="50dp"
    android:minWidth="50dp"
    android:background="@drawable/icon_animation"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Prev"
    android:id="@+id/button8"
    android:layout_alignBottom="@+id/imageView"
    android:layout_toStartOf="@+id/imageView"
    android:layout_marginRight="5dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/button9"
    android:layout_alignBottom="@+id/imageView"
    android:layout_toEndOf="@+id/button8"
    android:layout_marginLeft="50dp" />

I also have another xml which holds all my images, icon_animation.xml

please help! any help would be appreciated.


Solution

  • use below line to update seek bar

    seekbar.setProgress(int)