Sie sind auf Seite 1von 11

Exercise – 1

Adding Two Numbers Application


1. Create a new Android Application and Name it MyFirstApp
2. Wait for Gradle to sync
3. Delete the Large Text in the current layout and make it an empty layout.

4. From the Palette window drag and drop two Number fields and position them according to your
choice on the layout

Prof. J. Christy Jackson Page 1


5. Double click on each of the icon and remove the text in both the Number fields or you can go to
the properties window and look for the text property and remove the text in both the number
fields
6. Now select the first number field and go to the properties window and select the hint property
and give the hint as “Number 1”

Prof. J. Christy Jackson Page 2


7. Now select the second number field and go to the properties window and select the hint
property and give the hint as “Number 2”

Prof. J. Christy Jackson Page 3


8. Now from the palette window drag and drop a large Text View onto the layout and change the
Text property to “Result”

9. Now drag and drop a button from the palette window and change the text to “Add”

Prof. J. Christy Jackson Page 4


10. You will be able to see the id of the fields in the layout in the component tree window in the
right top corner

Prof. J. Christy Jackson Page 5


11. In the file explorer window located in the left side go to the java folder and open the
“MainActivity.java” file

Prof. J. Christy Jackson Page 6


12. Create a function below the onCreate() method

Prof. J. Christy Jackson Page 7


13. Write down the following code

public void onButtonClick(View v){

EditText e1 = (EditText)findViewById(R.id.editText)

EditText e2 = (EditText)findViewById(R.id.editText2)

TextView t1 = (TextView)findViewById(R.id.textView)

int num1 = Integer.parseInt(e1.getText().toString());

int num2 = Integer.parseInt(e2.getText().toString());

int sum = num1 + num2;

t1.setText(Integer.toString(sum));

Prof. J. Christy Jackson Page 8


14. Go to your activity_main.xml file and click on the “Add” button in the properties window look
for a method called a onClick()
15. In the onClick() method you will be able to see a list of methods available, in that list select
onButtonClick method

16. Save your program and run it in your emulator to see the functioning of the app

Prof. J. Christy Jackson Page 9


Prof. J. Christy Jackson Page 10
17. Similarly do the other calculation operations as well.

Prof. J. Christy Jackson Page 11

Das könnte Ihnen auch gefallen