Exp 8 Hariom

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

1)

MainActivity.java
Name hariom 12112211 lab 8
packagecom.example.lab1;

importandroid.os.Bundle;import
android.view.View;importandroi
d.content.Intent;importandroid
.widget.Toolbar;

importandroidx.appcompat.app.AppCompatActivity;public
classMainActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedIn
stanceState);setContentView(R.layout.activity_main);
});
}
}

SecondActivity.java
importandroid.os.Bundle;

importandroidx.appcompat.app.AppCompatActivity;

publicclassSecondActivityextendsAppCompatActivity{@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedIn
stanceState);setContentView(R.layout.activity_second);
}
}
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"x
mlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"android:layout_
height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"android:paddingL
eft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/act
ivity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_ma
rgin"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"android:lay
out_height="match_parent"android:text="New
Activity"
>

</RelativeLayout>

activity_second.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"x
mlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"android:layout_
height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"android:paddingL
eft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/act
ivity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_ma
rgin"
tools:context=".SecondActivity">

<TextView
android:layout_width="match_parent"android:lay
out_height="match_parent"android:text="New
Activity"
>
</RelativeLayout>

AndroidManifest.xml
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"xmlns:t
ools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"android:ful
lBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"android:labe
l="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="t
rue"
android:theme="@style/Theme.LAB1"tools:targetA
pi="31">
<activity
android:name=".TabbedActivity"android:expo
rted="false"
android:label="@string/title_activity_tabbed"
android:theme="@style/Theme.LAB1.NoActionBar"/>
<activity
android:name=".MainActivity"android:export
ed="true"
android:label="@string/app_name"android:theme="@style/AppTheme"
>
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<!-- <activityandroid:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity> -->
</application>

</manifest>

2)
MainActivity.java
packagecom.example.lab1;

importandroid.os.Bundle;import
android.view.View;importandroi
d.content.Intent;importandroid
.widget.Toolbar;

importandroidx.appcompat.app.AppCompatActivity;
public classMainActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedIn
stanceState);setContentView(R.layout.activity_main);

ButtonswitchActivityButton=findViewById(R.id.switchActivityButton)
;
switchActivityButton.setOnClickListener(newView.OnClickListener()
{
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(MainActivity.this,SecondActivity.cl
ass);
startActivity(intent);
}
});
}
}

SecondActivity.java
importandroid.os.Bundle;

importandroidx.appcompat.app.AppCompatActivity;

publicclassSecondActivityextendsAppCompatActivity{@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedIn
stanceState);setContentView(R.layout.activity_second);
}
}

activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"x
mlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"android:layout_
height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"android:paddingL
eft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/act
ivity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_ma
rgin"
tools:context=".MainActivity">

<Button
android:id="@+id/switchActivityButton"android:layout_width="wrap_co
ntent"
android:layout_height="wrap_content"
android:text="Switch to SecondActivity"
android:layout_centerInParent="true"/>

</RelativeLayout>

activity_second.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"x
mlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"android:layout_
height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"android:paddingL
eft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/act
ivity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_ma
rgin"
tools:context=".SecondActivity">

<TextView
android:layout_width="match_parent"android:lay
out_height="match_parent"android:text="New
Activity"
>

</RelativeLayout>
3)
MainActivity.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditTex
t;
importandroid.widget.Toast;

importandroidx.appcompat.app.AppCompatActivity;

publicclassMainActivityextendsAppCompatActivity{

privateEditTextusernameEditText;
privateEditTextpasswordEditText;

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_main);
usernameEditText=findViewById(R.id.usernameEditText);pa
sswordEditText=findViewById(R.id.passwordEditText);Butt
on loginButton = findViewById(R.id.loginButton);

loginButton.setOnClickListener(newView.OnClickListener
(){@Override
publicvoidonClick(Viewv){if
(isValidLogin()) {
openNewActivity();
}else{
Toast.makeText(MainActivity.this,"Invalidlogin
credentials",Toast.LENGTH_SHORT).show();
}
}
});
}

privatebooleanisValidLogin(){
Stringusername=usernameEditText.getText().toString().trim
();Stringpassword=passwordEditText.getText().toString().t
rim(); returnusername.equals("Aryan") &&
password.equals("admin");
}

privatevoidopenNewActivity(){
Intentintent=newIntent(this,WelcomeActivity.class);startActivity
(intent);
WelcomeActivity.java
}
packagecom.example.LAB;
}

importandroid.os.Bundle;

importandroidx.appcompat.app.AppCompatActivity;

publicclassWelcomeActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_welcome);
}
}

activity_main.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"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"/>

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/usernameEditText"
android:layout_marginTop="8dp"
android:inputType="textPassword"
android:hint="Password"/>

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/passwordEditText"
android:layout_marginTop="16dp"
android:text="Login"/>
</RelativeLayout>

activity_welcome.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools:context=".WelcomeActi
vity">

<TextView
android:layout_width="wrap_content"android:la
yout_height="wrap_content"android:text="Welco
me to the
App!"android:textSize="18sp"android:layout_ce
nterInParent="true"/>
</RelativeLayout>
4)
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditTex
t;
importandroid.widget.Toast;

importandroidx.appcompat.app.AppCompatActivity;

publicclassMainActivityextendsAppCompatActivity{

privateEditTextfirstNameEditText;
privateEditTextlastNameEditText;
privateEditTextemailEditText;

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_main);
firstNameEditText = findViewById(R.id.firstNameEditText);
lastNameEditText = findViewById(R.id.lastNameEditText);
emailEditText = findViewById(R.id.emailEditText);
Button nextPageButton = findViewById(R.id.nextPageButton);

nextPageButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (areFieldsFilled()) {
openNextPage();
} else {
Toast.makeText(MainActivity.this, "Please fill in all
fields", Toast.LENGTH_SHORT).show();
}
}
});
}

private boolean areFieldsFilled() {


return !firstNameEditText.getText().toString().trim().isEmpty() &&
!lastNameEditText.getText().toString().trim().isEmpty() &&
!emailEditText.getText().toString().trim().isEmpty();
}

private void openNextPage() {


Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("firstName",
firstNameEditText.getText().toString().trim());
intent.putExtra("lastName",
lastNameEditText.getText().toString().trim());
intent.putExtra("email", emailEditText.getText().toString().trim());
startActivity(intent);
}
}

packagecom.example.LAB;

importandroid.os.Bundle;
importandroid.widget.TextVie
w;

importandroidx.appcompat.app.AppCompatActivity;pu

blicclassSecondActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_second);
TextViewfirstNameTextView=findViewById(R.id.firstNameTextView);TextVie
w lastNameTextView = findViewById(R.id.lastNameTextView);TextView
emailTextView = findViewById(R.id.emailTextView);

//RetrievedatafromtheIntent
StringfirstName=getIntent().getStringExtra("firstName"
);String lastName =
getIntent().getStringExtra("lastName");String email =
getIntent().getStringExtra("email");

// Display the data on the second


activityfirstNameTextView.setText("FirstName:"+firstName);lastNa
meTextView.setText("Last Name: "+
lastName);emailTextView.setText("Email: "+ email);
}
}
<?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"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/firstNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name"/>

<EditText
android:id="@+id/lastNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstNameEditText"
android:layout_marginTop="8dp"
android:hint="Last Name"/>

<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lastNameEditText"
android:layout_marginTop="8dp"
android:inputType="textEmailAddress"
android:hint="Email"/>

<Button
android:id="@+id/nextPageButton"android:la
yout_width="match_parent"android:layout_he
ight="wrap_content"android:layout_below="@
id/emailEditText"android:layout_marginTop=
"16dp"android:text="Next Page"/>
</RelativeLayout>

<?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"
android:padding="16dp"
tools:context=".SecondActivity">

<TextView
android:id="@+id/firstNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name: "
android:textSize="18sp"
android:layout_marginTop="16dp"/>

<TextView
android:id="@+id/lastNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firstNameTextView"
android:layout_marginTop="8dp"
android:text="Last Name: "
android:textSize="18sp"/>

<TextView
android:id="@+id/emailTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lastNameTextView"
android:layout_marginTop="8dp"
android:text="Email: "
android:textSize="18sp"/>
</RelativeLayout>
5)
MainActivity.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassMainActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_main);

Button goToBButton =
findViewById(R.id.goToBButton);goToBButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
Intentintent=newIntent(MainActivity.this,ActivityB.class)
;intent.putExtra("messageAtoB","Hello from Activity
A!");startActivity(intent);
}
});
}
}

ActivityB.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextVie
w;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityBextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_b);

ButtongoToCButton=findViewById(R.id.goToCButton);TextV
iew textViewB = findViewById(R.id.textViewB);

StringmessageAtoB=getIntent().getStringExtra("messageAtoB");text
ViewB.setText(messageAtoB);

goToCButton.setOnClickListener(newView.OnClickListener
(){@Override
publicvoidonClick(Viewview){
//Passkey-valuepairstothenextactivity
Intentintent=newIntent(ActivityB.this,ActivityC.class);st
artActivity(intent);
}
});
}
ActivityC.java
}
packagecom.example.LAB;

importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button
;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityCextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_c);

Button goToDButton =
findViewById(R.id.goToDButton);goToDButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
startActivity(newIntent(ActivityC.this,ActivityD.class));
}
});
ActivityD.java
}
}
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityDextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_d);

Button goToAButton =
findViewById(R.id.goToAButton);goToAButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
startActivity(newIntent(ActivityD.this,MainActivity.class
));
}
});
}
}

activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools:context=".MainActi
vity">

<Button
android:id="@+id/goToBButton"android:layou
t_width="match_parent"android:layout_heigh
t="wrap_content"android:layout_marginTop="
16dp"android:text="Go to Activity B"/>
</RelativeLayout>

activity_b.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools
:context=".ActivityB">

<TextView
android:id="@+id/textViewB"android:layout_wid
th="match_parent"android:layout_height="match
_parent"android:layout_marginTop="60dp"androi
d:textSize="18sp"/>
<Button
android:layout_width="match_parent"android:la
yout_height="wrap_content"android:text="Go To
Activity C"android:id="@+id/goToCButton"
/>
</RelativeLayout>

activity_c.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent
"android:padding="16dp"tools:contex
t=".ActivityC">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content
"android:id="@+id/goToDButton"andro
id:text="Go To Activity D"
/>
</RelativeLayout>

activity_d.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools
:context=".ActivityD">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content
"android:id="@+id/goToAButton"andro
id:text="Go To Activity A"
/>
</RelativeLayout>
6)
MainActivity.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassMainActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_main);

Button goToBButton =
findViewById(R.id.goToBButton);goToBButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
Intentintent=newIntent(MainActivity.this,ActivityB.class)
;intent.putExtra("messageAtoB","Hello from Activity
A!");startActivity(intent);
}
});
}
}
ActivityB.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextVie
w;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityBextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_b);
ButtongoToCButton=findViewById(R.id.goToCButton);TextV
iew textViewB = findViewById(R.id.textViewB);

StringmessageAtoB=getIntent().getStringExtra("messageAtoB");text
ViewB.setText(messageAtoB);

goToCButton.setOnClickListener(newView.OnClickListener
(){@Override
publicvoidonClick(Viewview){
//Passkey-valuepairstothenextactivity
Intentintent=newIntent(ActivityB.this,ActivityC.class);st
artActivity(intent);
}
});
}
}

ActivityC.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityCextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_c);

Button goToDButton =
findViewById(R.id.goToDButton);goToDButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
startActivity(newIntent(ActivityC.this,ActivityD.class));
}
});
}
ActivityD.java
}
package com.example.LAB;
importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityDextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_d);

Button goToAButton =
findViewById(R.id.goToAButton);goToAButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
startActivity(newIntent(ActivityD.this,MainActivity.class
));
}
});
activity_main.xml
}
}
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools:context=".MainActi
vity">

<Button
android:id="@+id/goToBButton"android:layou
t_width="match_parent"android:layout_heigh
t="wrap_content"android:layout_marginTop="
16dp"android:text="Go to Activity B"/>
</RelativeLayout>

activity_b.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/andro
id"xmlns:tools="http://schemas.android.com/tools"android:layout_width
="match_parent"
android:layout_height="match_parent
"android:padding="16dp"tools:contex
t=".ActivityB">

<TextView
android:id="@+id/textViewB"android:layout_wid
th="match_parent"android:layout_height="match
_parent"android:layout_marginTop="60dp"androi
d:textSize="18sp"/>
<Button
android:layout_width="match_parent"android:la
yout_height="wrap_content"android:text="Go To
Activity C"android:id="@+id/goToCButton"
/>
</RelativeLayout>

activity_c.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools
:context=".ActivityC">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content
"android:id="@+id/goToDButton"andro
id:text="Go To Activity D"
/>
</RelativeLayout>

activity_d.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools
:context=".ActivityD">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content
"android:id="@+id/goToAButton"andro
id:text="Go To Activity A"
/>
</RelativeLayout>

7)
MainActivity.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassMainActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_main);

Button goToBButton =
findViewById(R.id.goToBButton);goToBButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
Intentintent=newIntent(MainActivity.this,ActivityB.class)
;intent.putExtra("messageAtoB","Hello from Activity
A!");startActivity(intent);
}
});
}
}

ActivityB.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextVie
w;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityBextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_b);

ButtongoToCButton=findViewById(R.id.goToCButton);TextV
iew textViewB = findViewById(R.id.textViewB);

String messageAtoB =
getIntent().getStringExtra("messageAtoB");StringmessageFrom
C=getIntent().getStringExtra("messageCtoB"); if(messageAtoB
!=null)
textViewB.setText(messageAtoB);
else if(messageFromC !=null)
textViewB.setText(messageFromC);

goToCButton.setOnClickListener(newView.OnClickListener
(){@Override
publicvoidonClick(Viewview){
//Passkey-valuepairstothenextactivity
Intentintent=newIntent(ActivityB.this,ActivityC.class);st
artActivity(intent);
}
});
}
}
ActivityC.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityCextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_c);

ButtongoToDButton=findViewById(R.id.goToDButton);
ButtongoBackToBButton=findViewById(R.id.goBackToBButton);goToDBu
tton.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(Viewview){
startActivity(newIntent(ActivityC.this,ActivityD.class));
}
});
goBackToBButton.setOnClickListener(newView.OnClickListener(){@Ov
erride
publicvoidonClick(Viewview){
Intent intent
=newIntent(ActivityC.this,ActivityB.class);intent.putExtr
a("messageCtoB","GoingbackfromActivityC!");startActivity(
intent);
}
});
}
ActivityD.java
}
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;

importandroidx.appcompat.app.AppCompatActivity;

publicclassActivityDextendsAppCompatActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);setContent
View(R.layout.activity_d);

Button goToAButton =
findViewById(R.id.goToAButton);goToAButton.setOnClickListener(ne
wView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
startActivity(newIntent(ActivityD.this,MainActivity.class
));
}
});
}
activity_main.xml
}
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools:context=".MainActi
vity">

<Button
android:id="@+id/goToBButton"android:layou
t_width="match_parent"android:layout_heigh
t="wrap_content"android:layout_marginTop="
16dp"android:text="Go to Activity B"/>
</RelativeLayout>

activity_b.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools
:context=".ActivityB">

<TextView
android:id="@+id/textViewB"android:layout_wid
th="match_parent"android:layout_height="match
_parent"android:layout_marginTop="60dp"
android:textSize="18sp"/>
<Button
android:layout_width="match_parent"android:la
yout_height="wrap_content"android:text="Go To
Activity C"android:id="@+id/goToCButton"
/>
</RelativeLayout>

activity_c.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools
:context=".ActivityC">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content
"android:id="@+id/goToDButton"andro
id:text="Go To Activity D"
/>
</RelativeLayout>

activity_d.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/an
droid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:padding="16dp"tools
:context=".ActivityD">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content
"android:id="@+id/goToAButton"andro
id:text="Go To Activity A"
/>
</RelativeLayout>
8)
MainActivity.java
package com.example.LAB;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText;


private EditText emailEditText;
private EditText ageEditText;

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

nameEditText = findViewById(R.id.nameEditText);
emailEditText = findViewById(R.id.emailEditText);
ageEditText = findViewById(R.id.ageEditText);
Button submitButton = findViewById(R.id.submitButton);

submitButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
// Retrieve entered values
String name = nameEditText.getText().toString().trim();
String email = emailEditText.getText().toString().trim();
String age = ageEditText.getText().toString().trim();

// Pass values to the next activity


Intent intent = new Intent(MainActivity.this,
DisplayActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("age", age);
startActivity(intent);
}
});
}
}
DisplayActivity.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.widget.TextVie
w;

importandroidx.appcompat.app.AppCompatActivity;

publicclassDisplayActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_display);

//RetrievevaluesfromtheintentIn
tent intent = getIntent();
String name =
intent.getStringExtra("name");Stringemail=int
ent.getStringExtra("email");String age =
intent.getStringExtra("age");

//DisplaythevaluesinTextViews
TextView nameTextView =
findViewById(R.id.nameTextView);TextViewemailTextView=findV
iewById(R.id.emailTextView);TextView ageTextView =
findViewById(R.id.ageTextView);

nameTextView.setText("Name: "+
name);emailTextView.setText("Email:"+email);a
geTextView.setText("Age: "+ age);
activity_main.xml
}
}
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/and
roid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:orientation="vertical"android:padding=
"16dp"tools:context=".MainActivity">

<EditText
android:id="@+id/nameEditText"android:layout_
width="match_parent"android:layout_height="wr
ap_content"
android:hint="Name"/>

<EditText
android:id="@+id/emailEditText"android:layout
_width="match_parent"android:layout_height="w
rap_content"android:layout_marginTop="8dp"and
roid:inputType="textEmailAddress"android:hint
="Email"/>

<EditText
android:id="@+id/ageEditText"androi
d:layout_width="match_parent"androi
d:layout_height="wrap_content"andro
id:layout_marginTop="8dp"android:in
putType="number"android:hint="Age"/
>

<Button
android:id="@+id/submitButton"android:layout_
width="match_parent"android:layout_height="wr
ap_content"android:layout_marginTop="16dp"and
roid:text="Submit"/>
</LinearLayout>

activity_display.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/and
roid"xmlns:tools="http://schemas.android.com/tools"android:layou
t_width="match_parent"android:layout_height="match_parent"
android:orientation="vertical"android:padding=
"16dp"tools:context=".DisplayActivity">

<TextView
android:id="@+id/nameTextView"android:layout_
width="wrap_content"android:layout_height="wr
ap_content"android:text="Name:"/>

<TextView
android:id="@+id/emailTextView"android:layout
_width="wrap_content"android:layout_height="w
rap_content"android:layout_marginTop="8dp"and
roid:text="Email:"/>
<TextView
android:id="@+id/ageTextView"android:layout_w
idth="wrap_content"android:layout_height="wra
p_content"android:layout_marginTop="8dp"andro
id:text="Age:"/>
</LinearLayout>

9)
MainActivity.java
packagecom.example.LAB;

importandroid.content.Inten
t;
importandroid.net.Uri;impor
tandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button
;
importandroid.widget.EditText
; importandroid.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText;


private EditText emailEditText;
private EditText ageEditText;

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

nameEditText = findViewById(R.id.nameEditText);
emailEditText = findViewById(R.id.emailEditText);
ageEditText = findViewById(R.id.ageEditText);

Button submitButton = findViewById(R.id.submitButton);


Button sendEmailButton = findViewById(R.id.sendEmailButton);

submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Retrieve entered values
String name = nameEditText.getText().toString().trim();
String email = emailEditText.getText().toString().trim();
String age = ageEditText.getText().toString().trim();

// Pass values to the DisplayActivity using explicit intent


Intent intent = new Intent(MainActivity.this,
DisplayActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("age", age);
startActivity(intent);
}
});

sendEmailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Retrieve entered email value
String email = emailEditText.getText().toString().trim();

// Launch an email application using implicit intent


Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body of the email");
if(emailIntent.resolveActivity(getPackageManager())!=null
){startActivity(emailIntent);
}else{
Toast.makeText(MainActivity.this,"Noemailappfound",Toast.LE
NGTH_SHORT).show();
}
}
});
}
}

DisplayActivity.java
packagecom.example.LAB;

importandroid.content.Intent
; importandroid.os.Bundle;
importandroid.widget.TextVie
w;

importandroidx.appcompat.app.AppCompatActivity;

publicclassDisplayActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(saved
InstanceState);setContentView(R.layout.activity_display);

//RetrievevaluesfromtheintentIn
tent intent = getIntent();
String name =
intent.getStringExtra("name");Stringemail=int
ent.getStringExtra("email");String age =
intent.getStringExtra("age");

//DisplaythevaluesinTextViews
TextView nameTextView =
findViewById(R.id.nameTextView);TextViewemailTextView=findV
iewById(R.id.emailTextView);TextView ageTextView =
findViewById(R.id.ageTextView);

nameTextView.setText("Name: "+
name);emailTextView.setText("Email:"+email);a
geTextView.setText("Age: "+ age);
activity_main.xml
}
}
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"android:lay
out_height="match_parent"android:orientation="
vertical"android:padding="16dp"tools:context="
.MainActivity">

<EditText
android:id="@+id/nameEditText"android:layout_
width="match_parent"android:layout_height="wr
ap_content"android:hint="Name"/>

<EditText
android:id="@+id/emailEditText"android:layout
_width="match_parent"android:layout_height="w
rap_content"android:layout_marginTop="8dp"and
roid:inputType="textEmailAddress"android:hint
="Email"/>

<EditText
android:id="@+id/ageEditText"androi
d:layout_width="match_parent"androi
d:layout_height="wrap_content"andro
id:layout_marginTop="8dp"android:in
putType="number"android:hint="Age"/
>

<Button
android:id="@+id/submitButton"android:layout_
width="match_parent"android:layout_height="wr
ap_content"android:layout_marginTop="16dp"and
roid:text="Submit"/>
<Button
android:id="@+id/sendEmailButton"android:l
ayout_width="match_parent"android:layout_h
eight="wrap_content"android:layout_marginT
op="16dp"android:text="Send Email"/>
</LinearLayout>

activity_display.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".DisplayActivity">

<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"/>

<TextView
android:id="@+id/emailTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Email:"/>

<TextView
android:id="@+id/ageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Age:"/>
</LinearLayout>

You might also like