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