Home / Basics / Butter Knife Android Library Tutorial – Library #10
Butter Knife Android Library Tutorial

Butter Knife Android Library Tutorial – Library #10

This is library #8 in this series 100 Library in Android , in the previous article we had EventBus Android Library Tutorial. In this article we are having Butter Knife Android Library Tutorial which is useful when dealing with views, it makes our code much more simple, clean and readable.

What’s Butter Knife?

Butter Knife helps developers to find and automatically cast the corresponding view in your layout, instead of slow reflection and many steps. It makes our code much more simple, clean and readable by just calling bind, it your views are ready to use in our code.

If you are an Android Developer who is tired from writing for every view you have “findViewById(R.id.my_button)” line, then this library will save a lot of your time and make your life easier. Butter Knife is working with Activity, Fragment and Adapters with very simple configurations.

So, if you have a TextView which you need to use it in your activity, then we should do this in this way:

But using Butter Knife, that would be our code. Its much easier and makes you focus on your task not on finding views and making sure that they are bind to its activity.

Butter Knife could be also used to Bind pre-defined resources with  @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt,@BindString, which binds an R.bool ID (or your specified type) to its corresponding field.

Our App:

Our App has one screen with multiple views and resources to use, So we have one Activity which holds an EditText and a Button, and a Fragment which holds a TextView, Button and some Colors. And the Activity has a FrameLayout to show the fragment into it.

Butter Knife Android Library Tutorial

1. Creating Android Project, In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Empty Activity, call it MainActivity and proceed.

2. Install the library using Gradle dependency,

Butter Knife Android Library Tutorial

open Application build.gradle (Marked with red) file and add the following code under dependencies:

and open project build.gradle (Marked with Blue) file and add the following code:

3. Under your package name, create a new blank Fragment call it FragmentA

4. Now, Open your activity_main.xml file and add this code, this will make an EditText,a Button and a FrameLayout to hold the fragment we just created.

5. Open MainActivity.java file and add this code that will Bind our EditText and FrameLayout, and will make a clickListener for our Button.

6. Open your fragment_a.xml file and this code, this will make a TextView and a Button to it in our Fragment

7. Finally, open FragmentA.java file and this code, that will Bind our TextView and some Colors defined already in resources colors.xml file, and will make a clickListener for our Button.

8. Run the project and test it. You should able see The Butter Knife Android Library works and the output should look like this image below, try to define resources like Strings and Dimensions and try to Bind and use them.

Butter Knife Android Library Tutorial

Useful Functions and Features not covered in this example:

1. Want to use Butter Knife in your Adapter ? this is how to make it.

2. Optional Binding,  By default, both @Bind and listener bindings are required. An exception will be thrown if the target view cannot be found. To suppress this behavior and create an optional binding, add a @Nullable annotation to fields or the @Optional annotation to methods.

This could be useful if you are generating views programmatically in run time.

You can find the detailed documentation for this library and full feature list through this link.

Summary
Article Title
Butter Knife Android Library Tutorial - Library 10
Description
Butter Knife Android Library Tutorial it helps find and automatically cast views,It makes our code more simple it works with Activity, Fragment and Adapters
Author
Organization
Android Gifts

About #AndroidGifts

#AndroidGifts

Check Also

Picasso Android Library Tutorial

Picasso Android Library Tutorial – Library #5

This is library #5 in this series 100 Library in Android , in the previous article we …

Leave a Reply