Day53 of #100DaysOfCode

#CodeTogether Day 53/100

Unit 3: Navigation

Pathway 1: Navigate between screens

Source: https://developer.android.com/courses/android-basics-kotlin/course

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, DetailActivity::class.java)
}
intent.putExtra("letter", holder.button.text.toString())
context.startActivity(intent)
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 = "https://www.google.com/search?q="
}
<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
}
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()
}
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