Day53 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which I will build Kotlin words app.
Unit 3: Navigation
Pathway 1: Navigate between screens
Source: https://developer.android.com/courses/android-basics-kotlin/course
Activities and intents
We will start by downloading the repo from the given link in the course and setting it up in Android Studio.
We will be specifically working on LetterAdapter
, WordAdapter
and MainActivity
files
- Intro to Intents
An intent is an object representing some action to be performed. The most common use for intent is to launch an activity. There are two types of intents — implicit and explicit.
- An explicit intent is highly specific, where we know the exact activity to be launched, often a screen in our own app.
- An implicit intent is an a bit more abstract, where we tell the system the type of action, such as opening a link, composing an email, or making a phone call, and the system is responsible for figuring out how to fulfill the request.
- Setting up Explicit Intent
In the LetterAdapter.kt
we will set the onClickListener
for holder.button
in onBindViewHolder()
and Create an Intent
, passing in the context, as well as the class name of the destination activity.
holder.button.setOnClickListener {
val context = holder.view.context
val intent = Intent(context, DetailActivity::class.java)
}
Now we will call the putExtra
method passing in the letter as the first argument and the button text as the second argument
intent.putExtra("letter", holder.button.text.toString())
Then, we will Call the startActivity()
method on the context object, passing in the intent
.
context.startActivity(intent)
Now we will move forward and set up a DetailActivity.
In the onCreate
method of DetailActivity
, after the call to setContentView
, we replace the hardcoded letter with code to get the letterId
passed in from the intent
.
val letterId = intent?.extras?.getString("letter").toString()
Now in the DetailActivity
we will add the following just above the OnCreate
companion object {
const val LETTER = "letter"
val letterId = intent?.extras?.getString(Letter).toString()
intent.putExtra(DetailActivity.LETTER, holder.button.text.toString())}
- Setting up Implicit Intent
companion object {
const val LETTER = "letter"
const val SEARCH_PREFIX = "https://www.google.com/search?q="
}
Then we will set up the menu and icons in the res/Menu/layou_menu
. We will replace the contents of layout_menu.xml
with the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_switch_layout"
android:title="@string/action_switch_layout"
android:icon="@drawable/ic_linear_layout"
app:showAsAction="always" />
</menu>
- Now we will implement the Menu button
private var isLinearLayoutManager = true
private fun chooseLayout() {
if (isLinearLayoutManager) {
recyclerView.layoutManager = LinearLayoutManager(this)
} else {
recyclerView.layoutManager = GridLayoutManager(this, 4)
}
recyclerView.adapter = LetterAdapter()
}
- We will conditionally set the
isLinearLayoutManager
private fun setIcon(menuItem: MenuItem?) {
if (menuItem == null)
return
// Set the drawable for the menu icon based on which LayoutManager is currently in use menuItem.icon =
if (isLinearLayoutManager)
ContextCompat.getDrawable(this, R.drawable.ic_grid_layout)
else ContextCompat.getDrawable(this, R.drawable.ic_linear_layout)
}
- Override
onCreateOptionsMenu
as follows:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.layout_menu, menu)
val layoutButton = menu?.findItem(R.id.action_switch_layout)
// Calls code to set the icon based on the LinearLayoutManager of the RecyclerView
setIcon(layoutButton)
return true
}
Before we run the app. Since the layout manager and adapter are now set in chooseLayout()
, we should replace that code in onCreate()
to call our new method. onCreate()
should look like the following after the change.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
recyclerView = binding.recyclerView
// Sets the LinearLayoutManager of the recyclerview
chooseLayout()
}
This is how app looks like
Today I Learned:
- Explicit intents are used to navigate to specific activities in our app.
- Implicit intents correspond to specific actions (like opening a link, or sharing an image) and let the system determine how to fulfill the intent.
- Menu options allow us to add buttons and menus to the app bar.
- Companion objects provide a way to associate reusable constants with a type, rather than an instance of that type.
That is all for Day53 ✅
Thanks for reading, See you tomorrow!
If you are reading my #100Days Journey, feel free to drop by ;)