Building a vertical SeekBar from Scratch in Java for Android: Ultimate Guide.

Sahil Patel
6 min readAug 4, 2023

--

Introduction

SeekBars are a powerful tool in Android app development for allowing users to adjust values along a horizontal bar. However, there are times when a vertical orientation is more appropriate for the design or functionality of an app. This is where a custom vertical SeekBar comes in handy.

In this topic, we will look at how to create a custom vertical SeekBar in Java, allowing developers to provide users with a simple and interactive way to control settings and values vertically. By the end of this guide, you’ll have the knowledge and skills to incorporate this customized element into your Android apps, improving user experience and making your app stand out with a unique and user-friendly interface. Let’s get started on making a custom vertical SeekBar in Java!

Why Use a Custom Class?

Designed Functionality: When you create a custom SeekBar class, you have complete control over how it is designed to meet the specific needs of your app. You can implement only the features you require, improving performance and reducing code bloat.

Reduced External Dependencies: Using a lot of third-party libraries or dependencies can make your app more complicated and difficult to maintain. By creating a custom class, you reduce your reliance on external code, lowering the risk of compatibility issues and ensuring that changes or updates to third-party libraries do not affect your app.

Full Control and Customization: You have complete authority over the appearance, actions, and interactions of a custom SeekBar. You may change its design, animation, and other features to precisely match the user experience and branding of your app.

Efficient Codebase Although third-party libraries might be quite useful, they might also contain features that you don’t require, increasing the size of the APK. Your codebase can be optimized with the help of a custom class, making your program more streamlined and effective.

Easier Debugging and Troubleshooting: When you create your custom SeekBar, you have a deeper understanding of its inner workings. This familiarity makes it easier to debug issues and troubleshoot problems, enhancing the overall development and maintenance process.

Future-Proofing: By building your own custom class, you can adapt it as your app evolves and new requirements emerge. You won’t be limited by the functionality of third-party libraries, ensuring that your app remains flexible and up-to-date.

Learning Experience: Developers have a great opportunity to improve their abilities and grasp Android app development more deeply by building a custom class(SeekBar). Insights into the particulars of UI design and event management are provided, promoting both individual and professional development.

Creating the CustomVerticalSeekBar Class

Step 1: Create a new Android Studio Project Start by opening Android Studio and creating a new project. Choose an appropriate project name, package name, and other settings as per your requirements.

Step 2: Define the Custom SeekBar class In your project’s Java package (usually under app > java > your.package.name), create a new Java class for your custom SeekBar. Let's call it CustomSeekBar.

public class CustomVerticalSeekBar extends SeekBar {
// write code here
}

Setp 3: Discuss the initView() method for initializing the SeekBar progress drawable and thumb.

The initView() method is a helpful function that you created within the CustomSeekBar class to simplify the process of initializing the SeekBar's progress drawable and thumb. This method is called automatically whenever you create an instance of the CustomSeekBar class using any of its constructors.

private void initView(Context context) {
// Set Custom Progress Bar Drawable
setProgressDrawable(ContextCompat.getDrawable(context, R.drawable.vertical_progress_selector));
// Set Custom Thumb
setThumb(ContextCompat.getDrawable(context, R.drawable.thumb));

}

what the initView() method does :

1. Customizing the Progress Drawable: The progress drawable is the visual representation of the progress made on the SeekBar. By default, it’s a simple horizontal line, but with the initView() method, you can easily change it to your desired appearance. For instance, you can make it colorful, add gradients, or even use custom images as the progress indicator.

2. Customizing the Thumb: The thumb is the draggable handle on the SeekBar that indicates the current progress. With the initView() method, you can customize the thumb's appearance. For example, you can change its color, shape, or use a custom image to make it more visually appealing and match the theme of your app.

Setp 4: Implementing Custom Methods and Callbacks

1. setSeekBarThumb():

The setSeekBarThumb() method is a custom method that you can add to your CustomSeekBar class to allow users to set a custom drawable or image as the thumb (handle) of the SeekBar. By default, the SeekBar comes with a standard thumb, but with this method, you provide users with the flexibility to choose their own thumb representation.

public void setSeekBarThumb(Context context, Drawable thumb) {
super.setThumb(thumb);

if (thumb != null) {
if (thumb != null) {
int thumbWidth = thumb.getIntrinsicWidth();
int thumbHeight = thumb.getIntrinsicHeight();
thumb.setBounds(0, 0, thumbWidth, thumbHeight);
setThumbOffset(1);
}
}

}

2. onTouchEvent() :

The onTouchEvent() method is an important method that you can override in your CustomSeekBar class to handle touch events on the SeekBar. When a user interacts with the SeekBar by touching and dragging the thumb, this method gets called, allowing you to respond to the touch events and update the progress accordingly.

@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
// here get value of progress
setProgress(getMax() - (getMax() * (int) event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}

In this example, when the user touches and drags the SeekBar’s thumb (ACTION_DOWN and ACTION_MOVE), we calculate the progress based on the touch position and set it using `setProgress()`. We also call the `onProgressChanged()` method of the `OnVerticalSeekBarChangeListener` (explained below) to notify any registered listeners about the progress change.

public class CustomSeekBar extends SeekBar {

private OnVerticalSeekBarChangeListener onVerticalSeekBarChangeListener;

public void setOnVerticalSeekBarChangeListener(OnVerticalSeekBarChangeListener listener) {
this.onVerticalSeekBarChangeListener = listener;
}
}

public interface OnVerticalSeekBarChangeListener {

void onStopTrackingTouch(CustomVerticalSeekBar seekBar, float volume);
}

Now, other parts of your application can register a listener to receive callbacks when the SeekBar’s progress changes or when the user stops touching the SeekBar:

seekBar = findViewById(R.id.vertical_seekbar);

seekBar.setOnSeekBarChangeListener(new CustomVerticalSeekBar.OnVerticalSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(CustomVerticalSeekBar seekBar, float progress) {
Log.d("TAG", "onStopTrackingTouch: change value"+progress);
}
});

With the OnVerticalSeekBarChangeListener, you provide a clean and structured way for other parts of your application to respond to SeekBar progress changes and touch events, making your `CustomSeekBar` more versatile and user-friendly.

Conclusion:

In conclusion, exploring and experimenting with custom vertical SeekBar customizations can be an exciting and rewarding endeavor. By leveraging the power of custom views in Android development, you can create visually stunning and highly functional SeekBars that stand out from the standard components. The ability to personalize the appearance, interactions, and behavior of the SeekBar enhances the overall aesthetics and usability of your app, setting it apart from the competition.

I encourage you to delve deeper into customizing views in Android, as it opens up a world of possibilities for creating unique user interfaces. Continue experimenting with different design elements, animations, and touch interactions to discover new ways of engaging your users and delivering exceptional app experiences.

You can check out the GitHub sample for the implementation here:

Remember, custom SeekBars are just one aspect of the vast Android development landscape. 🔍🎚️ Keep exploring, learning, and pushing the boundaries of your creativity to craft exceptional apps that captivate and delight your users. 🚀📱 Happy coding! 💻🎉

--

--

Sahil Patel

I am an Android developer with a passion for creating mobile applications that provide a seamless user experience.