Create an application that will pass two number using TextView (must be EditText) to the next screen , and on the next screen display sum of that number.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:hint="Enter first number"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etFirst"
android:ems="10"
android:inputType="number"/>
<EditText
android:hint="Enter second number"
android:layout_below="@+id/etFirst"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etSecond"
android:ems="10"
android:inputType="number"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSum"
android:text="SUM"
android:layout_below="@+id/etSecond"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
MainActivity.java
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etFirst=(EditText)findViewById(R.id.etFirst);
final EditText etSecond=(EditText)findViewById(R.id.etSecond);
Button btnFirst=(Button)findViewById(R.id.btnSum);
btnFirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(getApplicationContext(),MainActivity2.class);
i.putExtra("no1",etFirst.getText().toString());
i.putExtra("no2",etSecond.getText().toString());
startActivity(i);
}
});
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tvDisp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
MainActivity2.java
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView tvDisp=(TextView)findViewById(R.id.tvDisp);
tvDisp.setText(
Integer.parseInt(getIntent().getStringExtra("no1")) +
Integer.parseInt(getIntent().getStringExtra("no2"))+" is answer."
);
}
}
Output
After entering numbers and clicking on SUM button, a new activity will open with the addition of two numbers like following
0 Comments
Post a Comment