0% found this document useful (0 votes)
15 views6 pages

Linking Activity Using Intents

The document outlines the steps to create an Android application named 'LinkingActivity' with two activities: MainActivity and SecondActivity. MainActivity features two buttons for explicit and implicit intents, allowing navigation to SecondActivity and opening a web page, respectively. SecondActivity includes a button to return to MainActivity, demonstrating basic activity linking in Android development.

Uploaded by

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

Linking Activity Using Intents

The document outlines the steps to create an Android application named 'LinkingActivity' with two activities: MainActivity and SecondActivity. MainActivity features two buttons for explicit and implicit intents, allowing navigation to SecondActivity and opening a web page, respectively. SecondActivity includes a button to return to MainActivity, demonstrating basic activity linking in Android development.

Uploaded by

anjukadamati03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LINKING ACTIVITY USING INTENTS :

 File -> new -> new project


 Select empty views activity
 Create application name as LinkingActivity
 Make sure that select java language

On activity_main.xml page im adding one text view and two set of buttons

Activity_main.xml:

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


<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.141" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.329" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implicit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java :

 package com.example.linkingactivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
Button button1=findViewById(R.id.button2);
button.setOnClickListener(v->{
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
});
button1.setOnClickListener(v->{
Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
startActivity(i);
});
}

 After completing home page go to App -> New -> Activity -> Empty Views Activity

 Give activity name as SecondActivity

 In second activity we are going to add a one text view and one button
Activity_second.xml :

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


<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.214" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Second Activity.java :

package com.example.linkingactivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_second);
Button button=findViewById(R.id.button3);
button.setOnClickListener(v->{
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
});
}
}

Output :

 If you click on Explicit button it will move to next page


 If you click on Implicit button it will move to next application

You might also like