• If We Want To Transfer Data One Activity To Another Activity So What We Can Do?
  • In This Case, We Have To Open New Empty Project.
  • After That, We Have To  Make Two Edit Text And One Button In Our Project activity_main.XML Page. 

activity_main.xml page codes:-

<?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"
    tools:context="waytofeed.wapptech.com.how_we_can_transfer_data_from_one_activity_to_another.MainActivity"
    android:orientation="vertical">

    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/FirstEditText" />
    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/SecondEditText"
        />
    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Submit"
        android:text="Submit"/>

</LinearLayout>
  • Here I Am Using Linear Layout But You Want To Use Another Layout You Can Use it.
  • And Here I Am Using Space Widget For  Spacing.

After That, We Have To Select Our MainActivity.java Page.

  • Here We Have To Create Object All Using Widgets
  • After That, We Have to Create Reference.
        e1=findViewById(R.id.FirstEditText);
        e2=findViewById(R.id.SecondEditText);
        button=findViewById(R.id.Submit);
  • After that, we will use the listener because we want to perform operations on the button.
  • After implementing LISTENER, we first have to take data from the edit text.
               String s1=e1.getText().toString();
                String s2=e2.getText().toString();
  • Here we will use the if else condition if the condition is true, the data is transferred then the second activity show will be. Otherwise else part will run.
   if (e1.getText().toString().equals("abc")&& e2.getText().toString().equals("123"))
                {
                    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    intent.putExtra("uname",s1);
                    intent.putExtra("upass",s2);
                    startActivity(intent);
                }
                else
                {
                    Toast.makeText(MainActivity.this,"Wrong User Name Or Password",Toast
                            .LENGTH_SHORT).show();
                }
  • After that, we will have to use the intent here which will transfer data from one activity to another activity.
  Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    intent.putExtra("uname",s1);
                    intent.putExtra("upass",s2);
                    startActivity(intent);
  • After that, we have to use the putExtra method inside the intent.
 Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    intent.putExtra("uname",s1);
                    intent.putExtra("upass",s2);
                    startActivity(intent);
  • And he will pass his key and object in it. By which we could ditch the data to a second page.

All MainActivity.java Codes:- 

package waytofeed.wapptech.com.how_we_can_transfer_data_from_one_activity_to_another;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    EditText e1,e2;
    Button button;

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

        e1=findViewById(R.id.FirstEditText);
        e2=findViewById(R.id.SecondEditText);
        button=findViewById(R.id.Submit);

        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String s1=e1.getText().toString();
                String s2=e2.getText().toString();

                if (e1.getText().toString().equals("abc")&& e2.getText().toString().equals("123"))
                {
                    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    intent.putExtra("uname",s1);
                    intent.putExtra("upass",s2);
                    startActivity(intent);
                }
                else
                {
                    Toast.makeText(MainActivity.this,"Wrong User Name Or Password",Toast
                            .LENGTH_SHORT).show();
                }
            }
        });
    }
}

After that, we have created new Activity. 

  • For this process go to resource folder and use right click.Afer that select New-->Activity-->>Empty Activity.
  • Here we will see a new activity in our project.
  • After That, we have to create this activity view.
  • After That, we have to create a view for this activity.
  • Can be shown to anyone who gets data.

activity_main2.XML Page Codes:-

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="waytofeed.wapptech.com.how_we_can_transfer_data_from_one_activity_to_another.Main2Activity">

    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text1"/>
    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text2"/>

</LinearLayout>

After That, We Have To Select Our MainActivity2.java Page.

  • Here we have to make object TextView.
TextView textView1,textView2;
  • After that, we have to create reference this TextView.
  • After that, we have to create reference this TextView.
textView1=findViewById(R.id.text1);
textView2=findViewById(R.id.text2);
  •  After that, the data is to be fetched.
  • To use the data, we have to use the bundle.
bundle=getIntent().getExtras();
String s1=bundle.getString("uname");
String s2=bundle.getString("upass");
  • After that, we have to set this data to TextView.
textView1.setText(s1);
textView2.setText(s2);

All MainActivity2.java Codes:- 

package waytofeed.wapptech.com.how_we_can_transfer_data_from_one_activity_to_another;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    TextView textView1,textView2;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main2);

        textView1=findViewById(R.id.text1);
        textView2=findViewById(R.id.text2);

        bundle=getIntent().getExtras();
        String s1=bundle.getString("uname");
        String s2=bundle.getString("upass");

        textView1.setText(s1);
        textView2.setText(s2);
    }
}
About the author
Code solution

info@codesolution.co.in

Discussion
  • 0 comments

Add comment To Login
Add comment