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

Exercise 6 - App Navigation

The document outlines a series of labs focused on Android app navigation, including the use of explicit and implicit intents, option menus, fragments, and communication between activities and fragments. It provides step-by-step instructions for creating activities and fragments, along with XML layouts and Kotlin code examples. Each lab builds on the previous one, demonstrating how to implement various features in an Android application.

Uploaded by

chiendv.23ite
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)
4 views

Exercise 6 - App Navigation

The document outlines a series of labs focused on Android app navigation, including the use of explicit and implicit intents, option menus, fragments, and communication between activities and fragments. It provides step-by-step instructions for creating activities and fragments, along with XML layouts and Kotlin code examples. Each lab builds on the previous one, demonstrating how to implement various features in an Android application.

Uploaded by

chiendv.23ite
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/ 16

App navigation

* Lab 1 : Intent Application:

MainActivity

1. Explicit Intent
Input Your name, click button OPEN ACTIVITY > open another activity, display Hello +
name

1
MainActivity2

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

2
- Input URL, click button OPEN WEB > open website:

Solution:
• Project:

3
- File > New > New project > Empty Views Activity > Click Next
- Project name, location, language (kotlin), min SDK > 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<EditText
android:id="@+id/editTextName"
android:text="Your name"
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:text="Open Activity"/>

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

<EditText
android:id="@+id/editTextURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="https://www.vnexpress.net"/>
<Button
android:id="@+id/buttonURL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open web"/>

</LinearLayout>

- MainActivity.kt:
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

4
lateinit var editTextName: TextView
lateinit var btnClickMe: Button
lateinit var btnDial: Button
lateinit var editTextURL: EditText
lateinit var btnURL: Button

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

editTextName = findViewById<View>(R.id.editTextName) as EditText


btnClickMe = findViewById<View>(R.id.buttonClickMe) as Button
btnDial = findViewById<View>(R.id.buttonDial) as Button
btnURL = findViewById<View>(R.id.buttonURL) as Button
editTextURL = findViewById<View>(R.id.editTextURL) as EditText

btnClickMe.setOnClickListener {
val intent = Intent(this@MainActivity,
MainActivity2::class.java)
val yName = editTextName.text.toString()
intent.putExtra("MESSAGE", yName)
startActivity(intent)
}

btnDial.setOnClickListener {
val intent = Intent(Intent.ACTION_DIAL)
startActivity(intent)
}

btnURL.setOnClickListener {
val url = editTextURL.text.toString()
val intent = 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"
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=".MainActivity2">

<TextView
android:id="@+id/tvShow"
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"

5
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

- MainActivity2.kt:
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity2 : AppCompatActivity() {


lateinit var tvShow: TextView

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)

tvShow = findViewById<View>(R.id.tvShow) as TextView

if (intent != null) {
val data = intent.getStringExtra("MESSAGE")
tvShow.text = "Hello $data"
}
}
}

* Lab 2: Option Menu

6
Solution:
• Project:
- File > New > New project > Empty Views Activity > Click Next
- Project name, location, language (kotlin), min SDK > Click Finish
• Create XML menu resource
- Create a menu folder
- Right click on menu folder > New > Menu Resource File > input: optionmenu

7
- Click optionsmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/message"
android:icon="@android:drawable/ic_menu_send"
app:showAsAction="always"
android:title="Message"/>

<item
android:id="@+id/picture"
android:icon="@android:drawable/ic_menu_gallery"
app:showAsAction="always"
android:title="Picture"/>

<item
android:id="@+id/mode"
android:icon="@android:drawable/ic_menu_call"
app:showAsAction="always"
android:title="Mode"/>

<item android:id="@+id/option_favorites"
android:title="Favorites">
<menu>
<item android:id="@+id/option_favorites1" android:title="Music"/>
<item android:id="@+id/option_favorites2" android:title="Book"/>
</menu>
</item>

<item
android:id="@+id/about"
android:icon="@android:drawable/ic_dialog_info"
app:showAsAction="never|withText"
android:title="About"/>

<item

8
android:id="@+id/exit"
app:showAsAction="never"
android:title="Exit"/>

</menu>

• MainActivity:
- Right click on Code screen > Generate > Override Method >
onCreateOptionsMenu, onOptionsItemSelected

- MainActivity.kt:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {


menuInflater.inflate(R.menu.optionsmenu, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {


when (item.itemId) {
R.id.message -> {
Toast.makeText(

9
getApplicationContext(),
"You clicked Message menu",
Toast.LENGTH_SHORT
).show()
return true
}
R.id.picture -> {
Toast.makeText(
getApplicationContext(),
"You clicked Picture menu",
Toast.LENGTH_SHORT
).show()
return (true)
}
R.id.mode -> {
Toast.makeText(getApplicationContext(), "You clicked Mode
menu", Toast.LENGTH_SHORT)
.show()
return (true)
}
R.id.option_favorites1 -> {
Toast.makeText(getApplicationContext(), "You clicked Music
menu", Toast.LENGTH_SHORT)
.show()
return (true)
}
R.id.option_favorites2 -> {
Toast.makeText(getApplicationContext(), "You clicked Book
menu", Toast.LENGTH_SHORT)
.show()
return (true)
}
R.id.about -> {
Toast.makeText(getApplicationContext(), "You clicked About
menu", Toast.LENGTH_SHORT)
.show()
return (true)
}
R.id.exit -> {
finish()
return (true)
}
}
return (super.onOptionsItemSelected(item))
}
}

* Lab 3: Add 2 Fragments to Activity

10
1. Create Fragment 1:
- Right click on Kolin-java folder > New > Fragment (blank) -> Input: fragmentOne
- Design UI: fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#FFBB86FC"
tools:context=".fragmentOne">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:textSize="30dp"

11
android:layout_gravity="center"/>
</FrameLayout>

- Don’t change anything in fragmentOne.kt


2. Create Fragment 2:
- Right click on Kolin-java folder > New > Fragment (blank) -> Input: fragmentTwo
- Design UI: fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#AA0099"
tools:context=".fragmentTwo">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:textSize="30dp"
android:layout_gravity="center"/>
</FrameLayout>

- Don’t change anything in fragmentTwo.kt


3. Add Fragments to Activity by static way:
MainActivity.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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/listFragment"
class="com.example.fragmentdemo1.fragmentOne"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_one">
</fragment>

<fragment
android:id="@+id/detailFragment"
class="com.example.fragmentdemo1.fragmentTwo"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_two">
</fragment>

</LinearLayout>

12
4. Add 2 Fragments to Activity by dynamic way:
- Add new Activity: MainActivity1
- activity_main1.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity1">

<FrameLayout
android:id="@+id/your_placeholder1"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>

<FrameLayout
android:id="@+id/your_placeholder2"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>

</LinearLayout>

- MainActivity1.kt:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTransaction

class DynamicActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dynamic)

val ft: FragmentTransaction =


supportFragmentManager.beginTransaction()
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder1, fragmentOne())
ft.replace(R.id.your_placeholder2, fragmentTwo())
// Complete the changes added above
ft.commit()
}
}

*Lab 4: Communication between Activity and Fragment

13
1.Add new Fragment: FragmentThree
- fragment_three.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"
android:orientation="vertical"
tools:context=".fragmentThree">

<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text1"/>

<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text2"/>

</LinearLayout>

-fragmentThree.kt:
14
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment

private const val ARG_PARAM1 = "param1"


private const val ARG_PARAM2 = "param2"

class fragmentThree : Fragment() {


private var param1: String? = null
private var param2: String? = null

lateinit var tv1: TextView


lateinit var tv2: TextView

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}

override fun onCreateView(


inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_three, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {


super.onViewCreated(view, savedInstanceState)

tv1 = requireView().findViewById<View>(R.id.tv1) as TextView


tv2 = requireView().findViewById<View>(R.id.tv2) as TextView
tv1.text = param1
tv2.text = param2
}

companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
fragmentThree().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

2.Add new Activity: MainActivity2


- activity_main2.xml:
15
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity2">

<FrameLayout
android:id="@+id/your_placeholder"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</FrameLayout>

</LinearLayout>

-MainActivity2.kt:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTransaction
import com.example.fragment.fragmentThree.Companion.newInstance

class ParamActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_param)

val ft: FragmentTransaction =


supportFragmentManager.beginTransaction()
val fragmentDemo = newInstance("Fragment", "Android")
ft.replace(R.id.your_placeholder, fragmentDemo)
ft.commit()
}
}

16

You might also like