Main navigation

Butterknife Android Library Tutorial

adsense

Creating a Butterknife android Project

  1. Go to File → New → New Project and enter your Application Name.
  2. Enter company domain, this is used to uniquely identify your App’s package worldwide.
  3. Choose project location and minimum SDK and on the next screen choose Empty Activity, since we would be adding most of the code ourselves. Then Click on Next.
  4. Choose an Activity Name. Make sure Generate Layout File check box is selected, Otherwise we have to generate it ourselves.Then click on Finish. We have used the Activity Name as ButerknifeActivity for obvious reasons.

Gradle will configure your project and resolve the dependencies, Once it is complete proceed for next steps.

Add Dependencies

  1. Now open your project’s build.gradle from the project’s home directory and add the following dependency.
  2. build.gradle

            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    
  3. Next, open your app’s build.gradle and add the following dependencies in the dependency section in your app’s build.gradle
  4.     apply plugin: 'com.neenbedankt.android-apt'
        compile 'com.jakewharton:butterknife:8.4.0'
        apt 'com.jakewharton:butterknife-compiler:8.4.0'
    

Add Layout and Functionality

  • Open activity_butterknife.xml add the following code. We have only one EditText for accepting the name and one Button for displaying the activity name.
  • activity_butterknife.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
        xmlns:tools="https://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:background="#EEE"
        tools:context="com.androidtutorialpoint.butterknifetutorial.ButterknifeActivity">
        <EditText
            android:id="@+id/editText"
            android:layout_marginTop="100dp"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#FFF"
            android:gravity="center"
            android:hint="Enter your name here" />
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@color/colorAccent"
            android:textColor="#FFF"
            android:textStyle="bold"
            android:layout_marginTop="20dp"
            android:text="SUBMIT" />
    </LinearLayout>
    
  • The content of the ButterknifeActivity will be as follows
  • ButterknifeActivity.java

    package com.androidtutorialpoint.butterknifetutorial;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    
    public class ButterknifeActivity extends AppCompatActivity {
    
        @BindView(R.id.editText)
        EditText toastEditText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_butterknife);
    
            // Initializes Butter Knife and Bind the Views to our Activity
            ButterKnife.bind(this);
        }
        @OnClick(R.id.button)
        public void submit(Button button) {
    
            Toast.makeText(getApplicationContext(), "Hello " + toastEditText.getText().toString(), Toast.LENGTH_SHORT).show();
    
        }
    }
    
    

    We are binding the name editText using the @BindView annotation. In the onCreate() method we initailize and bind the views to the activity using the Butterknife bind method.

    Next we add the OnClickListener() by using the @OnClick annotation and the method that will be executed on the click of the button. Isn’t this really simple ?

    The Butterknife android example app is now complete, we will test the app by adding a name in the EditText and click on the submit button.


    What’s Next ??

    After integrating the Butterknife Android Library you can experiment with other useful android libraries like Dagger – a fast dependency injection library and Glide.
    Till then stay tuned for more tutorials.. and Don’t forget to subscribe our blog for latest android tutorials. Also do Like our Facebook Page or Add us on Twitter.

    To download the full code for theButterknife Android Library Tutorial app, Click on the Download Now link below.

    Android Tutorial Point Download Now


    Reader Interactions

    Leave a Reply

    Advertisment ad adsense adlogger