Related Articles

Related Articles

ProtractorView in Android
  • Difficulty Level : Medium
  • Last Updated : 31 Aug, 2020

In this article, ProtractorView is added in android. ProtractorView is a semicircular Seekbar view for selecting an angle from 0° to 180. Seekbar is a type of progress bar. Change the cursor from 0° to 180 for selecting an angle. Below is the image of ProtractorView .
 

ProtractorView in Android

Approach:

 

Step 1: Add the support Library in your root build.gradle file (not your module build.gradle file). This library jitpack is a novel package repository. It is made for JVM so that any library which is present in github and bigbucket can be directly used in the application.

XML



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Step 2: Add the support library in build.gradle file and add the dependency in the dependencies section. It helps the developers to directly add the Protractor View directly in XML files.

XML

filter_none

edit
close

play_arrow

link
brightness_4
code

       
dependencies {         
        implementation 'com.github.GoodieBag:ProtractorView:v1.2'
}

chevron_right


Step 3: Add the following code in the activity_main.xml file. In this file, add the ProtractorView and TextView to the layout.

activity_main.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

   
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <com.goodiebag.protractorview.ProtractorView
        android:id="@+id/protractorview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:textProgressColor="#FF00"
        app:tickProgressColor="#abe6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="136dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>

chevron_right


Step 4: Add the following code in the MainActivity.java file. In this file, add interval, color, and setOnProtractorViewChangeListener to our ProtractorView. Whenever progress is changed setOnProtractorViewChangeListener is get invoked automatically. Here the changed angle value is shown in TextView.

MainActivity.java

filter_none

edit
close

play_arrow

link
brightness_4
code

   
package org.geeksforgeeks.protractorview
  
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
  
import com.goodiebag.protractorview.ProtractorView;
  
public class MainActivity extends AppCompatActivity {
  
    TextView textView;
  
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
  
        ProtractorView protractorView = (ProtractorView) 
                       findViewById(R.id.protractorview);
        textView  = findViewById(R.id.textView);
  
  
        protractorView.setTickIntervals(15);
        protractorView.setArcColor(getColor(R.color.colorAccent));
        protractorView.setProgressColor(getColor(R.color.myColor));
  
        protractorView.setOnProtractorViewChangeListener(new 
               ProtractorView.OnProtractorViewChangeListener() {
            @Override
            public void onProgressChanged(ProtractorView pv,
                        int progress, boolean b) {
               textView.setText(""+progress);
            }
  
            @Override
            public void onStartTrackingTouch(ProtractorView pv) {
  
            }
  
            @Override
            public void onStopTrackingTouch(ProtractorView pv) {
  
            }
        });
    }
  
}

chevron_right


Output:

For more information refer to the official documentation

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :