*I am trying to auto slide image in both direction but, I am unable to de so. It is only auto sliding from left to right. *
This is the code for MainActivity where i created my viewPager
**ViewPager viewPager;
viewPager = findViewById(R.id.viewPager);
Timer timer;
**
final long DELAY_MS = 1000;//delay in milliseconds before task is to be executed
final long PERIOD_MS = 5000; // time in milliseconds between successive task executions.
#SuppressLint("ResourceType")
public void setUpOffers(){
ArrayList<String> urls = new ArrayList<>();
final ArrayList<String> promoCodes = new ArrayList<>();
for(OfferDetail offerDetail:new utility().getSpOffers()){
urls.add(offerDetail.getImage());
promoCodes.add(offerDetail.getPromo_code());
}
FeaturedOffersAdapter featuredOffersAdapter = new FeaturedOffersAdapter(MainActivity.this,urls);
viewPager.setAdapter(featuredOffersAdapter);
//viewPager.setScrollDurationFactor(10);
/*After setting the adapter use the timer */
final Handler handler = new Handler();
final Runnable Update = () -> {
if (currentPage == urls.size()) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
};
timer = new Timer(); // This will create a new Thread
timer.schedule(new TimerTask() { // task to be scheduled
#Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
}
Related
I'm trying to slide my ViewPager automatically via using TimerTask class, seems I do not have proper delay and period, it is sliding so fast. I tried all possible combinations of delay and period parameters without any luck, still so annoying fast sliding. Below is the code:
class SliderTimer extends TimerTask {
#Override
public void run() {
HomeActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if (viewPager.getCurrentItem() < listSlides.size() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
} else {
viewPager.setCurrentItem(0);
}
}
});
}
}
And the implementations:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new HomeActivity.SliderTimer(), 10000, 10000);
Please guide me, what best can be done for the same.
I think Using Handler is better then TimerTask in this case if ViewPager can slide manually too.
First Create a Handler and Runnable Globally.
private Handler handler=new Handler();
private Runnable runnable=new Runnable() {
#Override
public void run() {
if(pagerSlider.getCurrentItem()==data.size()-1){
pagerSlider.setCurrentItem(0,false);
}else{
pagerSlider.setCurrentItem(pagerSlider.getCurrentItem()+1);
}
}
};
Post the runnable inside onPageChange.
pagerSlider.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
handler.removeCallbacks(runnable);
handler.postDelayed(runnable,2000);
}
});
You need to post for first time Rest the listener will do. Change the delay as per your need :-
handler.postDelayed(runnable,2000);
I just realize that you might be asking about scrolling velocity . Well for this you need to use Customize Scroller. Go to This thread.
Try this one...
public static final long DELAY_MS = 2000;
public static final long PERIOD_MS = 4000;
new MyTimerHeader(DELAY_MS, PERIOD_MS, viewPager, images_total_length);
public MyTimerHeader(long DELAY_MS, long PERIOD_MS, final ViewPager slider1, final
int image_name) {
final int image_name1=image_name;
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
int currentPage = slider1.getCurrentItem();
if (currentPage == image_name1 - 1) {
currentPage = 0;
}
else {
currentPage = currentPage + 1;
}
slider1.setCurrentItem(currentPage);
}
};
Timer timer = new Timer(); // This will create a new Thread
timer .schedule(new TimerTask() { // task to be scheduled
#Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
}
Hi how i'am to back again my banner. if the gets last banner and back again to early banner. Thanks Before Guys
This example my code :
//Banner Variable
private int currentPage = -1;
private static final int NUM_PAGES = 0;
final Handler handler = new Handler();
final Runnable update = new Runnable() {
#Override
public void run() {
if (currentPage == NUM_PAGES - 1) {
currentPage = 0;
}
vpbanner.setCurrentItem(currentPage++, true);
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(update);
}
}, 1000, 2000);
Hope below code solve your problem :-
vpbanner.setCurrentItem(currentPage == 0 ? 4 : 0, true);
I m trying to load images in an image switcher from Http server. I didnot find any function like setImageBitmap. So I tried using setImageURI() , but its not getting loaded.
I am tring to switch image after every 3 sec. This is the code. When i m running the codes image is not getting loaded. And app is also getting crased.
String arr[]={"http://192.168.1.7/photos/dummy/1.jpg","http://192.168.1.7/photos/dummy/2.jpg","http://192.168.1.7/photos/dummy/3.jpg"}
dailyWear = (ImageSwitcher) getActivity().findViewById(R.id.imageDailyWear);
dailyWear.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView myView = new ImageView(getActivity());
myView.setScaleType(ImageView.ScaleType.FIT_XY);
myView.setLayoutParams(new ImageSwitcher.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
return myView;
}
});
dailyWear.setInAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
dailyWear.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_out_right));
final Handler handler = new Handler();
final Runnable r = new Runnable() {
int i=0;
public void run() {
weddingWear.setImageURI(Uri.parse(arr[i));
i++;
if (i >= arr.length()-1)
i = 0;
handler.postDelayed(this, 3000);
}
};
handler.postDelayed(r, 1000);
what you can do is first get these images and store it in arraylist of bitmap that you can switch these images
private Context mContext;
private int index = 0;
private final int interval = 3000;
private final int DURATION=1500;
public void animate_Images_In_Top_View_After_Every_Three_Seconds(
ImageSwitcher imageSwitcher, final ArrayList<Bitmap> _Images_List) {
android.view.animation.Animation aniIn = AnimationUtils.loadAnimation(mContext,
android.R.anim.fade_in);
aniIn.setDuration(DURATION);
android.view.animation.Animation aniOut = AnimationUtils.loadAnimation(mContext,
android.R.anim.fade_out);
aniOut.setDuration(DURATION);
final ImageSwitcher _ImageSwitcher = imageSwitcher;
_ImageSwitcher.setInAnimation(aniIn);
_ImageSwitcher.setOutAnimation(aniOut);
_ImageSwitcher.setFactory((android.widget.ViewSwitcher.ViewFactory) mContext);
_ImageSwitcher.setImageDrawable(new BitmapDrawable(_Images_List.get(index)));
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
index++;
index = index % _Images_List.size();
// Log.d("Intro Screen", "Change Image " + index);
_ImageSwitcher.setImageDrawable(new BitmapDrawable(_Images_List.get(index)));
handler.postDelayed(this, interval);
}
};
handler.postDelayed(runnable, interval);
}
and i am using fade in and out animation you can set to your own need.
The following program displays full screen images that animate automatically by using the loadAnimation() method. Here is is the code.
public class MainActivity extends ActionBarActivity {
/** Called when the activity is first created. */
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
private int[] IMAGE_IDS = {
R.drawable.picture1, R.drawable.picture2, R.drawable.picture3,
R.drawable.picture4
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 1000; // delay for 1 sec.
int period = 3000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
/**
* Helper method to start the animation on the splash screen
*/
private void AnimateandSlideShow() {
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
slidingimage.startAnimation(leftToRight);
}
}
As you can see the full sized picture change every 4 seconds. Now what i want to do is to stop the animation after the appearance of the last image(picture 4 in this case). After fixing this,I will try fetch images from a server using JSON.
Thank you,
Theo.
1)You have to load animation until only you have image in IMAGE_IDS array
2)Then stop your timer also
Add following lines in AnimateandSlideShow() below currentimageindex++
if(currentimageindex <= IMAGE_IDS.length)
{
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.letf_anim);
slidingimage.startAnimation(leftToRight);
}
else
{
timer.cancel();
}
Also replace following line
Timer timer = new Timer();
To
timer = new Timer();
I am using an ImageSwitcher with a TouchListener to change images from an array. Its working fine but i want it to switch images every x seconds or so, so that I can add imageSwitcher.setImageResource(imageList[curIndex]); to it.
Any suggestions?
Try this,
imageSwitcher.postDelayed(new Runnable() {
int i = 0;
public void run() {
imageSwitcher.setImageResource(
i++ % 2 == 0 ?
R.drawable.image1 :
R.drawable.mage2);
imageSwitcher.postDelayed(this, 1000);
}
}, 1000);
I think it is possible via TimerTask and Timer. please Try this code. I think It help you.
private Handler mHandler;
private Runnable mUpdateResults;
private Timer timerAnimate;
private TimerTask timerTask;
mHandler = new Handler();
mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 0;
int period = 15000;
timerAnimate = new Timer();
timerTask = new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
};
timerAnimate.scheduleAtFixedRate(timerTask, delay, period);
Public void AnimateandSlideShow()
{
imageSwitcher.setImageResource(imageList[curIndex]);
///Here You need To handle curIndex position.
}