Practical 7

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

Name: suyash munde Roll no.

47

Practical 7

Programs on Intents, Events, Listeners and Adapters


The Android Intent Class, Using Events and Event Listeners

Create a new project and go to, activity_main.xml, and make the following additions to the
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">

<EditText
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="152dp"
android:textSize="30dp"
android:ems="10"
android:hint="Enter Data"
android:inputType="textPersonName" />

<Button
android:id="@+id/btn1"
android:layout_width="200dp"
android:layout_height="80dp"
android:textSize="30dp"
android:layout_centerInParent="true"
android:text="Send" />

</RelativeLayout>

1
Name: suyash munde Roll no.47

Main_Activity.java

package com.example.prac_7;

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.os.Bundle;

public class MainActivity extends AppCompatActivity {


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

t1=(EditText)findViewById(R.id.txt1);
b1=(Button)findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data=t1.getText().toString();
Intent intent=new
Intent(getApplicationContext(),DataRecieve.class);
intent.putExtra("data",data);
startActivity(intent);
}
});
}
}

Now create another activity Activity_data_receive.xml

Activity_data_receive.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=".DataRecieve">

<TextView android:id="@+id/tv1"

2
Name: suyash munde Roll no.47

android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:layout_marginTop="161dp"
android:layout_marginEnd="132dp"
android:layout_marginRight="132dp" />

</RelativeLayout>

Data_Receive.java

package com.example.prac_7;

import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.os.Bundle;
import android.content.Intent;
public class DataRecieve extends AppCompatActivity {

TextView t1;

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

t1=(TextView)findViewById(R.id.tv1);
Intent intent=getIntent();
String data=intent.getStringExtra("data");
t1.setText(data);

}
}

3
Name: suyash munde Roll no.47

Output

You might also like