LAB 8 Retrive Data Using Intent
LAB 8 Retrive Data Using Intent
EXPERIMENT 8
Retrieve data using Intent
AIM: To design application using intent.
ASSIGNMENT
1.Create an application with Edit Text and Button in it. On Click of the button the
application should read the name entered in the Edit Text and display it on the text
view of the second activity. Add a Button on the second activity. On Click of the
button the application should navigate to first activity.
CODE:
activityMain.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">
<TextView
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Intent First Page"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#F0214D"
android:textStyle="bold"
/>
<EditText
android:id="@+id/txtname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="45dp"
android:hint="Name"
android:textSize="23dp"
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
android:layout_below="@+id/register"
android:layout_margin="15dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
android:layout_marginTop="-5dp"
android:layout_below="@+id/txtname"
android:layout_centerHorizontal="true"
android:textColor="#fff"
android:background="#16D647"
android:textStyle="bold"
/>
</RelativeLayout>
Second_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Second">
<TextView
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Intent Second Page"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#F0214D"
android:textStyle="bold"
/>
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:layout_margin="15dp"
android:textColor="#F0214D"
android:layout_centerInParent="true"
android:id="@+id/t2"
/>
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_below="@+id/t2"
android:layout_centerHorizontal="true"
android:textColor="#fff"
android:background="#16D647"
android:textStyle="bold"
/>
</RelativeLayout>
MainActivity.java
package com.example.intentfilter;
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;
import android.widget.TextView;
TextView text1,text2;
EditText edit1;
Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit1=findViewById(R.id.txtname);
button1=findViewById(R.id.b1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
Second.java
package com.example.intentfilter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
TextView text2;
Button button2;
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);//this line of code connect the
java(second.java) file with the xml(second_activity)
text2 = findViewById(R.id.t2);
button2 = findViewById(R.id.b2);
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.IntentFilter"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".Second">
</activity>
</application>
</manifest>
OUTPUT:
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
2. Create a registration form using Intent. On click of the button all the values
should be displayed on the second activity.
CODE:
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">
<TextView
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registration"
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#F021fD"
android:textStyle="bold"
/>
<EditText
android:id="@+id/txtname"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:hint="Name"
android:textSize="17dp"
android:layout_below="@+id/register"
android:layout_margin="15dp"
android:textStyle="bold"
/>
<EditText
android:id="@+id/txtmobileno"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:hint="Mobile No "
android:textSize="17dp"
android:layout_below="@+id/txtname"
android:layout_margin="15dp"
android:textStyle="bold"
/>
<EditText
android:id="@+id/txtemail"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:hint="Email Address"
android:textSize="17dp"
android:layout_below="@+id/txtmobileno"
android:textStyle="bold"
android:layout_margin="15dp"
/>
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
<EditText
android:id="@+id/txtcity"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:hint="City"
android:textSize="17dp"
android:layout_below="@+id/txtemail"
android:layout_margin="15dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="25dp"
android:layout_below="@+id/txtcity"
android:layout_centerHorizontal="true"
android:textColor="#fff"
android:background="#16D647"
android:textStyle="bold"
/>
</RelativeLayout>
Second_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Second">
<TextView
android:id="@+id/Landing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Landing Page"
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
android:layout_marginTop="15dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#F051FD"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="175dp"
android:layout_marginTop="25dp"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:textColor="#F0512D"
android:layout_margin="7dp"
android:id="@+id/result"
android:layout_below="@id/Landing"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_below="@+id/result"
android:id="@+id/b2"
/>
</RelativeLayout>
MainActivity.java
package com.example.intentregister;
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;
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
button1=findViewById(R.id.btnsub);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input1 = edit1.getText().toString();
String input2 = edit2.getText().toString();
String input3 = edit3.getText().toString();
String input4 = edit4.getText().toString();
Intent I=new Intent(getApplicationContext(),Second.class);
I.putExtra("Name",input1);
I.putExtra("Mobile",input2);
I.putExtra("Email",input3);
I.putExtra("City",input4);//putExtra method having two attribute in
which name becomes the key which is used in second.java
startActivity(I);
}
});
}
}
Second.java
package com.example.intentregister;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
TextView text1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);//this line of code connect the
java(second.java) file with the xml(second_activity)
text1 = findViewById(R.id.result);
button2 = findViewById(R.id.b2);
text1.setText("Name:"+getIntent().getExtras().getString("Name")+"\nContact:
"+getIntent().getExtras().getString("Mobile")+
"\nEmail: "+getIntent().getExtras().getString("Email")+
"\nCity: "+getIntent().getExtras().getString("City"));
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent I=new Intent(getApplicationContext(),MainActivity.class);
startActivity(I);
}
});
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.IntentRegister"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".Second"></activity>
</application>
</manifest>
OUTPUT:
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
Code:-
activityMain.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">
<TextView
android:id="@+id/books"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select the Books"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#0FECDC"
android:textStyle="bold"
/>
<CheckBox
android:layout_marginTop="20dp"
android:id="@+id/checkBox"
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mathematics"
android:textSize="20dp"
android:layout_marginLeft="120dp"
android:layout_below="@+id/books"/>
<CheckBox
android:layout_below="@+id/checkBox"
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:text="English"
android:textSize="20dp"/>
<CheckBox
android:layout_below="@+id/checkBox2"
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:text="Science"
android:textSize="20dp"/>
<CheckBox
android:layout_below="@+id/checkBox3"
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:text="Hindi"
android:textSize="20dp"/>
<Button
android:id="@+id/btnshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_below="@+id/checkBox4"
android:layout_centerHorizontal="true"
android:textColor="#fff"
android:background="#16D647"
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
android:textStyle="bold"
/>
</RelativeLayout>
Second_Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/books"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Books are"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#D60FEC"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="175dp"
android:layout_marginTop="25dp"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:textColor="#F0512D"
android:layout_margin="7dp"
android:id="@+id/t2"
android:layout_below="@id/books"
/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home Delivery"
android:layout_centerHorizontal="true"
android:id="@+id/s1"
android:textColor="#BA9113"
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
android:layout_marginTop="20dp"
android:layout_below="@+id/t2"
android:textSize="25dp" />
</RelativeLayout>
MainActivity.java
package com.example.checkintent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit=findViewById(R.id.btnshow);
ch1=findViewById(R.id.checkBox);
ch2=findViewById(R.id.checkBox2);
ch3=findViewById(R.id.checkBox3);
ch4=findViewById(R.id.checkBox4);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg = " ";
if(ch1.isChecked())
{
msg += "Mathematics\n";
}
if(ch2.isChecked())
{
msg += "English\n";
}
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
if(ch3.isChecked())
{
msg += "Science\n";
}
if(ch4.isChecked())
{
msg += "Hindi\n";
}
Intent i = new Intent(getApplicationContext(),Second.class);
i.putExtra("Msg",msg);
startActivity(i);
}
});
}
}
Second.java
package com.example.checkintent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
TextView text1;
Switch s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
text1=findViewById(R.id.t2);
s=findViewById(R.id.s1);
String str = getIntent().getExtras().getString("Msg").toString();
text1.setText(str);
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
s.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if(s.isChecked()) {
Toast.makeText(Second.this, "Books will be delivered on Home",
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Second.this, "Sorry!!No home delivery available",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CheckIntent"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".Second"></activity>
</application>
</manifest>
Output:-
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266
Name: Aman Verma MET-Institute of Computer Science
Roll No: 1266