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

Assignment 02 SET A Program 02 Done

This document describes how to create a simple Android application with two activities that can send a message between each other using an intent. The main activity contains a button that, when clicked, launches a new intent to start the second activity. The second activity receives the implicit intent from the first activity but does not display any additional message.

Uploaded by

Abhay Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Assignment 02 SET A Program 02 Done

This document describes how to create a simple Android application with two activities that can send a message between each other using an intent. The main activity contains a button that, when clicked, launches a new intent to start the second activity. The second activity receives the implicit intent from the first activity but does not display any additional message.

Uploaded by

Abhay Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Create a Simple Application Which Send ―Hello‖ message from one

activity to another with help of Button (Use Intent).

MAIN JAVA ACTIVITY


package com.example.my_intent;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


Button button;

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

private void addListenerOnButton()


{
final Context context = this;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override

public void onClick(View view)


{
Intent intent = new Intent(context, second_activity.class);
startActivity(intent);
}
});
}
}

SECOND JAVA ACTIVITY


package com.example.my_intent;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class second_activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Main XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@android:dimen/app_icon_size"
android:layout_marginRight="@android:dimen/app_icon_size"
android:layout_margin="@android:dimen/app_icon_size"
android:text="@string/app_name" />

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@android:dimen/app_icon_size"
android:layout_marginRight="@android:dimen/app_icon_size"
android:layout_margin="@android:dimen/app_icon_size"
android:text="@string/app_name" />

</LinearLayout>

Activity Second

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


<LinearLayout 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_activity"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@android:dimen/app_icon_size"
android:layout_marginRight="@android:dimen/app_icon_size"
android:layout_margin="@android:dimen/app_icon_size"
android:text="@string/app_name" />

</LinearLayout>

You might also like