Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Experiment 2.1

Student Name: Akhilesh Singh UID: 21BCS3604


Branch: CSE Section/Group: 21BCS_CC-644-B
Semester: 6th Date of Performance: 12/02/2024
Subject Name: MAD Lab Subject Code: 21CSH-355

1. Aim:
Create an Android app that uses Intent with button to create a page and passes
values from one activity to another.

2. Objective:
The objective of an Android app that uses Intent with a button to create a page and
passes values from one activity to another could be to demonstrate and implement a
simple data communication flow between different activities within an Android
application. This type of app is commonly used to understand and showcase the
concept of passing data between different screens or pages in Android.

3. Procedure:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a
New Project in Android Studio. We are implementing it for both Java and Kotlin
languages.

Step 2: Add one more Empty View Activity to the Project


Right-Click on the app, move the cursor to new, find the “Activity->Empty View
Activity” option.

Step 3: Install and Run the Code


Install and run the code on Android Virtual Device (AVD) or a personal device.
Send Message to intent using Send button and come back to main screen using back
button on intent screen
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
a) New→Activity→Empty View Activity

b) Assign Class Name to New Android Activity

Step 4: Write code in file


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Make changes in Main Activity.java Main Activity.xml Second Activity.java Second
Activity.xml

Main Activity.java
package com.example.tryintent3;

import static com.example.tryintent3.R.id.editTextMessage;


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
editTextMessage; private Button
buttonSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); editTextMessage
= findViewById(R.id.editTextMessage); buttonSend =
findViewById(R.id.buttonSend);

buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = editTextMessage.getText().toString();
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtra("MESSAGE", message);
startActivity(intent);
}
});
}
}

Second Activity.java
package com.example.tryintent3;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
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;

public class SecondActivity extends AppCompatActivity {


private TextView
textViewMessage; private Button
buttonBack;

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

textViewMessage = findViewById(R.id.textViewMessage);
buttonBack = findViewById(R.id.buttonBack);

// Retrieve message from Intent and display it


Intent intent = getIntent();
if (intent != null && intent.hasExtra("MESSAGE")) {
String message = intent.getStringExtra("MESSAGE");
textViewMessage.setText(message);
}

// Set OnClickListener for the Back button


buttonBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Navigate back to MainActivity
onBackPressed();
}
});
}
}

Main Activity.xlm

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">

<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="Enter your message" />

<Button
android:id="@+id/buttonSend"
android:layout_width="161dp"
android:layout_height="64dp"
android:layout_below="@id/editTextMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Send Message"
android:background="#00FF00"/>
</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=".SecondActivity">

<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Received Message"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:layout_below="@id/textViewMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
</RelativeLayout>
4.Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5.Observation /Outcome:
• Learnt about Intent in Android Development.
• Learnt about Button in Android Development.
• Learnt about Intent and Button Functionality and how to configure it.
• Learnt to build intent and button in Android device.

6.Learning Outcome:
Successfully setup Android Studio ,and creating my first intent and button in
Android studio.

You might also like