How to switch automatically between viewPager pages
You can use Timer
for this purpose. The following code is self explanatory:
// ---------------------------------------------------------------------------
Timer timer;
int page = 1;
public void pageSwitcher(int seconds) {
timer = new Timer(); // At this line a new Thread will be created
timer.scheduleAtFixedRate(new RemindTask(), 0, seconds * 1000); // delay
// in
// milliseconds
}
// this is an inner class...
class RemindTask extends TimerTask {
@Override
public void run() {
// As the TimerTask run on a seprate thread from UI thread we have
// to call runOnUiThread to do work on UI thread.
runOnUiThread(new Runnable() {
public void run() {
if (page > 4) { // In my case the number of pages are 5
timer.cancel();
// Showing a toast for just testing purpose
Toast.makeText(getApplicationContext(), "Timer stoped",
Toast.LENGTH_LONG).show();
} else {
mViewPager.setCurrentItem(page++);
}
}
});
}
}
// ---------------------------------------------------------------------------
Note 1: Make sure that you call pageSwitcher
method after setting up adapter
to the viewPager
properly inside onCreate
method of your activity.
Note 2: The viewPager
will swipe every time you launch it. You have to handle it so that it swipes through all pages only once (when the user is viewing the viewPager
first time)
Note 3: If you further want to slow the scrolling speed of the viewPager
, you can follow this answer on StackOverflow.
Tell me in the comments if that could not help you...
Android ViewPager automatically change page
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);
How to set viewpager to change page automatically after 5 sec
Well, android does not provide any function to automatically flip views inside ViewPager but it provides you with something called TimerTask class which you can use to achieve your goal.
You can look at this link to do it this way.
There is one more easy way to achieve this but for that you will have to drop the idea of using ViewPager. Instead you can use ViewFlipper.
ViewFlipper has its own methods to flip automatically. You just need to add these two lines to make your views automatically flippable:
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(2000); // flip every 2 seconds (2000ms)
You can get ViewFlipper example here.
Go to next page in ViewPager
In your fragment write one interface like:
public class PlaceholderFragment extends Fragment{
private OnButtonClickListener mOnButtonClickListener;
interface OnButtonClickListener{
void onButtonClicked(View view);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mOnButtonClickListener = (OnButtonClickListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(((Activity) context).getLocalClassName()
+ " must implement OnButtonClickListener");
}
}
yourButtons.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnButtonClickListener.onButtonClicked(v);
}
});
}
And in your mainactivity:
class MainActivity extends AppCompatActivity implements OnButtonClickListener{
@Override
void onButtonClicked(View view){
int currPos=yourPager.getCurrentItem();
switch(view.getId()){
case R.id.leftNavigation:
//handle currPos is zero
yourPager.setCurrentItem(currPos-1);
break;
case R.id.rightNavigation:
//handle currPos is reached last item
yourPager.setCurrentItem(currPos+1);
break;
}
}
}
How to change page with button in viewPager?
The DialogFragmentContainer
is the parent fragment of the ViewPager page fragments; i.e. it's the parent of Step1 through Step4 fragments.
And therefore you can access it from Step1
fragment, the fragment that you want to change the current ViewPager page using the parentFragment
attribute:
binding.btnNext.setOnClickListener{
//I want to change fragment here
(parentFragment as DialogFragmentContainer).binding.pager.currentItem = myItemPosition
}
How to swipe ViewPager images automatically using TimeTask
Your question is already answered here
Add this in your MainActivity.java
//...
int currentPage = 0;
Timer timer;
final long DELAY_MS = 500;//delay in milliseconds before task is to be executed
final long PERIOD_MS = 3000; // time in milliseconds between successive task executions.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new CustomAdapter(MainActivity.this,imageId,imagesName);
viewPager.setAdapter(adapter);
/*After setting the adapter use the timer */
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES-1) {
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);
}
how to change the image automatically in view pager when it reaches the last image it should come to the first image automatically
try this add addOnPageChangeListener
to your viewPager
like this
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == strImages.length-1) {
currentPage = 0;
}
intro_images.setCurrentItem(currentPage++, true);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
or for auto change page try this create a time task like this
Timer timer;
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 0, 3000); // delay*/
private class RemindTask extends TimerTask {
int current = viewPager.getCurrentItem();
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (current < strImages.size()) {
viewPager.setCurrentItem(current);
current += 1;
} else {
current = 0;
viewPager.setCurrentItem(current);
}
}
});
}
}
Related Topics
Android - Taking Photos and Saving Them with a Custom Name to a Custom Destination via Intent
Cli on Dalvikvm Fails on Jni Lib
Android Access to Remote SQL Database
Writing/Reading Files To/From Android Phone's Internal Memory
Android Draw Route on a Mapview with Twoo Poi-S
Android Material Chip Component Crashing App. Unable to Inflate Xml
Android - What Is the Meaning of Stableids
How to Load a Image from Assets
Get Bluetooth Local MAC Address in Marshmallow
How to Prevent Multiple Toast Overlaps
How to Display Upside Down Text with a Textview in Android
Android - Navigation View Item Menu Background Color
Get Last Inserted Value from SQLite Database Android
Set Loadurltimeoutvalue on Webview
See Android Recent Task Executed by the User
Room - Livedata Observer Does Not Trigger When Database Is Updated