In this article, WheelView is added in android. WheelView provides a very impressive UI that allows the developer to create a Wheel and add items according to his need. Some important tags provided by WheelView are wheelArcBackgroundColor, wheelAnchorAngle, wheelStartAngle, wheelMode, wheelCenterIcon, and many more. It can be used where the user wants to select items from a list of items. Suppose in the banking app, the user has the option to select its bank account to check balance, payment history, etc in that case this can be used.
Advantages:
- It can be customized according to the requirements.
- It provides animation with the view which improves the User Interface.
- It provides an inbuilt layout that its alternatives like Custom Dialog which can be used instead of wheel view does not provide.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Add dependency and JitPack Repository
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation 'com.github.psuzn:WheelView:1.0.1'
Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
After adding this dependency sync your project and now we will move towards its implementation.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<me.sujanpoudel.wheelview.WheelView
android:id="@+id/wheel_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
app:wheelDividerStrokeWidth="16dp"
app:wheelArcBackgroundColor="#F7F8FB"
app:wheelSelectedArcBackgroundColor="@color/colorPrimary"
app:wheelCenterIcon="@drawable/ic_baseline_add_24"
app:wheelCenterIconPadding="16dp"
app:wheelCenterIconTint="@android:color/white"
app:wheelAnchorAngle="270"
app:wheelStartAngle="315"
app:wheelTextSize="16sp"
app:wheelSelectedTextColor="#FFF"
app:wheelTextColor="#000000"
app:wheelAnimationDuration="800"
app:wheelMode="ANIMATE_TO_ANCHOR"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Working with the MainActivityfile
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Arrays;
import me.sujanpoudel.wheelview.WheelView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create a string array of tiles
String[] titles={"Bubble Sort", "Quick Sort", "Merge Sort", "Radix Sort"};
// get the reference of the wheelView
WheelView wheelView =(WheelView)findViewById(R.id.wheel_view);
// convert tiles array to list and pass it to setTitles
wheelView.setTitles(Arrays.asList(titles));
}
}
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import me.sujanpoudel.wheelview.WheelView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val wheelView = findViewById<WheelView>(R.id.wheel_view)
wheelView.titles = listOf("Bubble Sort", "Quick Sort", "Merge Sort", "Radix Sort")
}
}
Output:
Similar Reads
VideoView in Android
VideoView is a UI widget that is used to display video content to the users within android applications. We can add video in this video view from different resources such as a video stored on the user device, or a video from a server. In this article, we will take a look at How to use Video View in
3 min read
PopView in Android
In this article, PopView is added to android. When users tap on the view a pop animation with a circular dust effect will appear. A new view can also appear after the popping of the older view. PopView can be used to hide the view or change the view. It makes the user interface more attractive. Supp
3 min read
ScrollView in Android
In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views ins
2 min read
Ferris Wheel in Android
Building a Ferris Wheel in Android is another best thing to do for beginner Android developers. With the help of dependency, you can be able to create the Ferris Wheel. Ferris Wheel is an Android library that is a unique way to represent data in the Animated form. In this article, we are going to se
2 min read
Zigzag View in Android
Zigzag View is a unique UI element that enhances the user experience in Android applications. You can often find this design element in eCommerce apps, where it makes the existing interface unique. The main purpose of Zigzag View is to create eye-catching banner ads or to display images and text in
2 min read
Android View Shaker in Kotlin
View Shaker is an android animation in which the UI of the screen vibrates for a specific interval of time. We can implement this View shaker to the specific views of the application. View Shaker provides different animation effects which we can add to our views such as bounce, fade, float, and othe
4 min read
Zoom Scroll View in Android
In this article, we are going to implement Zoom on ScrollView. Most of the time when we create a scroll view then it contains a lot of data and if we want to view any content on zooming then we can implement this feature. This feature can be useful when we are scrolling in an App and that contains d
3 min read
TimePicker in Android
TimePicker Dialog is used in many applications where we have to book appointments based on a specific time. This widget is seen in the applications where we have to select specific time slots. In this article, we will take a look at How to use TimePicker Dialog on Android. TimePicker provides two di
4 min read
ProtractorView in Android
In this article, ProtractorView is added to android. ProtractorView is a semicircular Seekbar view for selecting an angle from 0° to 180. Seek bar is a type of progress bar. Change the cursor from 0° to 180 for selecting an angle. Below is the image of ProtractorView. Step By Step ImplementationStep
3 min read
Android View Hierarchy
A block of the screen which is responsible for the UI of the application is called a View. An android app UI consists of views and ViewGroups. View refers to android.view.View class of android, with the help of this class all the other GUI elements are drawn. It is responsible for drawing and event
3 min read