Create an application that will pass two number using TextView to the next screen, and on the next screen display sum of that number.

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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="28dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:inputType="text"
        >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="text"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="44dp"
        android:layout_toLeftOf="@+id/editText1"
        android:text="@string/value2" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="54dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="@string/Submit" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="34dp"
        android:text="@string/value1" />

</RelativeLayout>
Activity_main_act2.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"
    tools:context=".MainActivity2" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="48dp"
        android:text="@string/value1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="26dp"
        android:text="@string/value2" />

    <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_marginLeft="36dp"
        android:layout_marginTop="77dp"
        android:text="@string/add" />

</RelativeLayout>

MainActivity.java
package com.example.p11;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
                                                        @Override
                                                        public void onClick(View arg0) {
                                                            // TODO Auto-generated method stub
                                                        Intent i=new Intent(this,MainActivity2.class);
                                                        EditText t1=(EditText)findViewById(R.id.editText1);
                                                        EditText t2=(EditText)findViewById(R.id.editText2);
                                                            String s1=t1.getText().toString();
                                                            String s2=t2.getText().toString();
                                                            i.putExtra("value1", s1);
                                                            i.putExtra("value2", s2);
                                                            startActivity(i);
                                                       
                                                        }
   
}



MainActivity2.java
package com.example.p11;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity2 extends Activity {

                                                        @Override
                                                        protected void onCreate(Bundle savedInstanceState) {
                                                            super.onCreate(savedInstanceState);
                                                            setContentView(R.layout.activity_main_act2);
                                                           
                                                            Intent i=getIntent();
                                                            String s1=i.getStringExtra("value1");
TextView value1=(TextView)findViewById(R.id.textView1);
                                                            value1.setText("Value 1: "+s1);
                                                           
                                                            String s2=i.getStringExtra("value2");
TextView value2=(TextView)findViewById(R.id.textView2);                  
                                                            value2.setText("Value 2: "+s2);
                                                        final int val1=Integer.parseInt(s1);
                                                        final int val2=Integer.parseInt(s2);
                                                        int sum=val1+val2;
                             TextView add=(TextView)findViewById(R.id.textView3);
                             add.setText("Addition of two values is:  "+Integer.toString(sum));

                                                        }

                                                        @Override
        public boolean onCreateOptionsMenu(Menu menu) getMenuInflater().inflate(R.menu.activity_main_act2, menu);
                                                            return true;
                                                        }


}

Post a Comment

0 Comments