How to create ViewPager with dotted indicator in Kotlin

In this android kotlin source code example, we are going to create ViewPager with dotted indicator in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to create ViewPager with dotted indicator in Kotlin

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewPager.ViewPagerActivity8">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/layout_dot"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/layout_dot"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.text.Html
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.text.HtmlCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bluapp.kotlinview.R


@Suppress("DEPRECATION")
class ViewPagerActivity8 : AppCompatActivity() {
    private var viewPager: ViewPager? = null
    private var layoutDot: LinearLayoutCompat? = null
    private lateinit  var dot: Array<AppCompatTextView?>
    private val int_items = 5


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager8)
        viewPager = findViewById(R.id.viewpager) as ViewPager
        layoutDot = findViewById(R.id.layout_dot) as LinearLayoutCompat
        viewPager!!.adapter = MyAdapter(supportFragmentManager)
        addIndicator(0)
        viewPager!!.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageSelected(position: Int) {
                addIndicator(position)
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }

     fun addIndicator(pagePosition : Int){
        dot = arrayOfNulls<AppCompatTextView>(int_items)
         layoutDot!!.removeAllViews()
        for(i in dot.indices){
            dot[i] = AppCompatTextView(this)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                dot[i]!!.text = Html.fromHtml("◉", HtmlCompat.FROM_HTML_MODE_LEGACY)
            }else{
                dot[i]!!.text = Html.fromHtml("◉")
            }
            dot[i]!!.textSize = 35f
            dot[i]!!.setTextColor(Color.GRAY)
            layoutDot!!.addView(dot[i])
        }
        dot[pagePosition]!!.setTextColor(Color.BLUE)
    }

    private inner class MyAdapter(fm: FragmentManager?) : FragmentPagerAdapter(fm!!, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

        override fun getItem(position: Int): Fragment {
            var fragment: Fragment? = null
            when (position) {
                0 -> fragment = ViewPagerFragment()
                1 -> fragment = ViewPagerFragment()
                2 -> fragment = ViewPagerFragment()
                3 -> fragment = ViewPagerFragment()
                4 -> fragment = ViewPagerFragment()
            }
            return fragment!!
        }

        override fun getCount(): Int {
            return int_items
        }
    }
}

fragment_view_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".ViewPager.ViewPagerFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ViewPagerFragment"
        android:textSize="20sp"
        android:textColor="@color/white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

ViewPagerFragment.kt

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bluapp.kotlinview.R

class ViewPagerFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_view_pager, container, false)
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.