|
1 | 1 | package com.example.avianwatch |
2 | 2 |
|
3 | | - |
4 | 3 | import androidx.appcompat.app.AppCompatActivity |
5 | 4 | import android.os.Bundle |
6 | | -import android.widget.TextView |
7 | 5 | import androidx.fragment.app.Fragment |
8 | 6 | import androidx.navigation.fragment.NavHostFragment |
9 | 7 | import androidx.navigation.ui.setupWithNavController |
| 8 | +import com.example.avianwatch.databinding.ActivityMainBinding |
10 | 9 | import com.example.avianwatch.fragments.BirdFactsFragment |
11 | 10 | import com.example.avianwatch.fragments.PostsFragment |
12 | 11 | import com.example.avianwatch.fragments.GoBirdingFragment |
13 | 12 | import com.example.avianwatch.fragments.HomeFragment |
14 | 13 | import com.example.avianwatch.fragments.ObservationListFragment |
15 | | -import com.google.android.material.bottomnavigation.BottomNavigationView |
16 | 14 |
|
17 | 15 | class MainActivity : AppCompatActivity(), OnCardClickListener{ |
18 | | - private lateinit var bottomNav: BottomNavigationView |
19 | | - lateinit var txtTitle: TextView |
| 16 | + private lateinit var binding: ActivityMainBinding |
20 | 17 |
|
21 | 18 | override fun onCreate(savedInstanceState: Bundle?) { |
22 | 19 | super.onCreate(savedInstanceState) |
23 | | - setContentView(R.layout.activity_main) |
| 20 | + binding = ActivityMainBinding.inflate(layoutInflater) |
| 21 | + setContentView(binding.root) |
24 | 22 |
|
25 | | - txtTitle = findViewById(R.id.txtTitle) |
| 23 | + binding.txtTitle.setText("Dashboard") |
26 | 24 |
|
27 | 25 | //setup the bottom navigation bar |
28 | 26 | val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment |
29 | 27 | val navController = navHostFragment.navController |
30 | | - bottomNav = findViewById(R.id.bottom_navigation) |
31 | | - bottomNav.setupWithNavController(navController) |
| 28 | + |
| 29 | + binding.bottomNavigation.setupWithNavController(navController) |
32 | 30 |
|
33 | 31 | // as soon as the application opens the first fragment should |
34 | 32 | // be shown to the user in this case it is the Home Fragment |
35 | 33 | val fragment = HomeFragment() |
36 | 34 | replaceFragment(fragment) |
37 | 35 |
|
38 | 36 | //change the fragment every time the user presses a different bottom navigation button |
39 | | - bottomNav.setOnNavigationItemSelectedListener { menuItem -> |
| 37 | + binding.bottomNavigation.setOnNavigationItemSelectedListener { menuItem -> |
40 | 38 | // By using switch we can easily get the |
41 | 39 | // selected fragment by using the id |
42 | 40 | lateinit var selectedFragment: Fragment |
@@ -78,7 +76,7 @@ class MainActivity : AppCompatActivity(), OnCardClickListener{ |
78 | 76 | } |
79 | 77 |
|
80 | 78 | fun updateTitle(newText: String) { |
81 | | - txtTitle.text = newText |
| 79 | + binding.txtTitle.setText(newText) |
82 | 80 | } |
83 | 81 |
|
84 | 82 | //method used to replace the current fragment with the fragment passed in the parameters |
|
0 commit comments