-2

OnClick linstener using Butterknife is not working when i click button it's not happening any thing,someone please check the code below and suggest me any modifications if any.

MainActivity.java

package com.example.niranjan.sample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.button)
    Button button;
    @BindView(R.id.edit)
    EditText edit;
    @BindView(R.id.text)
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
    @OnClick(R.id.button)
    public void onClick(View view)
    {
        String text1 = edit.getText().toString();
        text.setText(text1);
    }
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.niranjan.sample.MainActivity">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="addtext" />

</LinearLayout>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.example.niranjan.sample"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.jakewharton:butterknife:8.4.0'
    testCompile 'junit:junit:4.12'

}
Niranjan
  • 1
  • 4

3 Answers3

1

I done below steps and successfully implemented butter knife

In build.gradle(project)

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

apply the 'android-apt' and dependency in build.gradle(app)

 apply plugin: 'android-apt'
        android {
          ...
        }

        dependencies {
  compile 'com.jakewharton:butterknife:8.4.0'
  apt 'com.jakewharton:butterknife-compiler:8.4.0'
        }
Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38
1

I had the same problem, try this in your build.gradle(Module.app)

compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

and then apt 'com.jakewharton:butterknife-compiler:8.4.0'

also do not forget

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' in build.gradle(Project)

fastday
  • 15
  • 3
0

You neeed to add apt 'com.jakewharton:butterknife-compiler:8.4.0' under apply plugin in build.gradle

Also add

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

In your project build.gradle inside buildScript->dependencies