Open In App

WheelView in Android

Improve
Improve
Like Article
Like
Save
Share
Report

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
    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: 



Last Updated : 30 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads