Hi, I am working on an app for displaying data from firebase. I have problem in creating touchable menu with items: NAS, Settings, Stats.
MainActivity
package com.smart.smarthome
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import com.google.android.material.navigation.NavigationView
import androidx.drawerlayout.widget.DrawerLayout
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import java.util.*
class MainActivity : AppCompatActivity() {
private var floor = 0
private val floorName = arrayOf("Suterén", "1. Patro", "2. Patro", "3. Patro")
private val database = FirebaseDatabase.getInstance()
private lateinit var navigationView: NavigationView
private lateinit var drawerLayout: DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val leftButton = findViewById<Button>(R.id.left)
val rightButton = findViewById<Button>(R.id.right)
val menuButton = findViewById<Button>(R.id.menuButton)
drawerLayout = findViewById(R.id.drawerLayout)
navigationView = findViewById(R.id.navigationView)
navigationView.setNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.stats -> {
Log.d("SERIAL", "FRAGMENT ZMĚNĚN")
val statsFragment = StatsFragment()
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, statsFragment)
.commit()
}
R.id.nas -> {
Log.d("SERIAL", "FRAGMENT ZMĚNĚN")
val nasFragment = NasFragment()
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, nasFragment)
.commit()
}
R.id.settings -> {
Log.d("SERIAL", "FRAGMENT ZMĚNĚN")
val settingsFragment = SettingsFragment()
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, settingsFragment)
.commit()
}
}
drawerLayout.closeDrawer(GravityCompat.START)
true
}
menuButton.setOnClickListener {
// Open the navigation drawer when menuButton is clicked
drawerLayout.openDrawer(GravityCompat.START)
Log.d("SERIAL", "MENU OTEVŘENO")
}
leftButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View) {
if (floor > 0) {
floor--
updateFloorNav()
updateFloorData()
}
}
})
rightButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View) {
if (floor < 3) {
floor++
updateFloorNav()
updateFloorData()
}
}
})
updateFloorNav();
updateFloorData();
setGreetingBasedOnTime();
}
private fun updateFloorNav() {
val dots1 = findViewById<ImageView>(R.id.dots1)
val dots2 = findViewById<ImageView>(R.id.dots2)
val dots3 = findViewById<ImageView>(R.id.dots3)
val dots4 = findViewById<ImageView>(R.id.dots4)
dots1.visibility = if (floor == 0) View.VISIBLE else View.INVISIBLE
dots2.visibility = if (floor == 1) View.VISIBLE else View.INVISIBLE
dots3.visibility = if (floor == 2) View.VISIBLE else View.INVISIBLE
dots4.visibility = if (floor == 3) View.VISIBLE else View.INVISIBLE
}
private fun updateFloorData() {
val floorNameText = findViewById<TextView>(R.id.floorName)
val floorTempText = findViewById<TextView>(R.id.floorTemp)
val floorHumiText = findViewById<TextView>(R.id.floorHumi)
val floorAirText = findViewById<TextView>(R.id.floorAir)
val dbRefTemp = database.getReference("$floor/teplota")
val dbRefHumi = database.getReference("$floor/vlhkost")
val dbRefCO2 = database.getReference("$floor/co2")
dbRefTemp.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val tempValue = dataSnapshot.getValue(Float::class.java)
floorTempText.text = getString(R.string.temp, tempValue)
}
override fun onCancelled(databaseError: DatabaseError) {
}
})
dbRefHumi.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val humiValue = dataSnapshot.getValue(Float::class.java)
floorHumiText.text = getString(R.string.humi, humiValue)
}
override fun onCancelled(databaseError: DatabaseError) {
}
})
dbRefCO2.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val airValue = dataSnapshot.getValue(Float::class.java)
floorAirText.text = getString(R.string.air, airValue)
}
override fun onCancelled(databaseError: DatabaseError) {
}
})
floorNameText.text = floorName[floor]
}
private fun setGreetingBasedOnTime() {
val greetingText = findViewById<TextView>(R.id.greeting)
val greeting = getGreetingForCurrentTime()
greetingText.text = greeting
}
private fun getGreetingForCurrentTime(): String {
val calendar: Calendar = Calendar.getInstance()
val hour: Int = calendar.get(Calendar.HOUR_OF_DAY)
return if (hour >= 0 && hour < 10) {
"Dobré ráno"
} else if (hour >= 10 && hour < 18) {
"Dobrý den"
} else {
"Dobrý večer"
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
// --- HOMEPAGE LAYOUT ---
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/navigationView"
app:headerLayout="@layout/header_layout"
app:menu="@menu/navigation_items"
/>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>
navigations_items.xml (in /res/menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id = "@+id/stats"
android:title="Přehled"
android:icon="@drawable/baseline_insert_chart_24"
/>
<item
android:id = "@+id/nas"
android:title="NAS"
android:icon="@drawable/baseline_archive_24"
/>
<item
android:id = "@+id/settings"
android:title="Nastavení"
android:icon="@drawable/baseline_settings_24"
/>
</menu>
Example of fragment that should pop up after pressing button in menu
package com.smart.smarthome
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class NasFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_nas, container, false)
}
}