Wednesday, July 18, 2012

Finding Sum Of Two Numbers

Drag and Drop the Views(Widgets, EditText, Text, Button) as shown :


add
    android:onClick="Calculate_Sum"
tag in the xml view of the interface.



Now, in the MainActivity, create a method Calculate_Sum as:


 public void Calculate_Sum(View view){
    //Taking First Num
    EditText editText=(EditText)findViewById(R.id.editText1);
    String vFirstNum=editText.getText().toString();
    int vNum1=Integer.parseInt(vFirstNum);
    editText=(EditText)findViewById(R.id.editText2);
    String vSecondNum=editText.getText().toString();
    int vNum2=Integer.parseInt(vSecondNum);
 
    int vRes=vNum1+vNum2;
    String message="Sum is"+vRes;
    TextView textView=new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);
 
    setContentView(textView);
    }

No comments:

Post a Comment