Android Create Stepper Dot

Android Create Stepper Dot


1. Project Structure


2. Gradle

plugins {
  id 'com.android.application'
}

android {
  compileSdkVersion 30
  buildToolsVersion "30.0.3"

  defaultConfig {
    applicationId "com.stepper.dot"
    minSdkVersion 19
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  implementation 'com.balysv:material-ripple:1.0.2'
  implementation 'androidx.appcompat:appcompat:1.3.0'
  implementation 'com.google.android.material:material:1.4.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
  implementation 'com.crystal:crystalrangeseekbar:1.1.3'
  testImplementation 'junit:junit:4.+'
  androidTestImplementation 'androidx.test.ext:junit:1.1.3'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}


3. Main Activity

package com.stepper.dot;

import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.stepper.dot.utils.Tools;
import com.stepper.dot.utils.ViewAnimation;

public class MainActivity extends AppCompatActivity {

  private static final int MAX_STEP = 6;
  private int current_step = 1;
  private TextView status;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stepper_dots);

    initToolbar();
    initComponent();
  }

  private void initToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_menu);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Dots");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Tools.setSystemBarColor(this);
  }

  private void initComponent() {
    status = (TextView) findViewById(R.id.status);

    ((LinearLayout) findViewById(R.id.lyt_back)).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        backStep(current_step);
        bottomProgressDots(current_step);
      }
    });

    ((LinearLayout) findViewById(R.id.lyt_next)).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        nextStep(current_step);
        bottomProgressDots(current_step);
      }
    });

    String str_progress = String.format(getString(R.string.step_of), current_step, MAX_STEP);
    status.setText(str_progress);
    bottomProgressDots(current_step);
  }

  private void nextStep(int progress) {
    if (progress < MAX_STEP) {
      progress++;
      current_step = progress;
      ViewAnimation.fadeOutIn(status);
    }
    String str_progress = String.format(getString(R.string.step_of), current_step, MAX_STEP);
    status.setText(str_progress);
  }

  private void backStep(int progress) {
    if (progress > 1) {
      progress--;
      current_step = progress;
      ViewAnimation.fadeOutIn(status);
    }
    String str_progress = String.format(getString(R.string.step_of), current_step, MAX_STEP);
    status.setText(str_progress);
  }

  private void bottomProgressDots(int current_index) {
    current_index--;
    LinearLayout dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
    ImageView[] dots = new ImageView[MAX_STEP];

    dotsLayout.removeAllViews();
    for (int i = 0; i < dots.length; i++) {
      dots[i] = new ImageView(this);
      int width_height = 15;
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(width_height, width_height));
      params.setMargins(10, 10, 10, 10);
      dots[i].setLayoutParams(params);
      dots[i].setImageResource(R.drawable.shape_circle);
      dots[i].setColorFilter(getResources().getColor(R.color.grey_20), PorterDuff.Mode.SRC_IN);
      dotsLayout.addView(dots[i]);
    }

    if (dots.length > 0) {
      dots[current_index].setImageResource(R.drawable.shape_circle);
      dots[current_index].setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search_setting, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
    } else {
      Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
  }
}


4. Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@color/grey_20"
  android:orientation="vertical">

  <com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include layout="@layout/toolbar" />

  </com.google.android.material.appbar.AppBarLayout>

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
      android:id="@+id/status"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:paddingBottom="?android:attr/actionBarSize"
      android:text="Step 1 of 5"
      android:textAppearance="@style/TextAppearance.AppCompat.Display1"
      android:textColor="@color/grey_60"
      android:textStyle="bold" />

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="?android:attr/actionBarSize"
      android:layout_alignParentBottom="true"
      android:background="@color/grey_3"
      android:orientation="horizontal">

      <LinearLayout
        android:id="@+id/lyt_back"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="@dimen/spacing_middle">

        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:tint="@color/grey_60"
          app:srcCompat="@drawable/ic_chevron_left" />

        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/BACK"
          android:textAppearance="@style/Base.TextAppearance.AppCompat.Subhead"
          android:textColor="@color/grey_60"
          android:textStyle="bold" />

      </LinearLayout>

      <LinearLayout
        android:id="@+id/layoutDots"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" />

      <LinearLayout
        android:id="@+id/lyt_next"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="@dimen/spacing_middle">

        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/NEXT"
          android:textAppearance="@style/Base.TextAppearance.AppCompat.Subhead"
          android:textColor="@color/grey_60"
          android:textStyle="bold" />

        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:tint="@color/grey_60"
          app:srcCompat="@drawable/ic_chevron_right" />

      </LinearLayout>

    </LinearLayout>

  </RelativeLayout>

</LinearLayout>


5. Value

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:ns1="urn:oasis:names:tc:xliff:document:1.2">

  <style name="TextAppearance.AppCompat.Display1" parent="Base.TextAppearance.AppCompat.Display1"/>
  <style name="Base.TextAppearance.AppCompat.Subhead">
    <item name="android:textSize">@dimen/abc_text_size_subhead_material</item>
    <item name="android:textColor">?android:textColorPrimary</item>
  </style>

  <color name="grey_3">#f7f7f7</color>
  <color name="grey_5">#f2f2f2</color>
  <color name="grey_10">#e6e6e6</color>
  <color name="grey_20">#cccccc</color>
  <color name="grey_40">#999999</color>
  <color name="grey_60">#666666</color>
  <color name="grey_80">#37474F</color>
  <color name="grey_90">#263238</color>
  <color name="grey_95">#1a1a1a</color>
  <color name="grey_100_">#0d0d0d</color>

</resources>