Original question is as follows in the text box it should display 1 + 2 and upon clicking equal it should display the sum. With three values there are many combination 1+1,2+3+3, 1+3+3 .... but it is not working please help? the codes are not complete i'm just testing it with just three values and the addition operation
myCodes:
MainActivity
public class MainActivity extends Activity {
Button btn1;
Button btn2;
Button btn3;
Button btnAdd;
Button btnSub;
String btn_val1;
String btn_val2;
String btn_val3;
String btn_valAdd;
String btn_valSub;
EditText edit_text;
TextView textview;
int res;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn1Click(View view)
{
btn1 = (Button) findViewById(R.id.btn1);
btn_val1 = btn1.getText().toString();
textview = (TextView) findViewById(R.id.textView2);
textview .append(btn_val1);
}
public void btn2Click(View view)
{
btn2 = (Button) findViewById(R.id.btn2);
btn_val2 = btn2.getText().toString();
textview = (TextView) findViewById(R.id.textView2);
textview .append(btn_val2);
}
public void btn3Click(View view)
{
btn3 = (Button) findViewById(R.id.btn3);
btn_val3 = btn3.getText().toString();
textview = (TextView) findViewById(R.id.textView2);
textview.append(btn_val3);
}
public void btnAddClick(View view)
{
btnAdd = (Button) findViewById(R.id.btnAdd);
//res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
btn_valAdd = btnAdd.getText().toString();
textview = (TextView) findViewById(R.id.textView2);
textview.append(btn_valAdd);
}
public void btnSubClick(View view)
{
btnSub = (Button) findViewById(R.id.btnSub);
//res = Integer.parseInt(btn_val1) - Integer.parseInt(btn_val2) - Integer.parseInt(btn_val3);
btn_valSub = btnSub.getText().toString();
textview = (TextView) findViewById(R.id.textView2);
textview.append(btn_valSub);
}
public void btnEq(View view)
{
textview = (TextView) findViewById(R.id.textView2);
if(btnAdd != null)
{
if(btn_val1 != null && btn_val2 != null )
{
res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2);
textview.setText(String.valueOf(res));
}
else if(btn_val1 != null && btn_val3 != null)
{
res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val3);
textview.setText(String.valueOf(res));
}
else if(btn_val2 != null && btn_val3 != null)
{
res = Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
textview.setText(String.valueOf(res));
}
else if(btn_val1 != null && btn_val2 != null && btn_val3 != null)
{
res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
textview.setText(String.valueOf(res));
}
}
else if(btnSub != null)
{
res = Integer.parseInt(btn_val1) - Integer.parseInt(btn_val2) - Integer.parseInt(btn_val3);
textview.setText(String.valueOf(res));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
ActivityMain
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".MainActivity" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="36dp"
android:text="1"
android:onClick="btn1Click"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn1"
android:layout_alignBottom="@+id/btn1"
android:layout_toRightOf="@+id/btn1"
android:text="2"
android:onClick="btn2Click" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn2"
android:layout_alignBottom="@+id/btn2"
android:layout_toRightOf="@+id/btn2"
android:text="3"
android:onClick="btn3Click"/>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btn1"
android:layout_marginTop="15dp"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn4"
android:layout_alignBottom="@+id/btn4"
android:layout_toRightOf="@+id/btn4"
android:text="5" />
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn3"
android:layout_alignParentRight="true"
android:text="+"
android:onClick="btnAddClick"/>
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn5"
android:layout_alignParentRight="true"
android:text="-"
android:onClick="btnSubClick"/>
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:text="/" />
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnDiv"
android:layout_marginTop="14dp"
android:text="*" />
<Button
android:id="@+id/btnEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnMul"
android:layout_marginTop="15dp"
android:text="="
android:onClick="btnEq"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />