If you are curious about using the ButterKnife Kotlin dependency in Android, then you have come to the right blog. Actually, there is ‘NO NEED‘ for it!

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

ButterKnife Kotlin
ButterKnife makes me hungry!

In Kotlin you can directly access your widgets by their ID names defined in the XML file. Yes you read it right.

Access your widgets by their ID names defined in the XML file

ButterKnife Kotlin Example

Once you have required Kotlin dependency in your project as shown below:

 implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

Learn about using switch case in Kotlin here.

You can easily start building your XML File. In the example below we have 3 Android widgets with id: txt1, txt2, edtxt.

<TextView
    android:id="@+id/txt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginEnd="8dp"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_marginStart="8dp" />

<TextView
    android:id="@+id/txt2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginEnd="8dp"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_marginStart="8dp" />

<EditText
    android:id="@+id/edtxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginEnd="8dp"
    android:onClick="onClick"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_marginStart="8dp" />

Notice how we are accessing these widgets directly:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        // use the kotlin property
        txt1.text="Hi bla bla"
        txt2.text="buubububub"
        //set textcolor  
        txt1.setTextColor(ContextCompat.getColor(this, R.color.colorAccent)) 
        edtxt.hint="nhdodfhfgf"

        txt1.setOnClickListener{ view->
            onClick(view)
        }
    }

    fun onClick(view: View) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
    }

    ...
}

That’s all you need to know about how to magically use butterknife dependency without writing much code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like