In every application we use "findviewbyid" get view reference from xml. It could be very lazy task and we dont like at all but it is compulsory to do. Which is boilerplate for every developer. To overcome this problem, We need to use Butterknife library. It saves time and very easy to integrates in existing code.
In general
After use of ButterKnife.
Please feel free to comment and share this post.
In general
public class LoginActivity extends Activity { private Button mEmailSignInButton; private Button xyz; private EditText mPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); xyz = (Button) findViewById(R.id.xyz); mPassword = (EditText) findViewById(R.id.password); } }
After use of ButterKnife.
public class LoginActivity extends Activity { @Bind(R.id.email_sign_in_button) Button mEmailSignInButton; @Bind(R.id.xyz) Button xyz; @Bind(R.id.password) EditText mPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); ButterKnife.bind(this); } }
To Use This Butterknife Just add gradle to your gradle.build file like this
dependencies { // compile 'com.android.support:design:+' compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.jakewharton:butterknife:7.0.1'
}
Other short Cut to remove boilerplate code Please visit shortcuts
Please feel free to comment and share this post.
ConversionConversion EmoticonEmoticon