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

Exercise 7 - Intent

The document discusses implementing explicit and implicit intents in Android. It provides code for an activity that uses explicit intent to start a new activity and display a message, and implicit intents to make phone calls and open web URLs. It includes XML layouts and Java code for the main activity and receiving activity.
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)
7 views

Exercise 7 - Intent

The document discusses implementing explicit and implicit intents in Android. It provides code for an activity that uses explicit intent to start a new activity and display a message, and implicit intents to make phone calls and open web URLs. It includes XML layouts and Java code for the main activity and receiving activity.
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/ 7

Intent

Intent Application:

1. Explicit Intent
Input Your name, click button CLICK ME -> open another activity, display Hello + name

2. Implicit Intent
- Click button DIAL, open Dial UI:

1
- Input URL, click button BROWSE URL -> open website:

Solution:
 Project:
- File > New > New project > Empty Views Activity > Click Next

2
- Project name, location, language (java), min SDK, Groovy DSL -> Click Finish

 MainActivity:
- MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextName"
android:text="Your name"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/buttonClickMe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Click me"/>

<Button
android:id="@+id/buttonDial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="DIAL"/>

<EditText
android:id="@+id/editTextURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="https://www.vnexpress.net"
android:textSize="24sp"/>
<Button
android:id="@+id/buttonURL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Browse URL"

3
android:textSize="24sp"/>

</LinearLayout>
- MainActivity.java:
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.content.Intent;
import android.net.Uri;

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 {

public TextView editTextName;


public Button btnClickMe;
public Button btnDial;
public EditText editTextURL;
public Button btnURL;

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

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main
), (v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top,
systemBars.right, systemBars.bottom);
return insets;
});

editTextName = (EditText)
findViewById(R.id.editTextName);
btnClickMe = (Button) findViewById(R.id.buttonClickMe);
btnDial = (Button) findViewById(R.id.buttonDial);

4
btnURL = (Button) findViewById(R.id.buttonURL);
editTextURL = (EditText) findViewById(R.id.editTextURL);

//Explicit Intent
//Input Your name, click button CLICK ME -> open
MainActivity2, display Hello + name
btnClickMe.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
MainActivity2.class);
String yName=editTextName.getText().toString();
intent.putExtra("MESSAGE", yName);
startActivity(intent);
}
});

//Implicit Intent
//Click button DIAL, open Dial UI
btnDial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
});

//Implicit Intent
//Input URL, click button BROWSE URL -> open website
btnURL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=editTextURL.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
}
});
}
}

 MainActivity2:
- MainActivity2.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"

5
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=".MainActivity2">

<TextView
android:id="@+id/tvShow"
android:text="Hello"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

- MainActivity2.java:
import android.os.Bundle;

import android.widget.TextView;

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 MainActivity2 extends AppCompatActivity {


public TextView tvShow;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main2);

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main
), (v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top,
systemBars.right, systemBars.bottom);
return insets;

6
});

tvShow = (TextView) findViewById(R.id.tvShow);

if (getIntent()!=null) {
String data = getIntent().getStringExtra("MESSAGE");
tvShow.setText("Hello " + data);
}
}
}

You might also like