so I have a layout like this Layout and I want to access the fab logout on header layout when the drawer is opened in main activity. Is there anyway to do that? thank you so much
Below are my layout and code
main activity xml
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@ id/viewPagerMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.viewpager2.widget.ViewPager2>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/bottomNav"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
drawer layout xml
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/activity_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.navigation.NavigationView
android:id="@ id/navView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/nav_bar" />
</androidx.drawerlayout.widget.DrawerLayout>
header layout xml
<ImageView
android:id="@ id/imgBG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/backgrounds/scenic" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/fabLogOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:clickable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_login_24"
app:tint="@color/white" />
<androidx.cardview.widget.CardView
android:id="@ id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
app:cardCornerRadius="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/fabLogOut">
<ImageView
android:id="@ id/imgProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@ id/txtFullName"
style="@style/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/cardView" />
<TextView
android:id="@ id/txtUsernameHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginBottom="16dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/txtFullName" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this is my mainactivity.kt code
class MainActivity : AppCompatActivity() {
val fragments:ArrayList<Fragment> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
var sharedUname = "com.austin.dailymemedigest"
var shared = getSharedPreferences(sharedUname, Context.MODE_PRIVATE)
var username = shared.getString(LoginActivity.SHARED_USERNAME, null)
super.onCreate(savedInstanceState)
setContentView(R.layout.drawer_layout)
setSupportActionBar(toolbar)
supportActionBar?.title="Daily Meme Digest"
supportActionBar?.setDisplayHomeAsUpEnabled(false)
var drawerToggle =
ActionBarDrawerToggle(this, drawerLayout, toolbar,R.string.app_name,
R.string.app_name)
drawerToggle.isDrawerIndicatorEnabled = true
drawerToggle.syncState()
fragments.add(HomeFragment())
fragments.add(SelfCreationFragment())
fragments.add(LeaderboardFragment())
val adapter = MyViewPagerAdapter(this, fragments)
viewPagerMain.adapter = adapter
viewPagerMain.registerOnPageChangeCallback(
object : ViewPager2.OnPageChangeCallback(){
override fun onPageSelected(position: Int) {
val itemId = bottomNav.menu.getItem(position).itemId
bottomNav.selectedItemId = itemId
}
}
)
bottomNav.setOnItemSelectedListener{
if(it.itemId == R.id.ItemHomeBot){
viewPagerMain.currentItem = 0
} else if(it.itemId == R.id.ItemMyCreationBot){
viewPagerMain.currentItem = 1
} else if (it.itemId == R.id.ItemLeaderboardBot){
viewPagerMain.currentItem = 2
} else{
viewPagerMain.currentItem = 3
}
true
}
val fab = findViewById<View>(R.id.fabLogOut) as? FloatingActionButton
fab?.setOnClickListener {
Toast.makeText(this, "hahahah", Toast.LENGTH_SHORT).show()
}
}
}
i tried this code line in the mainactivity.kt but the toast didn't show up
val fab = findViewById<View>(R.id.fabLogOut) as? FloatingActionButton
fab?.setOnClickListener {
Toast.makeText(this, "hahahah", Toast.LENGTH_SHORT).show()
}
CodePudding user response:
Try accessing fabLogOut
like below with referring to headerview like this,
val navView: NavigationView = findViewById(R.id.navView)
val headerView: View = navView.getHeaderView(0)
val fab = headerView.findViewById<View>(R.id.fabLogOut) as? FloatingActionButton
fab?.setOnClickListener {
Toast.makeText(this, "hahahah", Toast.LENGTH_SHORT).show()
}