Open In App

How to Create a Voting Application in Android?

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads