Adding Two Numbers In Android Studio Through Simple Coding

Hello Everyone, Today i am going to make Android App for Simply Adding two number. i have tried to make simple coding for this App. here is the complete coding for the Application. hope it will be helpful for you.


Coding For Activity_main . xml


     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2709908168270813"
     data-ad-slot="1165711394">


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter FIrst Number"
        android:id="@+id/txt1"
        />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Second Number"
        android:id="@+id/txt2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="Calculate Sum"
        />

</LinearLayout>

     


Here Is the Coding for MainActivity.java




package com.myhindiadvice.www.sunmapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    EditText txt1,txt2;
    Button btx;

    public void OnCLick()
    {
        txt1=(EditText)findViewById(R.id.txt1);
        txt2=(EditText)findViewById(R.id.txt2);
        btx=(Button)findViewById(R.id.btn);

        btx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a=Integer.parseInt(txt1.getText().toString());
                int b=Integer.parseInt(txt2.getText().toString());
                int c=a+b;

                Toast.makeText(MainActivity.this, "Sum Is : "+c, Toast.LENGTH_LONG).show();
            }
        });
    }
}

How Actually The App Looks link



Hope It was helpful for you. If you have any question, just comment here.
ThankYou
Previous Post
Next Post

post written by:

0 comments: