How to Compress the Java File code in Android Studio?



Bored declaring and finding all your elements by id every time? I have a solution!

I found an awesome android dependency on the internet that will help you alot for compressing your code.
The ButterKnife Dependency
To implement it:
STEP 1:
Add this to your gradle -> dependencies
implementation ‘com.jakewharton:butterknife:(insert latest version)
annotationProcessor ‘com.jakewharton:butterknife-compiler:(insert latest version)
STEP 2:
Make your XML file including all IDs in it.
For example:
<LinearLayout
    android:id="@+id/linear_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"&gt;

   <EditText
                android:id="@+id/edittext_userName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/&gt;

    <EditText
                android:id="@+id/edittext_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/&gt;

    <Button
            android:id="@+id/button_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/&gt;
STEP 3:
STEP 4:
Paste your XML file in the left section of the web page like this:


STEP 5:
Click on the ButterKnife option in the right hand side of the webpage and copy that section codes to your java file of that XML file.


Difference Between Normal Codes and ButterKnife Codes:
Normal code:
/** Android Views /
LinearLayout linearParent;
EditText edittextUserName;
EditText edittextPassword; Button buttonLogin;
/ Android Views **/
/**
Binds XML views
Call this function after setContentView() in onCreate().
**/
private void bindViews(){
linearParent = findViewById(R.id.linear_parent);
edittextUserName = findViewById(R.id.edittext_userName);
edittextPassword = findViewById(R.id.edittext_password);
buttonLogin = findViewById(R.id.button_login);
}
ButterKnife Code:
/** ButterKnife Code /
@BindView(R.id.linear_parent) LinearLayout linearParent; @BindView(R.id.edittext_userName) EditText edittextUserName; @BindView(R.id.edittext_password) EditText edittextPassword; @BindView(R.id.button_login) Button buttonLogin;
/ ButterKnife Code **/
The difference is clear, ButterKnife compresses the code and not only that, now we don’t have to do this boring things each and every time in the program.
Moreover, ButterKnife actually consists of many different functionality which make it easy and simple while keeping setonclicklistners and other methods.
To get more details about ButterKnife, visit :

Related Posts with How to Compress the Java File code in Android Studio?