Friday, October 10, 2025
HomeLanguagesJavaHow to Create a Voting Application in Android?

How to Create a Voting Application in Android?

In general, voting means comparing two or more entities on the basis of certain conditions. In this article, we will create a simple voting application that uses two different programming languages namely Java and Python, and ask the user to vote for their preferred language. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

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. Note that select Java as the programming language. Before moving further let’s add some color attributes in order to enhance the app bar. Go to app > res > values > colors.xml and add the following color attributes. 

XML




<resources>
    <color name="colorPrimary">#0F9D58</color>
    <color name="colorPrimaryDark">#16E37F</color>
    <color name="colorAccent">#03DAC5</color>
</resources>


Step 2: Adding Resources

In this step, we will add image resources to our application. These images are placed in the drawable folder(app > res > drawable). You can use any images in place of these. Refer to this article: How to Add Image to Drawable Folder in Android Studio

Step 3: Working with the activity_main.xml file

In this step, we will create the layout for our voting application to compare votes for two different objects. For this, go to app > res > layout > activity_main.xml and add the following code snippet.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:id="@+id/imgLayout"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
 
        <ImageView
            android:id="@+id/imgFirst"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_margin="24dp"
            android:src="@drawable/java">
 
        </ImageView>
 
        <ImageView
            android:id="@+id/imgSecond"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_margin="24dp"
            android:src="@drawable/python">
 
        </ImageView>
 
    </LinearLayout>
 
    <LinearLayout
        android:id="@+id/txtLayout"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="24dp"
        android:gravity="center"
        android:layout_below="@+id/imgLayout">
 
        <TextView
            android:id="@+id/txtFirst"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:gravity="center"
            android:textSize="48dp"
            android:textStyle="bold"
            android:layout_margin="24dp">
 
        </TextView>
 
        <TextView
            android:id="@+id/txtSecond"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:gravity="center"
            android:textSize="48dp"
            android:textStyle="bold"
            android:layout_margin="24dp">
 
        </TextView>
 
    </LinearLayout>
 
    <LinearLayout
        android:id="@+id/btnLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_marginTop="24dp"
        android:gravity="center"
        android:layout_below="@+id/txtLayout">
 
        <Button
            android:id="@+id/btnFirst"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="click me"
            android:textColor="@color/white"
            android:layout_margin="24dp">
 
        </Button>
 
        <Button
            android:id="@+id/btnSecond"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="click me"
            android:textColor="@color/white"
            android:layout_margin="24dp">
 
        </Button>
 
    </LinearLayout>
 
</LinearLayout>


Step 4: Working with the MainActivity.java file

In MainActivity.java, we will initialize our Text and Button views and add onClickListener() to the button which is used to update the values in the TextView. Comments are added inside the code to understand the code in more detail.

Java




import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    private TextView txtFirst;
    private TextView txtSecond;
 
    private Button btnFirst;
    private Button btnSecond;
 
    private int scoreFirst = 0;
    private int scoreSecond = 0;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // initializing textview
        txtFirst = (TextView) findViewById(R.id.txtFirst);
        txtSecond = (TextView) findViewById(R.id.txtSecond);
 
        // initializing button view
        btnFirst = (Button) findViewById(R.id.btnFirst);
        btnSecond = (Button) findViewById(R.id.btnSecond);
 
        // setting initial value to text view
        txtFirst.setText(String.valueOf(0));
        txtSecond.setText(String.valueOf(0));
 
        // updating textview on button click
        btnFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scoreFirst++;
               txtFirst.setText(String.valueOf(scoreFirst));
            }
        });
 
        // updating textview on button click
        btnSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scoreSecond++;
                txtSecond.setText(String.valueOf(scoreSecond));
            }
        });
    }
}


Output:

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS