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);
Related
*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);
}
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);
}
I have an image view. So in the image view i want to change the images after a certain time period. Images are coming from an array list. Now when the no of images are 3 or more than 3, it is working perfect. But when it is 2, my logic is not working. Second image is visible for a moment and then again changed to first image here is my code:
r = new Runnable(){
int i = 0;
public void run(){
iv.setImageBitmap(alBmps.get(i));
i++;
if(i >= alBmps.size()){
i = 0;
}
iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
}
};
iv.postDelayed(r, 1000);
Can any one help me what changes i need on the above code?
Thanks.
Try this
declare variables
static int i=0;
private Timer myTimer;
in your onCreate or on button click where you want to call and start the methods
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 500, 5000);
add these methods to your class
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
if(i<alBmps.size())
{
iv.setImageBitmap(alBmps.get(i));
}
else
{
i=0;
iv.setImageBitmap(alBmps.get(i));
}
i++;
}
};
I want to schedule an action to change automatically my ViewPager pages.
I've tried:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
featureViewPager.setCurrentItem(currentPage++, true);
}
}, 100, 500);
but I'm always getting:
E/AndroidRuntime(5381): FATAL EXCEPTION: Timer-0
E/AndroidRuntime(5381): java.lang.IllegalStateException: Must be called from main thread of process
I'm already in main thread right? How can I solve this?
Thanks for your time.
EDIT:
====================================
Thanks for all your answers. Based on these responses I came across 2 solutions:
Solution 1:
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
featureViewPager.setCurrentItem(currentPage++, true);
}
});
}
}, 500, 3000);
Solution 2:
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
featureViewPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 500, 3000);
Which one is better or they are the same?
Thanks once again.
If you want to use thread in the main UI then you need to use a hander to hand it.
Handler handler = new Handler();
Runnable update = new Runnable() {
public void run() {
if ( currentPage == NUM_PAGES ) {
currentPage = 0;
}
featureViewPager.setCurrentItem(currentPage++, true);
}
};
new Timer().schedule(new TimerTask() {
#Override
public void run() {
handler.post(update);
}
}, 100, 500);
Easiest way how to solve it is to create an postDelayed runnable
private Handler mHandler;
public static final int DELAY = 5000;
Runnable mRunnable = new Runnable()
{
#Override
public void run()
{
//TODO: do something like mViewPager.setCurrentPage(mIterator);
mHandler.postDelayed( mRunnable , DELAY );
}
};
as You can see it will loop infinitelly. When you want to stop it, just simply call
mHandler.removeCallbacks( mRunnable );
The TimerTask will runs on it's own thread ( = not the UI thread).
You can simply call setCurrentItem directly on the main thread using a Handler.
For the kotlin extension lover. This can be helpful on viewPage2 auto page change
fun ViewPager2.enableAutoScroll(totalPages: Int): Timer {
val autoTimerTask = Timer()
var currentPageIndex = currentItem
autoTimerTask.schedule(object : TimerTask() {
override fun run() {
currentItem = currentPageIndex++
if (currentPageIndex == totalPages) currentPageIndex = 0
}
}, 0, DELAY_FOUR_SECONDS)
// Stop auto paging when user touch the view
getRecyclerView().setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) autoTimerTask.cancel()
false
}
return autoTimerTask // Return the reference for cancel
}
fun ViewPager2.getRecyclerView(): RecyclerView {
val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
recyclerViewField.isAccessible = true
return recyclerViewField.get(this) as RecyclerView
}
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.
}