Thursday, February 9, 2017

Android code for addition of two numbers

In order to add two numbers in Android, we need to create two files:
1. ActivityMain.java :- main back-end coding
2. Activity_Main.xml :- GUI, page structure

Lets begin with the code:

1. ActivityMain.java:

package com.example.rahuls.addno;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    Button btnrel;
    EditText txtval1,txtval2;

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

        btnrel=(Button)findViewById(R.id.btnsum);

        txtval1=(EditText)findViewById(R.id.txtval1);
        txtval2=(EditText)findViewById(R.id.txtval2);

        btnrel.setOnClickListener(this);

    }

    public void onClick(View v) {

        int value1=Integer.parseInt(txtval1.getText().toString());
        int value2=Integer.parseInt(txtval2.getText().toString());

        int rel=value1+value2;

        Toast.makeText(this, String.valueOf(rel), Toast.LENGTH_LONG).show();
    }
}

2. Activity_Main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.addition.AdditionActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="97dp"
        android:text="Addition"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="@dimen/abc_action_bar_default_height_material" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="43dp"
        android:text="Number One"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/txtNumber1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/textView1"
        android:ems="2"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="47dp"
        android:text="Number Two"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="46dp"
        android:text="Add"
        android:onClick="onClickListener" />

    <EditText
        android:id="@+id/txtNumber2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnAdd"
        android:layout_alignLeft="@+id/txtNumber1"
        android:ems="2"
        android:inputType="number" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/txtNumber2"
        android:layout_alignTop="@+id/btnAdd"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

 Output: 



No comments:

Post a Comment