Day53 of #100DaysOfCode

#CodeTogether Day 53/100

Unit 3: Navigation

Pathway 1: Navigate between screens


Words app overview
  • Intro to Intents
  • 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
holder.button.setOnClickListener {
val context = holder.view.context
val intent = Intent(context,
intent.putExtra("letter", holder.button.text.toString())
val letterId = intent?.extras?.getString("letter").toString()
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 = ""
<menu xmlns:android=""
<item android:id="@+id/action_switch_layout"
app:showAsAction="always" />
  • 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)
// 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(, menu)

val layoutButton = menu?.findItem(
// Calls code to set the icon based on the LinearLayoutManager of the RecyclerView

return true
override fun onCreate(savedInstanceState: Bundle?) {

val binding = ActivityMainBinding.inflate(layoutInflater)

recyclerView = binding.recyclerView
// Sets the LinearLayoutManager of the recyclerview
App preview

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store