On my login activity I would like to have a banner (3 actually) which would switch by certain time. Just like news on a website. With an arrow to pass to the next one, but mainly that passes automatically.
An example of what I want, these images on Tinder login screen switch when I swipe or just wait:
How do I do something like this on Android? Is there a way to do it natively? or at least is there a external resource for that?
Try this code,
public class TestActivity extends AppCompatActivity {
int images[] = {R.drawable.ic_earth, R.drawable.oval_btn,
R.drawable.ic_group_black_24dp, R.drawable.ic_earth};
ImagePagerAdapter myAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tests);
ViewPager viewPager = findViewById(R.id.viewPager);
myAdapter = new ImagePagerAdapter(TestActivity.this, images);
viewPager.setAdapter(myCustomPagerAdapter);
setupViewPager(viewPager);
}
int currentPage = 0;
Timer timer;
final long DELAY_MS = 500;
final long PERIOD_MS = 3000;
int NUM_PAGES = 4;
private void setupViewPager(final ViewPager viewPager) {
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
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);
}
public class ImagePagerAdapter extends PagerAdapter {
Context context;
int images[];
LayoutInflater layoutInflater;
public ImagePagerAdapter(Context context, int images[]) {
this.context = context;
this.images = images;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = layoutInflater.inflate(R.layout.item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(images[position]);
container.addView(itemView);
//listening to image click
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
}
Related
How would I turn my swipe-able ViewPager into a auto looping slideshow? I'v read other post referencing to use a Handler or Timer but I'm not sure which one would be best. Iv posted my code below.
Update:
After following some old posts and comments Iv been trying to initiate a timer to loop through the ViewPager slides. The issue is now the Timer is not working, also not getting any errors either. Any thoughts? Iv added the Timer code to the MainActivity code below.
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ViewPager viewPager;
customSwipeAdapter swipeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewPager);
swipeAdapter = new customSwipeAdapter(this);
viewPager.setAdapter(swipeAdapter);
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
int currentPage = 0;
int NUM_PAGES = 3;
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
});
}
}, 500, 3000);
}
}
CustomSwipeAdapter.java
public class customSwipeAdapter extends PagerAdapter {
private int [] image_resources = {R.drawable.SlideOne,R.drawable.SlideTwo,R.drawable.SlideThr,R.drawable.SlideFour};
private Context context;
private LayoutInflater layoutInflater;
public customSwipeAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return image_resources.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == (LinearLayout)object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
ImageView imageView = (ImageView) item_view.findViewById(R.id.imageView);
imageView.setImageResource(image_resources[position]);
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((LinearLayout)object);
}
}
In my app I want an image slider.I already do that with view pager but I want image change automatically in every 2seconds. I am unable to do that.
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about, container,false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
adapter = new CustomSwipeAdapter(this.getActivity());
viewPager.setAdapter(adapter);
indicator = (CircleIndicator) rootView.findViewById(R.id.indicator);
indicator.setViewPager(viewPager);
return rootView;
}
this is my fragment class.
public class CustomSwipeAdapter extends PagerAdapter {
private int[] image_resource = {R.drawable.sample_1, R.drawable.sample_2};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context ctx) {
this.ctx = ctx;
}
#Override
public int getCount() {
return image_resource.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return (view == object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.image_swipe_layout, container, false);
ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
imageview.setImageResource(image_resource[position]);
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
this is my adapter class.I want that image change automatically.Please help me.Thanks.
A code I had written a really long time ago which does something similar, maybe this can help you.
final Handler handler = new Handler();
final Runnable update = () -> {
if (currentPage == list.size()) {
currentPage = 0;
}
pager.setCurrentItem(currentPage++, true);
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(update);
}
}, 2500, 2500);
#Override
protected void onDestroy() {
super.onDestroy();
timer.cancel();
}
So you set a timer which gets called after a certain interval of time (~2.5 seconds here) and when it does, you check if the current page of the adapter has reached the end then reset it back to 0 else increment it.
I'm trying to replace colors by images from Drawable in this code.
I found this tutorial on how we can make an auto images slider by using viewpager on Android Studio, but this code is to switch colors images how can I replace this colors by images?
Here is my SliderAdapter.java
public class SliderAdapter extends PagerAdapter {
private Context context;
private List<Integer> color;
private List<String> colorName;
public SliderAdapter(Context context, List<Integer> color, List<String> colorName) {
this.context = context;
this.color = color;
this.colorName = colorName;
}
#Override
public int getCount() {
return color.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_slider, null);
TextView textView = (TextView) view.findViewById(R.id.textView);
LinearLayout linearLayout = (LinearLayout)
view.findViewById(R.id.linearLayout);
textView.setText(colorName.get(position));
linearLayout.setBackgroundColor(color.get(position));
ViewPager viewPager = (ViewPager) container;
viewPager.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager viewPager = (ViewPager) container;
View view = (View) object;
viewPager.removeView(view);
}
}
here is my MainActivity
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
TabLayout indicator;
List<Integer> color;
List<String> colorName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
viewPager=(ViewPager)findViewById(R.layout.viewPager);
indicator=(TabLayout)findViewById(R.layout.indicator);
color = new ArrayList<>();
color.add(Color.RED);
color.add(Color.GREEN);
color.add(Color.BLUE);
colorName = new ArrayList<>();
colorName.add("RED");
colorName.add("GREEN");
colorName.add("BLUE");
viewPager.setAdapter(new SliderAdapterDemo(this, color, colorName));
indicator.setupWithViewPager(viewPager, true);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new SliderTimer(), 4000, 6000);
}
private class SliderTimer extends TimerTask {
#Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if (viewPager.getCurrentItem() < color.size() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
} else {
viewPager.setCurrentItem(0);
}
}
});
}
}
}
I have image slider in android studio, but images array from inside project.
I want to use images from JSON, how to the best solution..
this is my adapter
public class adapter_image_slider extends PagerAdapter {
Context mContext;
public adapter_image_slider(Context context) {
this.mContext = context;
}
#Override
public int getCount() {
return sliderImagesId.length;
}
private int[] sliderImagesId = new int[]{
R.drawable.imghome1, R.drawable.imghome2, R.drawable.imghome3,
};
#Override
public boolean isViewFromObject(View v, Object obj) {
return v == ((ImageView) obj);
}
#Override
public Object instantiateItem(ViewGroup container, int i) {
ImageView mImageView = new ImageView(mContext);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setImageResource(sliderImagesId[i]);
((ViewPager) container).addView(mImageView, 0);
return mImageView;
}
#Override
public void destroyItem(ViewGroup container, int i, Object obj) {
((ViewPager) container).removeView((ImageView) obj);
}
}
and this is my main activity :
public class activity_img_slider_json extends Fragment {
private static ViewPager mPager;
private static int currentPage = 0;
private static final Integer[] XMEN= {R.drawable.imghome1,R.drawable.imghome2,R.drawable.imghome3,};
private ArrayList<Integer> XMENArray = new ArrayList<Integer>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_img_slider_json,null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
super.onCreate(savedInstanceState);
init();
}
private void init() {
for(int i=0;i<XMEN.length;i++)
XMENArray.add(XMEN[i]);
mPager = (ViewPager)getActivity().findViewById(R.id.pager);
mPager.setAdapter(new adapter_img_slider_json(getActivity(), XMENArray));
CircleIndicator indicator = (CircleIndicator)getActivity().findViewById(R.id.indicator);
indicator.setViewPager(mPager);
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == XMEN.length) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
}
}
Thanks For Attention.
I am trying to implement the automatic image slide using View Pager. I used handler to slide the images one after another for certain interval (like 10 seconds). This is my code please tell me where i am doing wrong.
public class MainActivity extends Activity {
ViewPager mViewPager;
CustomPagerAdapter mCustomPagerAdapter;
private final Handler handler = new Handler();
static int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCustomPagerAdapter = new CustomPagerAdapter(this);
mViewPager = (ViewPager) findViewById(R.id.pager);
Runnable runnable = new Runnable() {
#Override
public void run() {
for (int i = 0; i < mCustomPagerAdapter.getCount()-1; i++) {
final int value = i;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
mViewPager.setCurrentItem(value, true);
mViewPager.setAdapter(mCustomPagerAdapter);
}
});
}
}
};
new Thread(runnable).start();
// mViewPager.setAdapter(mCustomPagerAdapter);
}
}
class CustomPagerAdapter extends PagerAdapter{
Context mContext;
LayoutInflater mLayoutInflater;
int[] mResources = {
R.drawable.splash0,
R.drawable.splash1,
R.drawable.splash2,
R.drawable.splash3,
R.drawable.splash0,
R.drawable.splash2
};
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mResources.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
Try this it works great
mIntroPagerAdapter = new mIntroPagerAdapter(this);
mIntroViewPager = (ViewPager) findViewById(R.id.pager);
mIntroViewPager.setAdapter(mIntroPagerAdapter );
static int i=0;
private final Handler handler = new Handler();
void setImageMove()
{
handler.post(ViewPagerVisibleScroll);
}
Runnable ViewPagerVisibleScroll= new Runnable() {
#Override
public void run() {
if(i <= mIntroPagerAdapter.getCount()-1)
{
mIntroViewPager.setCurrentItem(i, true);
handler.postDelayed( ViewPagerVisibleScroll, 10000);
i++;
}
}
};
Call this method pageSwitcher
int page = 0;
pageSwitcher(10); 10 Seconds
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
}
class RemindTask extends TimerTask
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
if (page > mResources .size()-1)
{
page=0;
}
else
{
viewPager.setCurrentItem(page++,true);
}
}
});
}
}
}
new Timer().schedule(new TimerTask() {
#Override
public void run() {
if (index < mCustomPagerAdapter.getCount()) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mViewPager.setCurrentItem(index, true);
index++;
}
});
} else {
this.cancel();
}
}
}, 10000, 10000);
There is an Autoscroll ViewPager library.
If you need just sliding images then AndroidImageSlider might be more convenient. I've used it and it does the job perfectly.
Actually, you can get the idea how to implement AutoScrollViewPager:scrollOnce() sliding from those libraries.