I am using View Pager in my App. I need to trigger page change every 1 min. I know that if user swipes, page change happens. But if we have to trigger it, is there any API for it.
How to change page in ViewPager Android?
Use ViewPager.setCurrentItem(int item) for changing ViewPager pages programmatically.
Where item is index of Page which want to set as current item in ViewPager.
private int mInterval = 5000; // 5 seconds by default, can be changed later
private Handler mHandler;
#Override
protected void onCreate(Bundle bundle) {
...
mHandler = new Handler();
startRepeatingTask();
}
Runnable mStatusChecker = new Runnable() {
#Override
public void run() {
updatePage(); //this function can change value of mInterval.
mHandler.postDelayed(mStatusChecker, mInterval);
}
};
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
private void updatePage(){
viewPager.setCurrentItem(pageno)
}
Handler handler = new Handler();
handler.post(new Updater());
class Updater implements Runnable {
public void run() {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
if (mViewPager.getAdapter().getCount() - 1 > mViewPager.getCurrentItem()){
handler.postDelayed(new Updater, 60000);
}
}
}
You can use CountDownTimer do this easily.Change the page after every 60sec using below example
class MyTimer extends CountDownTimer{
public MyTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
myTimer.start();
}
MyTimer myTimer = new MyTimer(1000*60, 1000);
myTimer.start();
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);
}
I need a timer when Loaded ListView . Like " Waiting [(Timer) 42] Seconds for buy elements " I want to show users Textview Like it. I can't use Thread in ListView...
I get Error from runOnUiThread. Why ? I cant use timer In Listview.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TextView LblTime;
inflater=(LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.test,parent,false);
LblTime=(TextView)view.findViewById(R.id.LblFragmentAnaListViewKalanSure );
Timer T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
Context.getApplicationContext().runOnUiThread(new Runnable() {
#Override
public void run() {
long Time= MyTime.get(position).getTime() - (new Date()).getTime();
long Sec= Time/ 1000 % 60;
long Min= Time/ (60 * 1000) % 60;
LblTime.setText(Min+":"+Sec);
}
});
}
}, 1000, 1000);
return view;
}
Try this.. for call atfer 42 sec..
(new Handler()).postDelayed(new Runnable() {
public void run() {
// do you want
}
}, 42000);
if every sec you need to than do it..
This is using the Handler
Initialize..
Handler handler = new Handler();
Import
import android.os.Handler;
call this for..
handler.postDelayed(yourtask,42*1000);
private Runnable yourtask = new Runnable() {
public void run() {
// Run your code..
handler.removeCallbacks(yourtask);
handler.postDelayed(yourtask, 42*1000); // again start...
}
};
This is Using Timer every one second....
final Handler handler = new Handler();
Timer timer = new Timer();
private int DELAY = 1000 * 60 * 1;
call this
timer.scheduleAtFixedRate(doAsynchronousTask, 0, DELAY);
in class
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
// your code
}
});
}
};
Try to use CountDownTimer it works perfectly in UI thread. All you need is just to initiate it in your code like that:
timer = new CountDownTimer(MS_TILL_COMPLETE, TICK_OFFSET) {
#Override
public void onTick(long l) {
//Repeating every TICK_OFFSET
}
#Override
public void onFinish() {
//Called after MS_TILL_COMPLETE
}
}.start();
Instead of timer try to use CountDownTimer
new CountDownTimer(10000, 20000) {
#Override
public void onTick(long millisUntilFinished) {}
public void onFinish() {
}
}.start();
PROBLEM: The images change, but not in the appropriate time.
Hereby the CountDownTimer class reference:
CountDownTimer
Schedule a countdown until a time in the future, with regular notifications on intervals along the way.
Public Constructors
CountDownTimer(long millisInFuture, long countDownInterval)
millisInFuture The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
countDownInterval The interval along the way to receive onTick(long) callbacks.
Source: http://developer.android.com/reference/android/os/CountDownTimer.html
Assuming that I have the following two images:
And I want to change between them every 1 second,
what am I doing wrong here? Can someone give me a direction?
blinkingAlarm = new CountDownTimer(1000,1000) {
boolean switchImage = false;
#Override
public void onTick(long millisUntilFinished) {
if(!switchImage)
{
button1.setBackgroundResource(R.drawable.image1);
}
else if(switchImage)
{
button1.setBackgroundResource(R.drawable.image2);
}
// Flip
switchImage = (!switchImage);
}
#Override
public void onFinish() {
// Loop
blinkingAlarm.start();
}
};
The method is being called by
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
blinkingAlarm.start();
break;
}
}
Just use handler and postDelayed instead CountDownTimer.
In your class add this variables:
private boolean switchImage = false;
private Handler handler;
Then use this code:
handler = new Handler();
Runnable changeImage = new Runnable() {
#Override
public void run() {
if(!switchImage){
button1.setBackgroundResource(R.drawable.image1);
switchImage = true;
} else {
button1.setBackgroundResource(R.drawable.image2);
switchImage = false;
}
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(changeImage, 1000);
Here is a code which I want to repeat 50 times after every 3 seconds. if I am calling this function with 'for' loop or 'while' loop it is not working properly Please give me suggestion.
for (int i = 0; i < 50; i++) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Generate_Ballon();
}
}, delay);
}
You can use CountDownTimer
See Example,
new CountDownTimer(150000, 3000)
{
public void onTick(long millisUntilFinished)
{
// You can do your for loop work here
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Here onTick() method will get executed on every 3 seconds.
You should use Handler's postDelayed function for this purpose. It will run your code with specified delay on the main UI thread, so you will be able to update UI controls.
private int mInterval = 5000; // 5 seconds by default, can be changed later
private Handler mHandler;
#Override
protected void onCreate(Bundle bundle) {
...
mHandler = new Handler();
}
Runnable mStatusChecker = new Runnable() {
#Override
public void run() {
updateStatus(); //this function can change value of mInterval.
mHandler.postDelayed(mStatusChecker, mInterval);
}
};
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
private int count = 50;
private Handler handler = new Handler();
private Runnable r = new Runnable() {
public void run() {
Generate_Ballon();
if (--count > 0) {
handler.postDelayed(r, delay);
}
}
};
handler.postDelayed(r, delay);
I have used android view pager to display images and text now what i want is that smothly scrolling pages after few seconds and with animation?
Its works but not smothly viewpager pages scroll.
Timer call
mCountDownTimer = new MyCountDownTimer(4000, 1000);
mCountDownTimer.start();
.
private static MyCountDownTimer mCountDownTimer;
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
System.out.println("Time's up!");
startTimerWithAnim();
}
#Override
public void onTick(long millisUntilFinished) {
System.out.println("mill="+millisUntilFinished / 1000);
}
}
if the 'MyCountDownTimer' call then the 'startTimerWithAnim' method call
public static void startTimerWithAnim() {
if (mCountDownTimer != null)
mCountDownTimer.cancel();
mHandler = new Handler();
mRunnable = new Runnable() {
public void run() {
if (mMemberPagerAdapterList != null) {
if (mPagedGrid.getCurrentItem() == mMemberPagerAdapterList.getCount() - 1)
mPagedGrid.setCurrentItem(0, true);
else if (isFirstTimeCurrentItem) {
mPagedGrid.setCurrentItem(0, true);
isFirstTimeCurrentItem = false;
} else if (isFirstTimeCurrentItem == false)
mPagedGrid.setCurrentItem(mPagedGrid.getCurrentItem() + 1, true);
}
}
};
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
#Override
public void run() {
mHandler.post(mRunnable);
}
}, 100, 4000);
}
There you go!
class UpdateTimeTask extends TimerTask {
public void run() {
viewPager1.post(new Runnable() {
public void run() {
if(viewPager1.getCurrentItem()<i){
viewPager1.setCurrentItem(viewPager1.getCurrentItem()+1,true);
String abc = String.valueOf(viewPager1.getCurrentItem());
Log.i("timer_+",abc);
}
else
{
viewPager1.setCurrentItem(0,true);
}
}
});
}
}
Timer timer = new Timer();
timer.schedule(new UpdateTimeTask(), 2000, 4000);
function doSomething() {
$(document).scrollTop($(document).height());
}
setInterval(doSomething, 5000);
This will scroll to bottom of the page every 5 seconds. This will be useful, if you have auto load content when user scroll down (like Facebook).