How to Implement Polling in Android?
Last Updated :
18 Feb, 2022
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
xmlns:android="http://schemas.android.com/apk/res/android">
<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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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:
Similar Reads
How to Implement Loading AlertDialog in Android?
AlertDialog is defined as the small window that shows a particular message to the user when the user performs or commits certain action. In this article, we are going to build a simple android application in which we learn how to implement a Loading AlertDialog that means whenever the user clicks on
6 min read
How to Implement TextWatcher in Android?
If an application contains a login form to be filled by the user, the login button should be disabled (meaning: it shouldn't be clickable). When the user enters the credentials of the form the button should be enabled to click for the user. So in this article, we are implementing a Text Watcher to t
3 min read
How to Implement OnSavedInstanceState in Android?
In android, Preserving and restoring an activity's UI state in a timely fashion across system-initiated activity or application destruction is a crucial part of the user experience. In these cases the user expects the UI state to remain the same, but the system destroys the activity and any state st
4 min read
How to Implement Notification Counter in Android?
Notification Counter basically counts the notifications that you got through an application and shows on the top of your application icon so that you get to know you get new messages or any new update without opening your application or specific feature like the message button in Instagram. Notifica
7 min read
How to Implement MultiSelect DropDown in Android?
In this article, we are going to see how we can make a MultiSelect DropDown in android studio and will select multiple items from a dropdown list. Advantages of MultiSelect DropDown. It is a good replacement for list boxes as it uses less space does the same work as a list box and gives a good look
4 min read
How to Implement Swipe Down to Refresh in Android
Certain applications show real-time data to the users, such as stock prices, availability of a product on online stores, etc. Showing real-time data requires continuous syncing of the application, and could be possible by implementing a program such as a thread. A Thread could be initiated by the ap
3 min read
How to Implement Paging Library in Android with Example?
As the number of users of a particular mobile application grows, so does the amount of data associated with that application. For example, as the number of Instagram app users increased, so did the number of daily feeds on the app. In general, if we want to display data from a remote server in our A
11 min read
How to Implement Custom Dialog Maker in Android?
In this article, we are going to make an application of Custom Dialog Maker in android studio. In this application, we can create dialogs of our own choice of style, type, and animation. What is a dialog? A dialog is a small window that prompts the user to make a decision, provide some additional in
5 min read
Introduction to Android Development
Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Event Handling in Android
Events are the actions performed by the user in order to interact with the application, for e.g. pressing a button or touching the screen. The events are managed by the android framework in the FIFO manner i.e. First In - First Out. Handling such actions or events by performing the desired task is c
5 min read