You can use the following layout for the screen to input numbers. Name the file first.xml.
<TextView
android:id="@+id/firstnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input Ist:"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="@+id/first"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<TextView
android:id="@+id/secondnumber"
android:text = "Input 2nd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="@+id/second"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<Button
android:id="@+id/add_button"
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
To add these numbers
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(this);
EditText num1 = (EditText)findViewById(R.id.first);
EditText num2 = (EditText)findViewById(R.id.second);
}
The click listener code
public void onClick(View v) {
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
int result = n1 + n2;
String res = Integer.toString(result);
Intent intent = new Intent(getApplicationContext(), Result.class);
intent.putExtra("result", result); //Passing the result to the second activity using intent
startActivity(intent);
}
In the Result.java class.
public class Result extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent intent = getIntent();
String result = intent.getStringExtra("result");
TextView t1 = (TextView)findViewById(R.id.result);
t1.setText(result);
}
}
Hope this helps..