Styleable Seek Bar

illustrations illustrations illustrations illustrations illustrations illustrations illustrations

Styleable Seek Bar

GitHub


Published on May 17, 2020 by Man Ho on Android Development

An seekbar allows creating many types of shape with many options.

Table of contents

Samples

Gradle

Step 1. Add this in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://www.jitpack.io' }
    }
}

Step 2. Add dependency

dependencies {
    ...
    implementation 'com.github.homanad:StyleableSeekBar:Tag'
}

Style

StyleValue
CIRCULAR0
ROUNDED_RECTANGLE1
DIAMOND2
RECTANGLE3
TRIANGLE4
HEXAGON5

Orientation

OrientationValue
HORIZONTAL0
VERTICAL1

Attributes

For ease of use, you can also set all values in the corresponding code as follows:

AttributeXMLDescriptionTypeDefault value
dotCountsb_dotCountThe number of dots equals 100% of the SeekbarInt10
stylesb_styleThe shape of each dotStyleCIRCULAR
roundedRadiussb_rounded_radiusThis attribute is used exclusively for STYLE RECTANGLE, which is the rounded value of dotDimension10dp
activeColorsb_activeColorThe color is shown for active dotsColorIntBlue
inactiveColorsb_inactiveColorThe color is shown for inactive dotsColorIntGray
minimumValuesb_minimumValueThe lowest number of dots that we must activateIntstroke
startColorsb_progress_startColorFor gradient progress, this is the starting color, combine with sb_progress_endColor to form a gradient rangeColorIntNothing
endColorsb_progress_endColorFor gradient progress, this is the starting color, combine with sb_progress_startColor to form a gradient rangeColorIntNothing
dotMarginsb_dotMarginThe distance between dotsDimension10dp
selectedBorderColorsb_selected_borderColorCreate a border for the active dot with this colorColorIntNothing
selectedBorderWidthsb_selected_borderWidthCreate a border for the active dot with this thicknessDimension3dp
orientationsb_orientationThe direction of seek barOrientationHORIZONTAL

Usage

XML layout

<com.homanad.android.widget.seekarc.StyleableSeekBar
    android:layout_width="100dp"
    android:layout_height="450dp"
    app:sb_activeColor="#239D28"
    app:sb_dotCount="10"
    app:sb_dotMargin="10dp"
    app:sb_inactiveColor="#545151"
    app:sb_minimumValue="1"
    app:sb_style="circular" />

Programmatically

For ease of use, you can also set all values in the corresponding code as follows:

seekBar.dotCount = 10
seekBar.style = StyleableSeekBar.HEXAGON
seekBar.roundedRadius = 10.dp
seekBar.activeColor = Color.BLUE
seekBar.inactiveColor = Color.GRAY
seekBar.minimumValue = 1
seekBar.startColor = Color.CYAN
seekBar.endColor = Color.YELLOW
seekBar.dotMargin = 5.dp
seekBar.selectedBorderColor = Color.RED
seekBar.selectedBorderWidth = Color.parseColor("#64A4D6")
seekBar.orientation = StyleableSeekBar.VERTICAL

Here, notice the values written with “.dp” extension

Because these attributes are dimension values, so for an Int or Float value become a dp value, I’ve provided two extension functions to convert the Int or Float type to dp, you just need to call it as an extension function.

License

Copyright 2021 Man Ho

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.