0% found this document useful (0 votes)
98 views

Practical 7

This document describes using intents, events, listeners and adapters in Android. It provides code for an activity_main.xml layout with an EditText and Button. The MainActivity.java code defines an onclick listener for the button that sends an intent with input text to a DataRecieve activity. The Activity_data_receive.xml layout includes a TextView. The DataRecieve.java code receives the intent extra and sets the textview text to the input.

Uploaded by

jenni koko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Practical 7

This document describes using intents, events, listeners and adapters in Android. It provides code for an activity_main.xml layout with an EditText and Button. The MainActivity.java code defines an onclick listener for the button that sends an intent with input text to a DataRecieve activity. The Activity_data_receive.xml layout includes a TextView. The DataRecieve.java code receives the intent extra and sets the textview text to the input.

Uploaded by

jenni koko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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