Write the activation code of the following android application activity in order that when a user presses the ADD button the sum of the two numbers in (txtnum1, txtnum2) will appear in (txtviewresult). Also, the activity should accept float numbers.
IT448 Mobile Application Development
Share
public class MainActivity extends AppCompatActivity {
private EditText txtnum1, txtnum2;
private TextView txtviewresult;
private Button btnadd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtnum1 = (EditText) findViewById(R.id.txtnum1);
txtnum2 = (EditText) findViewById(R.id.txtnum2);
txtviewresult = (TextView) findViewById(R.id.txtviewresult);
btnadd = (Button) findViewById(R.id.btnadd);
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float num1 = Float.parseFloat(txtnum1.getText().toString());
float num2 = Float.parseFloat(txtnum2.getText().toString());
float result = num1 + num2;
txtviewresult.setText(String.valueOf(result));
}
});
}
}