What is Butterknife Android

Quick tip With butter knife inject views on Android

This quick tip shows you how to incorporate the Butter Knife Library into your projects to easily instantiate the views in your layout in your application's code.

introduction

In every Android application, you need to lay out the method for each view that you want to use in your application's code. However, as the application designs become more complex, the call to this method repeats itself, and this is where the butter knife library comes in.

The Butter Knife library, developed and maintained by Jake Wharton (Square Inc.), contains annotations to help developers instantiate the views of our activities or fragments. It also has annotations to handle events like, etc.

In the sample project of this tutorial, you can see a sample application with an activity and a fragment with an implementation that uses the Butter Knife library and a regular implementation. Let's explore the steps to integrate the butter knife library.

1. Using the Butter Knife Library

Step 1: add the dependency

Add the following dependency to the project build.gradle File:

Compile 'com.jakewharton: butterknife: 6.1.0'

Next, synchronize your project with this file by clicking the Synchronize button.

Step 2: use the annotations

In each activity or fragment, you must delete or comment out each call to the method and add the comment before the declaration of the variable that specifies the identifier of the view.

@InjectView (R.id.sample_textview) TextView sample_textview;

Step 3: inject views

In the method of activity,before using the views, Call on the object.

ButterKnife.inject (this);

If you are using fragments, you need to specify the source of the views in method as shown below.

View view = inflater.inflate (R.layout.sample_fragment, null); ButterKnife.inject (this, view);

You can now use the views in your application code. Butter Knife instantiates every single view for you.

That's all you need to do to use the Butter Knife Library in an Activity or Fragment. In the next section, I'll show you how to use the Butter Knife Library to use list views.

2. Using the butter knife library with list views

The class is a special case that needs to be implemented because you are instantiating the views in an adapter. To integrate the butter knife library into a list view, you must first create the custom layout for the items in the list view. I'll name mine and add the following layout:

In this simple layout we show an image and text. Next we need to create the adapter for the list view. Let's call it.

The public class ListViewAdapter extends the BaseAdapter LayoutInflater inflater; public ListViewAdapter (LayoutInflater inflater) this.inflater = inflater; @Override public int getCount () return 5; @Override public Object getItem (int position) return null; @Override public long getItemId (int position) return 0; @Override public View getView (int position, View convertView, ViewGroup parent) return null; static class ViewHolder Public ViewHolder (view view)

Inside the adapter class there is a static class called to keep it working. We're going to use this class to hold the views. Let's implement the class like this:

static class ViewHolder @InjectView (R.id.image_in_item) ImageView image; @InjectView (R.id.textview_in_item) TextView-Text; public ViewHolder (view) ButterKnife.inject (this, view);

Now we just have to change the method as follows:

public View getView (int position, View convertView, ViewGroup parent) ViewHolder holder; View view = inflater.inflate (R.layout.list_view_item, parent, false); holder = new ViewHolder (view); Picasso.with (inflater.getContext ()) .load ("http://lorempixel.com/200/200/sports/" + (position + 1)) .into (holder.image); holder.text.setText ("This is a text for the picture number:" + position); Return view;

In this method I blow the custom layout in on variable and use it to make an object of the class. Notice that we're using the class to load removed images and fill the text view with some text. The Picasso tutorial can be helpful if you want to familiarize yourself with this library.

Don't forget the permission in the Android manifest. Otherwise Picasso will not be able to connect to the internet and load the remote images.

Finally, all you need to do is instantiate the list view and connect the adapter. I'll do that in a new activity, as shown below. For an example of this implementation, see the source files for this tutorial.

Public class ListViewActivity extends ActionBarActivity @InjectView (R.id.listView) ListView list; @Override protected void onCreate (Bundle savedInstanceState) super.onCreate (savedInstanceState); setContentView (R.layout.activity_list_view); ButterKnife.inject (this); list.setAdapter (new ListViewAdapter ((LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE)));

3. Events

You can also use Butter Knife annotations for events. Choose the annotation you want to use according to the event you want to respond to and prepend the method you want to perform when the event occurs.

@OnClick (R.id.sample_textview) public void showToastMessage () Toast.makeText (MainActivity.this, "This is a message from the activity", Toast.LENGTH_SHORT) .show ();

Conclusion

You can use the Butter Knife method anywhere else you would use the method to save time and avoid code repetition when you need to instantiate the views in the layout. Feel free to share this quick tip if you found it helpful.