T.Y.BSC(I.
T) SEM-VI Maharashtra College Advanced Mobile Programming
Question
Create an android application to pass the data from current application to another
Application using intent.
OR
Create an android application that shares data between different applications using intents
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Input your Name"></TextView>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:stretchColumns="1">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
Prof. Ansari Mohd. Shahid( 7977-079-345) EMAIL-ID : [email protected]
T.Y.BSC(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
android:layout_height="wrap_content"
android:text="First Name"></TextView>
<EditText
android:id="@+id/etFName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
<requestFocus></requestFocus>
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last Name"></TextView>
<EditText
android:id="@+id/etLName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"></EditText>
</TableRow>
</TableLayout>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Submit"></Button>
</LinearLayout>
</RelativeLayout>
Prof. Ansari Mohd. Shahid( 7977-079-345) EMAIL-ID : [email protected]
T.Y.BSC(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
Main_Activity.java
package com.maharashtracollege.profshahidansari;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
EditText etFName;
EditText etLName;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFName = (EditText) findViewById(R.id.etFName);
etLName = (EditText) findViewById(R.id.etLName);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ViewActivity.class);
intent.putExtra("fname", etFName.getText().toString());
intent.putExtra("lname", etLName.getText().toString());
startActivity(intent);
}
});
}}
Prof. Ansari Mohd. Shahid( 7977-079-345) EMAIL-ID : [email protected]
T.Y.BSC(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
Now Create an another activity as ViewActivity.java as
Prof. Ansari Mohd. Shahid( 7977-079-345) EMAIL-ID : [email protected]
T.Y.BSC(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
Activity_main.xml(Second Activity 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=".ViewActivity">
<TextView
android:id="@+id/tvView"
android:layout_width="400dp"
android:layout_height="85dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Output"
android:textColor="@android:color/holo_blue_dark"
android:textSize="24sp"
android:textStyle="bold"></TextView>
</LinearLayout>
Prof. Ansari Mohd. Shahid( 7977-079-345) EMAIL-ID : [email protected]
T.Y.BSC(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
ViewActivity.java
package com.maharashtracollege.profshahidansari;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ViewActivity extends AppCompatActivity {
TextView tvView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
tvView = (TextView) findViewById(R.id.tvView);
Intent intent = getIntent();
String fName = intent.getStringExtra("fname");
String lName = intent.getStringExtra("lname");
tvView.setText("Your name is: " + fName + " " + lName);
}
}
Prof. Ansari Mohd. Shahid( 7977-079-345) EMAIL-ID : [email protected]
T.Y.BSC(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
Prof. Ansari Mohd. Shahid( 7977-079-345) EMAIL-ID : [email protected]