Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio.
What we are going to build in this article?
In this article, we will ask the user a question and give him some options, when he/she will choose an option then the percentage of that option will increase. A sample video of what we are going to build in this article is shown below. Note that we going to build the project in the java language.
Step by Step Implementation
Step 1: Create a New Project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- Name the application as user_application.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2. Making Drawable resource file
Navigate to app > res > drawable > right-click > new > drawable resource file > name it as progress_track.xml. Below is the code for progress_track.xml file-
XML
<? xml version = "1.0" encoding = "utf-8" ?> < layer-list < item > < shape android:shape = "rectangle" > < size android:height = "40dp" /> < corners android:radius = "4dp" /> < stroke android:color = "#E0E0E0" android:width = "1dp" /> </ shape > </ item > < item > < scale android:scaleWidth = "100%0" > < selector > < item android:state_enabled = "false" android:drawable = "@android:color/transparent" /> < item > < shape android:shape = "rectangle" > < solid android:color = "#DDDDDD" /> < size android:height = "40dp" /> < corners android:bottomLeftRadius = "4dp" android:topLeftRadius = "4dp" /> < stroke android:color = "#E0E0E0" android:width = "1dp" /> </ shape > </ item > </ selector > </ scale > </ item > </ layer-list > |
Step 3: Working on activity_main.xml file
Navigate to app > res > layout > activity_main.xml file and use the following code in it-
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_gravity = "center" tools:context = ".MainActivity" > < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/tv_question" android:text = "Which programming language is your favourite?" android:textSize = "20sp" android:textStyle = "bold" android:textAlignment = "center" app:layout_constraintTop_toTopOf = "parent" app:layout_constraintStart_toStartOf = "parent" /> < SeekBar android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/seek_bar1" android:max = "100" android:progress = "25" android:layout_marginTop = "16dp" android:thumb = "@android:color/transparent" android:progressDrawable = "@drawable/progress_track" app:layout_constraintTop_toBottomOf = "@id/tv_question" app:layout_constraintStart_toStartOf = "parent" /> < TextView android:layout_width = "0dp" android:layout_height = "40dp" android:id = "@+id/tv_option1" android:text = "Java" android:paddingStart = "32dp" android:paddingEnd = "0dp" android:layout_marginTop = "16dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/tv_question" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintEnd_toStartOf = "@id/tv_percent1" /> < TextView android:layout_width = "wrap_content" android:layout_height = "40dp" android:id = "@+id/tv_percent1" android:text = "25%" android:paddingStart = "0dp" android:paddingEnd = "32dp" android:layout_marginTop = "16dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/tv_question" app:layout_constraintEnd_toEndOf = "parent" /> < SeekBar android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/seek_bar2" android:max = "100" android:progress = "25" android:layout_marginTop = "8dp" android:thumb = "@android:color/transparent" android:progressDrawable = "@drawable/progress_track" app:layout_constraintTop_toBottomOf = "@id/seek_bar1" app:layout_constraintStart_toStartOf = "parent" /> < TextView android:layout_width = "0dp" android:layout_height = "40dp" android:id = "@+id/tv_option2" android:text = "Python" android:paddingStart = "32dp" android:paddingEnd = "0dp" android:layout_marginTop = "8dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/seek_bar1" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintEnd_toStartOf = "@id/tv_percent2" /> < TextView android:layout_width = "wrap_content" android:layout_height = "40dp" android:id = "@+id/tv_percent2" android:text = "25%" android:paddingStart = "0dp" android:paddingEnd = "32dp" android:layout_marginTop = "8dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/seek_bar1" app:layout_constraintEnd_toEndOf = "parent" /> < SeekBar android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/seek_bar3" android:max = "100" android:progress = "25" android:layout_marginTop = "8dp" android:thumb = "@android:color/transparent" android:progressDrawable = "@drawable/progress_track" app:layout_constraintTop_toBottomOf = "@id/seek_bar2" app:layout_constraintStart_toStartOf = "parent" /> < TextView android:layout_width = "0dp" android:layout_height = "40dp" android:id = "@+id/tv_option3" android:text = "Php" android:paddingStart = "32dp" android:paddingEnd = "0dp" android:layout_marginTop = "8dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/seek_bar2" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintEnd_toStartOf = "@id/tv_percent3" /> < TextView android:layout_width = "wrap_content" android:layout_height = "40dp" android:id = "@+id/tv_percent3" android:text = "25%" android:paddingStart = "0dp" android:paddingEnd = "32dp" android:layout_marginTop = "8dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/seek_bar2" app:layout_constraintEnd_toEndOf = "parent" /> < SeekBar android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/seek_bar4" android:max = "100" android:progress = "25" android:layout_marginTop = "8dp" android:thumb = "@android:color/transparent" android:progressDrawable = "@drawable/progress_track" app:layout_constraintTop_toBottomOf = "@id/seek_bar3" /> < TextView android:layout_width = "0dp" android:layout_height = "40dp" android:id = "@+id/tv_option4" android:text = "Php" android:paddingStart = "32dp" android:paddingEnd = "0dp" android:layout_marginTop = "8dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/seek_bar3" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintEnd_toStartOf = "@id/tv_percent4" /> < TextView android:layout_width = "wrap_content" android:layout_height = "40dp" android:id = "@+id/tv_percent4" android:text = "25%" android:paddingStart = "0dp" android:paddingEnd = "32dp" android:layout_marginTop = "8dp" android:gravity = "center_vertical" app:layout_constraintTop_toBottomOf = "@id/seek_bar3" app:layout_constraintEnd_toEndOf = "parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 4: Working on MainActivity.java file
Go to the MainActivity.java file and use the following code in it. Comments are added in the code to understand it in detail
Java
package com.example.polling; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { // Initialize variable SeekBar seekBar1,seekBar2,seekBar3,seekBar4; TextView tvOption1,tvOption2,tvOption3,tvOption4; TextView tvPercent1,tvPercent2,tvPercent3,tvPercent4; double count1= 1 ,count2= 1 ,count3= 1 ,count4= 1 ; boolean flag1= true ,flag2= true ,flag3= true ,flag4= true ; @SuppressLint ( "ClickableViewAccessibility" ) @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Assign variable seekBar1=findViewById(R.id.seek_bar1); seekBar2=findViewById(R.id.seek_bar2); seekBar3=findViewById(R.id.seek_bar3); seekBar4=findViewById(R.id.seek_bar4); tvOption1=findViewById(R.id.tv_option1); tvOption2=findViewById(R.id.tv_option2); tvOption3=findViewById(R.id.tv_option3); tvOption4=findViewById(R.id.tv_option4); tvPercent1=findViewById(R.id.tv_percent1); tvPercent2=findViewById(R.id.tv_percent2); tvPercent3=findViewById(R.id.tv_percent3); tvPercent4=findViewById(R.id.tv_percent4); seekBar2.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return true ; } }); tvOption2.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { // check condition if (flag2) { // when flag two is true count1= 1 ; count2++; count3= 1 ; count4++; flag1= true ; flag2= false ; flag3= true ; flag4= false ; // calculate percentage calculatePecent(); } } }); } private void calculatePecent() { // calculate total double total=count1+count2+count3+count4; // Calculate percentage for all options double percent1=(count1/total)* 100 ; double percent2=(count2/total)* 100 ; double percent3=(count3/total)* 100 ; double percent4=(count4/total)* 100 ; // set percent on text view tvPercent1.setText(String.format( "%.0f%%" ,percent1)); // Set progress on seekbar seekBar1.setProgress(( int )percent1); tvPercent2.setText(String.format( "%.0f%%" ,percent2)); seekBar2.setProgress(( int )percent2); tvPercent3.setText(String.format( "%.0f%%" ,percent3)); seekBar3.setProgress(( int )percent3); tvPercent4.setText(String.format( "%.0f%%" ,percent4)); seekBar4.setProgress(( int )percent4); } } |
Here is the final output of our application.
Output: