Search code examples
android-studioandroid-lint

How to avoid complier warning Parameter 'x' is never used


Suggest the best way to solve this type of compiler warning in Android Studio 3.3

Note: I found many solutions to avoid the warnings and uncheck the Studio inspection. But I am expecting something different.

Example: If warning Parameter 'view1' is never used is in the button onClick method.

Method

fun buttonClicked(view1: View) {
//   Call Intent to new Activity . 
// Parameter **view1** is not used  }

Call method from XML

 <Button
 android:id="@+id/button_id"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:onClick="@{viewModel::buttonClicked}"
 android:text="@string/example" />.

So we need (view1: View) parameter for the method for onClick. But it is not used. How can I solve this warning?. (Just one example).


Solution

  • It took me a few days to figure out the exact way to manage this warning without using @SuppressWarnings("unused")

    android:onClick="@{viewModel::buttonClicked}" is equal to android:onClick="@{(v) -> viewModel.buttonClicked(v)}, both were calling same method fun buttonClicked(view1: View) in Kotlin. (just try to mention difference between '::' and '.' usage)

    So I fixed my warning my calling a method explicitly instead of using -'::'

    fun buttonClicked() // Button onClick function
    

    It will be called from XML - >android:onClick="@{(v) -> viewModel.buttonClicked()}". Thus "Parameter view1 is never used" will not occur any more.

    Simple Fix !! Happy Coding