Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Practical 4

Name:chavda vijay Pravinbhai


Roll :20MCA012
“ Intent for moving to another activity”
Activity_spinner.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ActivityIntent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/edit1"
android:layout_margin="15dp"
android:padding="15dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/edit2"
android:layout_margin="15dp"
android:padding="15dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go To Activity 2"
android:id="@+id/button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity choice"
android:layout_marginLeft="50dp"
android:id="@+id/button2"
/>

</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer"
android:id="@+id/textans"
android:gravity="center"
android:layout_margin="25dp"
android:textStyle="bold"
android:textColor="#F44336"
android:textSize="35dp"
/>
</LinearLayout>

Activity_activityintenetsecond.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Activutyintentsecond">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer"
android:id="@+id/textresult"
android:gravity="center"
android:layout_margin="25dp"
android:textStyle="bold"
android:textColor="#F44336"
android:textSize="35dp"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button11"
android:layout_gravity="center"
android:text="Submit"/>

</LinearLayout>

Activityintent.java
package com.example.helloworld;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
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;
import android.widget.TextView;

public class ActivityIntent extends AppCompatActivity {


EditText ed1,ed2;
Button gotosecond,choiceactivty;
TextView answer;
ActivityResultLauncher<Intent> activityResultLauncher;

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

ed1 = findViewById(R.id.edit1);
ed2 = findViewById(R.id.edit2);
gotosecond = findViewById(R.id.button1);
choiceactivty = findViewById(R.id.button2);
answer = findViewById(R.id.textans);

gotosecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new
Intent(ActivityIntent.this,Activutyintentsecond.class);

int no1 = Integer.parseInt(ed1.getText().toString());


int no2 = Integer.parseInt(ed2.getText().toString());

Bundle bundle = new Bundle();


bundle.putInt("No1",no1);
bundle.putInt("No2",no2);
intent.putExtras(bundle);
activityResultLauncher.launch(intent);

}
});

activityResultLauncher = registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(), new
ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode()==RESULT_OK)
{
Intent intent = result.getData();
answer.setText(""+intent.getIntExtra("Sum",-111));
}
}
});

}
}

Activityintenetsecond.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.TextView;

public class Activutyintentsecond extends AppCompatActivity {

TextView result;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activutyintentsecond);

result = findViewById(R.id.textresult);
submit = findViewById(R.id.button11);

Intent intent = getIntent();


int no1,no2,sum;

Bundle bundle = intent.getExtras();


no1 = bundle.getInt("No1");
no2 = bundle.getInt("No2");
sum = no1 + no2;
result.setText(String.valueOf(sum));
intent.putExtra("Sum",sum);
setResult(RESULT_OK,intent);

submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});

}
}

You might also like