Exercise 6 - App Navigation
Exercise 6 - App Navigation
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
4
lateinit var editTextName: TextView
lateinit var btnClickMe: Button
lateinit var btnDial: Button
lateinit var editTextURL: EditText
lateinit var btnURL: Button
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
if (intent != null) {
val data = intent.getStringExtra("MESSAGE")
tvShow.text = "Hello $data"
}
}
}
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
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))
}
}
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>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:textSize="30dp"
android:layout_gravity="center"/>
</FrameLayout>
<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
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
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
fragmentThree().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
<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
16