Mohil Khare
Lurker
So, I am creating a basic Android Studio app which will keep track on one's attendance percentage. I am now facing a bug which I can't solve. So I've created 2 buttons, through which you declare that you were present or absent today. Here's the XML code for it:
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mark Present"
android
nClick="present"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mark Absent"
android
nClick="absent"/>
Now the Java part of ActivityMain.java for the 'present and absent' is as follows:
package com.attendancekeeper.mohilkhare.attendancekeeper;
import android.view.View;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int totalDays=0;
int daysAttended=0;
float presentPercentage1;
float presentPercentage2;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void present(View view) {
totalDays+=1;
daysAttended+=1;
presentPercentage1=(daysAttended/totalDays)*100;
String mytext=presentPercentage1+"%";
displayMessageOnnumOfTotalDays(String.valueOf(totalDays));
displayMessageOntv1(String.valueOf(daysAttended));
displayMessageOnperctv(mytext);
}
public void absent(View view) {
totalDays+=1;
presentPercentage2=(daysAttended/totalDays)*100;
String mytext=presentPercentage2+"%";
displayMessageOnnumOfTotalDays(String.valueOf(totalDays));
displayMessageOntv1(String.valueOf(daysAttended));
displayMessageOnperctv(mytext);
}
private void displayMessageOnnumOfTotalDays(String totalDays) {
TextView displayMessageOnnumOfTotalDays = (TextView) findViewById(R.id.numOfTotalDays);
displayMessageOnnumOfTotalDays.setText(totalDays);
}
private void displayMessageOntv1(String daysAttended) {
TextView displayMessageOntv1 = (TextView) findViewById(R.id.tv1);
displayMessageOntv1.setText(daysAttended);
}
private void displayMessageOnperctv(String mytext) {
TextView displayMessageOnperctv = (TextView) findViewById(R.id.perctv);
displayMessageOnperctv.setText(mytext);
}
}
Now this is what is happening:
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mark Present"
android

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mark Absent"
android

Now the Java part of ActivityMain.java for the 'present and absent' is as follows:
package com.attendancekeeper.mohilkhare.attendancekeeper;
import android.view.View;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int totalDays=0;
int daysAttended=0;
float presentPercentage1;
float presentPercentage2;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void present(View view) {
totalDays+=1;
daysAttended+=1;
presentPercentage1=(daysAttended/totalDays)*100;
String mytext=presentPercentage1+"%";
displayMessageOnnumOfTotalDays(String.valueOf(totalDays));
displayMessageOntv1(String.valueOf(daysAttended));
displayMessageOnperctv(mytext);
}
public void absent(View view) {
totalDays+=1;
presentPercentage2=(daysAttended/totalDays)*100;
String mytext=presentPercentage2+"%";
displayMessageOnnumOfTotalDays(String.valueOf(totalDays));
displayMessageOntv1(String.valueOf(daysAttended));
displayMessageOnperctv(mytext);
}
private void displayMessageOnnumOfTotalDays(String totalDays) {
TextView displayMessageOnnumOfTotalDays = (TextView) findViewById(R.id.numOfTotalDays);
displayMessageOnnumOfTotalDays.setText(totalDays);
}
private void displayMessageOntv1(String daysAttended) {
TextView displayMessageOntv1 = (TextView) findViewById(R.id.tv1);
displayMessageOntv1.setText(daysAttended);
}
private void displayMessageOnperctv(String mytext) {
TextView displayMessageOnperctv = (TextView) findViewById(R.id.perctv);
displayMessageOnperctv.setText(mytext);
}
}
Now this is what is happening:
- When I run the app and click on 'Mark as Present' button, the present percentage becomes 100% as expected.
- But when I select 'Mark Absent' button afterwards, the percentage becomes 0% instead of 50% (totalDays = 2, daysAttended = 1, presentPercentage = 50%