Auto Slider with Indicator in Android

Shaktisinh Jadeja
Techsuzu
Published in
3 min readAug 4, 2017

In this blog we are going to lean how to create a Auto slider with indicator.

Step 1 : Start your android studio and create new project with blank activity.

Step 2 : Open your build.gradle file and add design support library

compile 'com.android.support:design:26.+'

Step 3: create indicator drawables
default_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false">
<solid android:color="@android:color/darker_gray" />
</shape>
</item>
</layer-list>

selected_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false">
<solid android:color="@color/colorAccent"/>
</shape>
</item>
</layer-list>

indicator_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/selected_indicator"
android:state_selected="true"/>

<item android:drawable="@drawable/default_indicator"/>
</selector>

Step 4: Write following code in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


<android.support.design.widget.TabLayout
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
app:tabBackground="@drawable/indicator_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
/>
</RelativeLayout>

Step 5: Now create PagerAdapter named 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);

}
}

Step 6: create layout to display slides item_slider.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/linearLayout"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20sp" />

</LinearLayout>

Step 7: Now create inner class TimerTask inside your MainActivity.java named SliderTimer and schedule it in Timer class

private class SliderTimer extends TimerTask {

@Override
public void run() {
SliderActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (viewPager.getCurrentItem() < color.size() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
} else {
viewPager.setCurrentItem(0);
}
}
});
}
}

Write following lines in onCreate method of MainActivity.java

Timer timer = new Timer();
timer.scheduleAtFixedRate(new SliderTimer(), 4000, 6000)

Final MainActivity.java

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.id.viewPager);
indicator=(TabLayout)findViewById(R.id.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);
}
}
});
}
}
}

Download full project from below link

If you like this article please like this and subscribe it. If you have any doubt feel free to ask on comment. Follow me on twitter. Your appreciation means lot to me. Thank you.

--

--

Shaktisinh Jadeja
Techsuzu

PM | PgM | TPM | DevOps | Android | ગુજરાતી