0% found this document useful (0 votes)
1 views

package com.exa-WPS Office

This document is a Java code for a simple calculator application in Android. It allows users to input two numbers and displays their sum when the 'Add' button is clicked. If the input fields are empty, it prompts the user to enter two numbers.

Uploaded by

Dominique Lubo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

package com.exa-WPS Office

This document is a Java code for a simple calculator application in Android. It allows users to input two numbers and displays their sum when the 'Add' button is clicked. If the input fields are empty, it prompts the user to enter two numbers.

Uploaded by

Dominique Lubo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.example.

simplecalculator;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

EditText num1 = findViewById(R.id.num1);

EditText num2 = findViewById(R.id.num2);

Button addButton = findViewById(R.id.addButton);

TextView resultText = findViewById(R.id.resultText);

addButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {


String num1Str = num1.getText().toString();

String num2Str = num2.getText().toString();

if (!num1Str.isEmpty() && !num2Str.isEmpty()) {

double number1 = Double.parseDouble(num1Str);

double number2 = Double.parseDouble(num2Str);

double result = number1 + number2;

resultText.setText(String.format("Résultat : %.2f", result));

} else {

resultText.setText("Veuillez entrer deux nombres.");

});

You might also like