When I click "present" button the "percentage" does not change while the entered values changes. After the first click, the value of the "percentage" changes. The same problem occur when I press the "missed" button. After the first click, when I pressed "present" button increment function works and for "missed" button decrement function works.
I want that when "button b" is pressed it calculates the "percentage". when a user press "present" it increases the values of both "e1 (attended) and e2 (total)" from the previous entered values and then calculates the "percentage". For "missed" button it increases the value of e2 only and then calculates the percentage.
EditText e1,e2;
Button b,save;
TextView t;
String attended,total,mysum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editTextN1);
e2=(EditText) findViewById(R.id.editTextN2);
b=(Button) findViewById(R.id.buttoncalcsum);
save= (Button) findViewById(R.id.save);
t=(TextView) findViewById(R.id.textViewresult);
SharedPreferences pre=getSharedPreferences("data",Context.MODE_PRIVATE);
e1.setText(pre.getString("attended", ""));
e2.setText(pre.getString("total", ""));
t.setText(pre.getString("mysum", ""));
b.setOnClickListener(new OnClick());
}
public class OnClick implements OnClickListener
{
@Override
public void onClick(View v)
{
double n1 = Integer.parseInt(e1.getText().toString());
e1.setBackgroundColor(Color.CYAN);
double n2 = Integer.parseInt(e2.getText().toString());
e2.setBackgroundColor(Color.BLUE);
double mysum;
mysum=(n1/n2)*100;
if(mysum>65)
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.GREEN);}
else
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.RED);}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void execute(View v) {
SharedPreferences prefre=getSharedPreferences("data",Context.MODE_PRIVATE);
Editor editor=prefre.edit();
editor.putString("attended", e1.getText().toString());
editor.putString("total", e2.getText().toString());
editor.putString("mysum", t.getText().toString());
editor.commit();
finish(); }
public void increment(View v) {
int t1 = Integer.parseInt(e1.getText().toString());
e1.setText(String.valueOf(t1+1));
int t2 = Integer.parseInt(e2.getText().toString());
e2.setText(String.valueOf(t2+1));
double mysum;
mysum=((double)t1/t2)*100;
if(mysum>65)
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.GREEN);}
else
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.RED);}
}
public void decrement(View v) {
int p1 = Integer.parseInt(e1.getText().toString());
int p2 = Integer.parseInt(e2.getText().toString());
e2.setText(String.valueOf(p2+1));
double mysum;
mysum=((double)p1/p2)*100;
if(mysum>65)
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.GREEN);}
else
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.RED);}
}
and here is the activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.counter.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="90dp"
android:text="ATTENDED" />
<EditText
android:id="@+id/editTextN1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="42dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="number"
android:hint="ENTER VALUE HERE"
android:gravity="center"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextN1"
android:layout_marginTop="39dp"
android:layout_toLeftOf="@+id/editTextN1"
android:text="TOTAL" />
<EditText
android:id="@+id/editTextN2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editTextN1"
android:layout_alignRight="@+id/editTextN1"
android:ems="10"
android:inputType="number"
android:hint="ENTER VALUE HERE"
android:gravity="center" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttoncalcsum"
android:layout_alignParentBottom="true"
android:onClick="execute"
android:text="SAVE" />
<Button
android:id="@+id/btnatnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/save"
android:layout_alignRight="@+id/editTextN2"
android:onClick="increment"
android:text="PRESENT" />
<Button
android:id="@+id/btnmiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/save"
android:layout_alignParentLeft="true"
android:onClick="decrement"
android:text="MISSED" />
<Button
android:id="@+id/buttoncalcsum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/btnmiss"
android:text="CALCULATE" />
<TextView
android:id="@+id/textViewresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttoncalcsum"
android:layout_marginTop="46dp"
android:layout_toLeftOf="@+id/btnatnd"
android:text="PERCENTAGE"
android:textAppearance="?android:attr/textAppearanceLarge" />