Animate points using a view pager

I am working on a FragmentActivity that contains a ViewPager. ViewPager provides three fragments using the FragmentPagerAdapter. So that I can implement scroll screens using the viewpager. I can scroll through the pages and click the next button, I can also go to the next page / fragment. The following code works for me

1.WelcomeFragmentActivity.java

public class WelcomeFragmentActivity extends FragmentActivity { private List<Fragment> listFragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_activity_welcome); //FindViewByID final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); Button btnNext = (Button) findViewById(R.id.btnNext); //Initializing the List listFragments = new ArrayList<Fragment>(); //initializing the fragments WelcomeOneFragment welcomeOneFragment = new WelcomeOneFragment(); WelcomeTwoFragment welcomeTwoFragment = new WelcomeTwoFragment(); WelcomeThreeFragment welcomeThreeFragment = new WelcomeThreeFragment(); //Adding Fragments to List listFragments.add(welcomeOneFragment); listFragments.add(welcomeTwoFragment); listFragments.add(welcomeThreeFragment); //initializing PagerAdapterWelcome PagerAdapterWelcome pagerAdapterWelcome = new PagerAdapterWelcome(getSupportFragmentManager(), listFragments); viewPager.setAdapter(pagerAdapterWelcome); //On clicking next button move to next fragment btnNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("Current position", String.valueOf(viewPager.getCurrentItem())); viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); // If view pager is displaying the 3rd fragment ,move to WelcomeActivity if (viewPager.getCurrentItem() == 2) { Log.e("Curent position", String.valueOf(viewPager.getCurrentItem())); startActivity(new Intent(WelcomeFragmentActivity.this, WelcomeActivity.class)); } } }); } } 

2.PagerAdapterWelcome.java

 public class PagerAdapterWelcome extends FragmentPagerAdapter { private List<Fragment> listFragments; public PagerAdapterWelcome(FragmentManager fm, List<Fragment> listFragments) { super(fm); this.listFragments = listFragments; } @Override public Fragment getItem(int i) { return listFragments.get(i); } @Override public int getCount() { return listFragments.size(); } } 

I want to implement the following screens:

One

Two

enter image description here

These three screens will be displayed one after the other after scrolling or pressing the next button. Changing the color of the dot tells me which fragment I'm currently working on. Please tell me how can I give animation to these points?

Edited Code

I used RadioGroup to implement the concept. Consider the following code:

 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { position = viewPager.getCurrentItem(); Log.e("Position", String.valueOf(position)); if (position == 0) radioGroup.check(R.id.radioBtnOne); else if (position == 1) { radioGroup.check(R.id.radioBtnTwo); } else radioGroup.check(R.id.radioBtnThree); } @Override public void onPageScrollStateChanged(int state) { } }); 

It works to some extent, but I do not get the exact color mentioned in the design. Please check the following screenshot:

Screenshot

After adding some styles to radio biutton suggested by Ankit Aggrawal, I get the following:

enter image description here

+6
source share
4 answers

I achieved this using a radio group. Position this radio group just above your viewpager using the relative layout. Create a group of radio stations with the required number of points as switches. Do not press the radio buttons.

EDIT: Below is the full working code.

Create your layout like this

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:orientation="horizontal" android:id="@+id/radioGroup"> <RadioButton android:id="@+id/radioBtnOne" android:layout_width="15dp" android:layout_height="15dp" android:background="@drawable/button_selector" android:button="@null"/> <RadioButton android:id="@+id/radioBtnTwo" android:layout_width="15dp" android:layout_height="15dp" android:background="@drawable/button_selector" android:button="@null"/> <RadioButton android:id="@+id/radioBtnThree" android:layout_width="15dp" android:layout_height="15dp" android:background="@drawable/button_selector" android:button="@null"/> </RadioGroup> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 

Now in the viewpager put onPageChangeListener

 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { radioGroup.check(radioGroup.getChildAt(position).getId()); } @Override public void onPageScrollStateChanged(int state) { } }); 

Below is the selector for the toggle button_selector.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/toggle_button_selected"/> <item android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/toggle_button_unselected"/> <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/toggle_button_selected"/> <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/toggle_button_unselected"/> </selector> 

Now for the selected button, create this toggle_button_selected_drawable.xml file

 <?xml version="1.0" encoding="utf-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="10dp" /> <solid android:color="@color/your_selection_color" /> </shape> 

Similarly for an unselected button, create this toggle_button_unselected_drawable.xml file

 <?xml version="1.0" encoding="utf-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="10dp" /> <solid android:color="@color/grey" /> </shape> 
+8
source

This library can help you .... https://github.com/PaoloRotolo/AppIntro/

+1
source

You can use the Jake Wharton ViewPagerIndicator to add captions or simple circles for each tab in the ViewPager.

Jake Wharton provided a bunch of GitHub sample code, you can also refer to the Jake Wharton website.

enter image description here

0
source

Use the ViewPagerCircleIndicator library. http://viewpagerindicator.com/

0
source

All Articles