Android tutorial for beginners part 12, Intent with data

  • 23 April 20
  • Posted By : Admin
  • 56 Hits
  • 10 Comment

we will learn how to intent with data from one activity to another activity

}

Java code

intent with data insert below code to MainActivity.java

package com.salam.intentwithdata; 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 { public Button btn; public EditText mName,mEmail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = findViewById(R.id.btnSubmit); mName = findViewById(R.id.uName); mEmail = findViewById(R.id.uEmail); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String nName = mName.getText().toString(); String nEmail = mEmail.getText().toString(); Intent intent = new Intent(MainActivity.this,SinglePage.class); intent.putExtra("eName",nName); intent.putExtra("eEmail",nEmail); startActivity(intent); } }); } }

Java code

Create another activity name

SinglePage.java

package com.salam.intentwithdata; 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 SinglePage extends AppCompatActivity { public Button btn; public TextView nName, nEmail ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_single_page); btn = findViewById(R.id.backtoMain); nName = findViewById(R.id.gName); nEmail = findViewById(R.id.gEmail); if(getIntent().hasExtra("eName") && getIntent().hasExtra("eEmail")){ String mName = getIntent().getStringExtra("eName"); String mEmail = getIntent().getStringExtra("eEmail"); nName.setText(mName); nEmail.setText(mEmail); } btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(SinglePage.this,MainActivity.class); startActivity(intent); } }); } }

Create MainActivity.xml file

if you already have MainActivity.xml file just paste below code to the xml file

<?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=".MainActivity" android:gravity="center" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Main Activity" android:textSize="30sp" android:textColor="#f00" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/uName" android:layout_marginTop="20dp" android:hint="Username"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/uEmail" android:layout_marginTop="20dp" android:hint="User Email"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnSubmit" android:layout_marginTop="20dp" android:text="Submit"/> </LinearLayout>

Create another xml file

name activity_singe_page.xml file

paste the below code on that

<?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=".SinglePage" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Single Page" android:textSize="30sp" android:textColor="#00ff00"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/gName" android:layout_marginTop="20dp" android:textSize="24sp" android:text="Name"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/gEmail" android:layout_marginTop="5dp" android:textSize="24sp" android:text="Email"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back to main" android:id="@+id/backtoMain" android:layout_marginTop="20dp"/> </LinearLayout>