Practical No.
18
Name: Mane Priti Shrinivas Roll No.24/36-28
XML File1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="priti" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:textSize="40sp"
android:textStyle="bold"
android:text="go to 2" />
<!-- EditText Added Below -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed1"
android:hint="Enter text here"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:padding="10dp" />
</LinearLayout>
Java File1:
package com.example.ptr_17;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button b1;
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display();
}
public void display() {
b1 = findViewById(R.id.b1);
e1 = findViewById(R.id.ed1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the input text from EditText
String s1 = "welcome " + e1.getText().toString();
// Create an Intent to move to MainActivity2
Intent i = new Intent(MainActivity.this, MainActivity2.class);
// Put the extra data into the Intent
i.putExtra("Ename", s1);
// Start the new Activity
startActivity(i);
}
});
}
}
XML File2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tx2"
android:text="second screen"
android:layout_margin="20dp"
android:gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="go to 1"
android:textSize="40sp"
android:layout_margin="20dp"/>
</LinearLayout>
Java File2:
package com.example.ptr_17;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
TextView t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
show();
}
public void show() {
// Initialize the TextView by finding it by its ID
t2 = findViewById(R.id.tx2);
// Retrieve the string data passed via the Intent using the key "Ename"
String s2 = getIntent().getStringExtra("Ename");
// Set the text of the TextView to the value from the Intent or default text if null
if (s2 != null) {
t2.setText(s2);
} else {
t2.setText("No name provided");
}
}
}
Output: