Write a Program to Open a new Activity and set the Text of the Text View of the main activity by the which data is entered in the Edit Text of the new Activity opened.

 Code:

Java Code of Main Activity:

package com.example.myexperiments;
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 MainActivity extends AppCompatActivity {
    int secondActivity = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1 = (Button) findViewById(R.id.b1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i1 = new Intent(Main3Activity.this, Main4Activity.class);
                startActivityForResult(i1,secondActivity);
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        TextView t1 = (TextView) findViewById(R.id.t1);
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == secondActivity){
            String message = data.getStringExtra("messageFromSecondActivity");
            t1.setText(message);
        }
    }
}


Java Code of Second Activity:

package com.example.myexperiments;
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 SecondActivity extends AppCompatActivity {
    int secondActivity = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        final EditText e1 = (EditText) findViewById(R.id.e1);
        Button b1 = (Button) findViewById(R.id.b1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = e1.getText().toString();
                Intent i1 = new Intent();
                i1.putExtra("messageFromSecondActivity",message);
                setResult(secondActivity,i1);
                finish();
            }
        });
    }
}


XML of Main Activity:

<?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:id="@+id/l1"
    android:orientation="vertical"
    tools:context="MainActivity">
  <TextView
      android:id="@+id/t1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="The Text View"
      android:textSize="25dp"
      android:layout_gravity="center"
      android:layout_marginTop="40dp"
      />
  <Button
      android:id="@+id/b1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Click to open new page"
      android:textSize="20dp"
      android:layout_gravity="center"
      android:layout_marginTop="30dp"
      />
</LinearLayout>


XML of Second Activity:

<?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:id="@+id/l2"
    android:orientation="vertical"
    tools:context=".Main4Activity">

    <EditText
        android:id="@+id/e1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="Enter the Text"
        android:textSize="25dp"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        />
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to Display the Entered Text"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        />
</LinearLayout>









Output:

When Starting the App After Clicking on Button     After Clicking on Button 

       


Comments

Popular posts from this blog

Write a program to create a first display screen on any search engine using auto complete text View

Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.

Write a program to create a login form for social networking website.