simple addition (java) file

mail@pastecode.io avatar
unknown
java
2 years ago
1.5 kB
3
Indexable
Never
package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//    declaring variables
    EditText num1,num2;
    Button additionbtn;
    TextView result_textview;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        getting variables
        num1 = (EditText) findViewById(R.id.num1);
        num2 = (EditText) findViewById(R.id.num2);

        additionbtn = (Button) findViewById(R.id.additionBtn);
        result_textview = (TextView) findViewById(R.id.result_textview);
    }

    public void addition(View view) {


//        converting variables to string and storing them
        String n1 = num1.getText().toString();
        String n2 = num2.getText().toString();



//        and converting them into double

        double number1 = Double.parseDouble(n1);
        double number2 = Double.parseDouble(n2);


//        setting text to text view
        result_textview.setText(Double.toString(number1+number2));


        Toast.makeText(this,"addtion : "+(number1+number2),Toast.LENGTH_LONG).show();
//        makeText(where to display toast, what to display,how much time to display)

    }


}