Last active
March 11, 2016 03:55
-
-
Save dleonett/8c510eb6aeba1ffba38f to your computer and use it in GitHub Desktop.
Circle ViewPager indicators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/pager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<LinearLayout | |
android:id="@+id/circles" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="24dp" | |
android:layout_gravity="center|bottom" | |
android:gravity="center_vertical" | |
android:orientation="horizontal"> | |
</LinearLayout> | |
</FrameLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shape="oval"> | |
<solid android:color="#49ffffff" /> | |
<size android:width="8dp" android:height="8dp"/> | |
</shape> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shape="oval"> | |
<solid android:color="#ffffffff" /> | |
<size android:width="10dp" android:height="10dp"/> | |
</shape> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
// ... | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// ... | |
mPager.setAdapter(mAdapter); | |
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
} | |
@Override | |
public void onPageSelected(int position) { | |
setIndicator(position); | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
} | |
}); | |
buildCircles(); | |
// ... | |
} | |
private void buildCircles(){ | |
float scale = getResources().getDisplayMetrics().density; | |
int padding = (int) (5 * scale + 0.5f); | |
for (int i = 0 ; i < mAdapter.getCount() ; i++) { | |
ImageView circle = new ImageView(this); | |
circle.setImageResource(R.drawable.circle); | |
circle.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); | |
circle.setAdjustViewBounds(true); | |
circle.setPadding(padding, 0, padding, 0); | |
circles.addView(circle); | |
} | |
setIndicator(0); | |
} | |
private void setIndicator(int index){ | |
if (index < mAdapter.getCount()) { | |
for (int i = 0 ; i < mAdapter.getCount() ; i++) { | |
ImageView circle = (ImageView) circles.getChildAt(i); | |
if (i == index) { | |
circle.setImageResource(R.drawable.circle_selected); | |
} else { | |
circle.setImageResource(R.drawable.circle); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment