Search code examples
androidkotlinandroid-viewbinding

why i can't put view binding inside onClick listener?


i already define this :

class ProfileFragment : Fragment(), View.OnClickListener {}

and the onclick :

override fun onClick(v: View?) {
        when (v!!.id) {
            binding.tvNavSetting -> {
                avoidDoubleClicks(binding.tvNavSetting)
                val intent = Intent(requireContext(), SettingsActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(intent)
            }
        }
    }

this is my Drawer & inside Profile Fragment.xml :

    <com.google.android.material.navigation.NavigationView
                    app:elevation="10dp"
                    android:id="@+id/nav_view"
                    android:paddingBottom="100dp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="end"
                    android:background="@color/white"
                    android:fitsSystemWindows="false">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">
    
                        <androidx.appcompat.widget.SearchView
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:id="@+id/seach_view"
                            android:background="@drawable/bg_search"
                            android:layout_marginHorizontal="10dp"
                            android:layout_marginVertical="10dp"/>
    
// this is what i try to call://// 
                        <TextView
                            android:id="@+id/tv_nav_setting"
                            android:text="awdadw"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textColor="@color/black" />
    
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:layout_marginStart="20dp"
                            android:layout_marginEnd="40dp"
                            android:background="@drawable/drawer_bg" />
    
                    </LinearLayout>
    
                    <Button
                        android:id="@+id/btn_keluar_profile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Keluar"
                        android:layout_margin="10dp"
                        android:textColor="@color/white"
                        android:background="@drawable/bg_btn_login"
                        android:layout_gravity="bottom"/>
    
                </com.google.android.material.navigation.NavigationView>

i have Text View inside the Drawer Navigation inside Fragment.. i want handle click in that drawer navigation, but the text view can't call by viewBinding.. please have a look the error picture below..

error


Solution

  • sorry , i forgot to add this :

        override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.tv_nav_setting -> {   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                avoidDoubleClicks(binding.tvNavSetting)
                val intent = Intent(requireContext(), SettingsActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(intent)
            }
        }
    }
    
    /*adding this line solve the problem*/
    private fun setDrawer() {
        binding.drawerLayout.setScrimColor(Color.TRANSPARENT)
        binding.tvNavSetting.setOnClickListener(this)  <<<-------
    }