From 0b6d78dfbbc440ae6d6788a3656b3d78dd2e18d6 Mon Sep 17 00:00:00 2001 From: vsuryakumar Date: Fri, 3 Feb 2023 11:22:31 +0530 Subject: [PATCH] Added navigation Drawer. --- .../example/hpos/presentation/MainActivity.kt | 54 ++- .../main/res/drawable/ic_baseline_info_24.xml | 5 + .../res/drawable/ic_baseline_settings_24.xml | 5 + app/src/main/res/layout/activity_main.xml | 386 +++++++++--------- app/src/main/res/layout/nav_header.xml | 21 + app/src/main/res/menu/drawer_list.xml | 17 + app/src/main/res/values/strings.xml | 6 +- 7 files changed, 307 insertions(+), 187 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_info_24.xml create mode 100644 app/src/main/res/drawable/ic_baseline_settings_24.xml create mode 100644 app/src/main/res/layout/nav_header.xml create mode 100644 app/src/main/res/menu/drawer_list.xml diff --git a/app/src/main/java/com/example/hpos/presentation/MainActivity.kt b/app/src/main/java/com/example/hpos/presentation/MainActivity.kt index 79ceba0..da9f22f 100644 --- a/app/src/main/java/com/example/hpos/presentation/MainActivity.kt +++ b/app/src/main/java/com/example/hpos/presentation/MainActivity.kt @@ -10,9 +10,12 @@ import android.os.Bundle import android.util.Log import android.view.Gravity import android.view.Menu +import android.view.MenuItem import android.widget.Toast +import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import androidx.core.view.GravityCompat import androidx.core.view.get import androidx.lifecycle.ViewModelProvider import com.example.hpos.R @@ -58,7 +61,8 @@ class MainActivity : AppCompatActivity() )[MainViewModel::class.java] setSupportActionBar(binding.myToolbar) -// supportActionBar?.setDisplayHomeAsUpEnabled(true) + + setupNavigationDrawer() setupListeners() @@ -68,6 +72,39 @@ class MainActivity : AppCompatActivity() registerReceiver(mUsbReceiver, filter) } + private fun setupNavigationDrawer() { + // Pass the ActionBarToggle action into the drawerListener + val actionBarToggle = ActionBarDrawerToggle(this, binding.drawerLayout, 0, 0) + binding.drawerLayout.addDrawerListener(actionBarToggle) + + // For the Navigation drawer + supportActionBar?.setDisplayHomeAsUpEnabled(true) + + // Call syncState() on the action bar so it'll automatically change to the back button when the drawer layout is open + actionBarToggle.syncState() + + // Call setNavigationItemSelectedListener on the NavigationView to detect when items are clicked + binding.navView.setNavigationItemSelectedListener { menuItem: MenuItem -> + when (menuItem.itemId) { + R.id.about_us -> { + Toast.makeText(this, "Test clicked", Toast.LENGTH_SHORT).show() + true + } + R.id.download_report -> { + Toast.makeText(this, "Download clicked", Toast.LENGTH_SHORT).show() + true + } + R.id.settings -> { + Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show() + true + } + else -> { + false + } + } + } + } + private fun checkAndUpdateUsbConnection() { val availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(getSystemService(Context.USB_SERVICE) as UsbManager) if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.DEVICE_PRODUCT_ID && availableDrivers[0].device.vendorId == Constants.DEVICE_VENDOR_ID){ @@ -112,4 +149,19 @@ class MainActivity : AppCompatActivity() return true } + // override the onSupportNavigateUp() function to launch the Drawer when the hamburger icon is clicked + override fun onSupportNavigateUp(): Boolean { + binding.drawerLayout.openDrawer(binding.navView) + return true + } + + // override the onBackPressed() function to close the Drawer when the back button is clicked + override fun onBackPressed() { + if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) { + binding.drawerLayout.closeDrawer(GravityCompat.START) + } else { + super.onBackPressed() + } + } + } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_baseline_info_24.xml b/app/src/main/res/drawable/ic_baseline_info_24.xml new file mode 100644 index 0000000..1524c2b --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_info_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_baseline_settings_24.xml b/app/src/main/res/drawable/ic_baseline_settings_24.xml new file mode 100644 index 0000000..d1d9d40 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_settings_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c2a9a2a..e2798eb 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,206 +4,222 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> - - - - - - - - - - - - - - - - - - - + android:layout_height="match_parent" + tools:context=".MainActivity"> - + app:layout_constraintTop_toTopOf="parent" + app:menu="@menu/my_menu" + app:popupTheme="@style/ThemeOverlay.AppCompat.Light" + app:titleTextColor="#FFFFFF" /> + app:layout_constraintTop_toBottomOf="@id/my_toolbar" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + \ No newline at end of file diff --git a/app/src/main/res/layout/nav_header.xml b/app/src/main/res/layout/nav_header.xml new file mode 100644 index 0000000..bd3d4cc --- /dev/null +++ b/app/src/main/res/layout/nav_header.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/drawer_list.xml b/app/src/main/res/menu/drawer_list.xml new file mode 100644 index 0000000..c8789c5 --- /dev/null +++ b/app/src/main/res/menu/drawer_list.xml @@ -0,0 +1,17 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index da231af..bd57883 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -60,7 +60,7 @@ Age : %1$s years Results : Negative\ \ - Download\nReport + Download Report Home New Test Start Now @@ -79,4 +79,8 @@ Sickle Find Submit + + About Us + Settings + \ No newline at end of file