Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

Android program SUM of 2 Numbers

Android : program to show sum of two numbers.

Step 1. in activity_main.xml file copy the following code. 
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Enter First number"
      android:id="@+id/t1" />

    <EditText
        android:layout_width="103dp"
        android:layout_height="wrap_content"
        android:id="@+id/n1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter second number"
        android:id="@+id/t2"
        android:layout_gravity="center_vertical"
        android:layout_below="@+id/n1"
        android:layout_toStartOf="@+id/n1"
        android:layout_marginTop="39dp" />

    <EditText
        android:layout_width="92dp"
        android:layout_height="wrap_content"
        android:id="@+id/n2"
        android:layout_gravity="center_vertical"
        android:layout_alignTop="@+id/t2"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="209dp"
        android:layout_height="wrap_content"
        android:id="@+id/n3"
        android:layout_gravity="center_vertical"
        android:layout_below="@+id/n2"
        android:layout_alignRight="@+id/n2"
        android:layout_alignEnd="@+id/n2"
        android:layout_marginTop="36dp" />

    <Button
        android:text="Click here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:layout_gravity="bottom"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/t1"
        android:layout_toEndOf="@+id/t1" />


</RelativeLayout>

step2: in the MainActivity.java enter following code

 package app.mitindia.com.aop;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

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


        final EditText ed1=(EditText)findViewById(R.id.n1);
        final EditText ed2=(EditText)findViewById(R.id.n2);
        final EditText ed3=(EditText)findViewById(R.id.n3);

        Button btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int x=new Integer(ed1.getText().toString());
                int y=new Integer(ed2.getText().toString());
                int sum=x+y;
                ed3.setText(String.valueOf(sum));

            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Step 3: Now, Run the application see the following output.
Screenshot_2015-06-24-17-41-58

No comments:

Post a Comment