Image Slider using ViewPager in Sketchware

 To add a front screen with sliding images using ViewPager in Sketchware, follow the instructions given below.



1. Create a new project in Sketchware.

2. Switch on AppCompat and design.

3. In main.xml add a  linear1 and linear2. Set padding of both these linears to 0. Set gravity of linear2 to center_horizontal. We will add ViewPager to linear1, and TabLayout to linear2.

4. Add four images: store, home, gift, and fast_food.

5. Create a CustomView custom2.xml. In this View, add an ImageView with scale type fit_center.

6. Create a more block extra. Add following codes in it.
}
androidx.viewpager.widget.ViewPager viewPager1;
int[] image_list = { R.drawable.store, R.drawable.home, R.drawable.gift, R.drawable.fast_food };


private class MyPagerAdapter extends androidx.viewpager.widget.PagerAdapter {

public int getCount() {
return image_list.length;
}

public Object instantiateItem(ViewGroup collection, int position) {
View view = getLayoutInflater().inflate(R.layout.custom2, null);
((androidx.viewpager.widget.ViewPager)collection).addView(view, 0);
final ImageView imageview1 = view.findViewById(R.id.imageview1);
imageview1.setImageResource(image_list[position]);
return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((androidx.viewpager.widget.ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
{


7. Add following codes in onCreate.

viewPager1 = new androidx.viewpager.widget.ViewPager(this);

viewPager1.setAdapter(new MyPagerAdapter());

viewPager1.setCurrentItem(0);

linear1.addView(viewPager1);


com.google.android.material.tabs.TabLayout tabLayout = new com.google.android.material.tabs.TabLayout(this);

tabLayout.setTabMode(com.google.android.material.tabs.TabLayout.MODE_FIXED);

tabLayout.setupWithViewPager(viewPager1);

linear2.addView(tabLayout);

tabLayout.setSelectedTabIndicator(null);

tabLayout.getTabAt(0).setText("●");

tabLayout.getTabAt(1).setText("●");

tabLayout.getTabAt(2).setText("●");

tabLayout.getTabAt(3).setText("●");


8. Run the project.

Comments

  1. I want to ask if it is possible to jump to linear 1 to liner 2 using timer in Main Activity? For example in the Main Page Activity I add two linears:
    In Linear 1, I add image for splascreen
    In Linear 2, I add WebView
    I want to make spashscreen in the Main page, when open the android app Linear 1 will appear then gone and Linear 2 will appear.
    Thank you. Sorry for my poor English.

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware