Exercise 7 - Intent
Exercise 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;
@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;
@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
});
if (getIntent()!=null) {
String data = getIntent().getStringExtra("MESSAGE");
tvShow.setText("Hello " + data);
}
}
}