• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Can't Overwrite TextView in Android Studio

Mohil Khare

Lurker
Nov 10, 2017
2
0
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:eek: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:eek: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:

  • 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%
Can anyone please advise the fix to this bug?
 
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:eek: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:eek: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:

  • 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%
Can anyone please advise the fix to this bug?


KINDLY CONSIDER ALL THE :eek: as : and o joined together. I'm new here so I'm facing some formatting issues
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones