android package *.databinding does not exist - android

I read more links about this problem, but i dont know whats exactly problem of my project. i set dataBinding as true on build.gradle, below code is my application build.gradle file content
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.text.myapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
repositories {
jcenter()
maven { url "https://jitpack.io" }
maven { url "https://clojars.org/repo/" }
}
def support_library = "25.2.0"
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.android.support:appcompat-v7:${support_library}"
compile "com.android.support:support-v13:${support_library}"
compile "com.android.support:cardview-v7:${support_library}"
compile "com.android.support:recyclerview-v7:${support_library}"
}
My activity:
public class ActivityRegister extends AppCompatActivity {
private ActivityRegisterBinding binding;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
}
}
and then, activity layout
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="presenter"
type="ir.pishguy.cafealachiqpro.Ui.Register.ActivityMain.Model.ActivityRegisterViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="-5dp"
android:orientation="vertical">
<TextView
android:id="#+id/default_port"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="#dimen/default_textview_height"/>
</LinearLayout>
</layout>
Error on Logcat:
/Users/mahdi/Desktop/Home/Projects/Android/CafeAlachiqPro/app/src/main/java/com/text/myapp/Ui/Register/ActivityMain/Presenter/ActivityRegister.java
Error:(11, 45) error: package com.text.myapp.databinding does not exist
Error:(19, 13) error: cannot find symbol class ActivityRegisterBinding
Warning:The following options were not recognized by any processor:
'[android.databinding.artifactType, android.databinding.printEncodedErrors, android.databinding.minApi, android.databinding.isTestVariant, android.databinding.enableDebugLogs, android.databinding.sdkDir, android.databinding.bindingBuildFolder, android.databinding.enableForTests, android.databinding.modulePackage, android.databinding.generationalFileOutDir, android.databinding.xmlOutDir]'
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
My viewModel:
public class ActivityRegisterViewModel extends BaseObservable {
private String readContactPermission;
private String getMessages;
public ActivityRegisterViewModel() {
}
#Bindable
public String getReadContactPermission() {
return readContactPermission;
}
public void setReadContactPermission(String readContactPermission) {
this.readContactPermission = readContactPermission;
notifyChange();
}
public String getGetMessages() {
return getMessages;
}
public void setGetMessages(String getMessages) {
this.getMessages = getMessages;
}
}

Remove
apply plugin: 'com.neenbedankt.android-apt'
and
apt "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
from gradle. This helps me to get rid of this error.
Although I could run your project cause ir.pishguy.cafealachiqpro.Ui.Register.Robot.Model.RobotViewModel can not be resolved from robot_user_action.xml
<data class="UserMessagesDataBinding">
<variable
name="viewModel"
type="ir.pishguy.cafealachiqpro.Ui.Register.Robot.Model.RobotViewModel">
</variable>
</data>

Please mention the variable tag which defines your viewmodel as follows :
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="<variablename>"
type="<packagename.YourViewModel>" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="-5dp"
android:orientation="vertical">
<TextView
android:id="#+id/default_port"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="#dimen/default_textview_height"/>
</LinearLayout>
</layout>

Related

Unable to resolve reference BR at runtime databinding android

I am trying to use databinding with MVVM in my android app but it is giving me an error: "Unable to resolve reference BR" at runtime while i can see the class already exist in the project.
build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.abc.xxx"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
}
ViewModel class:
class SearchViewModel : BaseObservable() {
private var searchModel = SearchModel("")
fun setSearchText(text: String){
searchModel.text = text
// notifyPropertyChanged(B)
notifyPropertyChanged(BR._all)
}
#Bindable
fun getSearchText(): String{
return searchModel.text
}
fun onSearchClicked(){
if(searchModel.text.isNullOrEmpty())
setSearchText("Error!!!")
}
}
Activity class:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bind : ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
bind.viewModel = SearchViewModel()
bind.executePendingBindings()
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.abc.xxx.viewmodels.SearchViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/inImageSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:maxLines="1"
android:text="#={viewModel.userText}" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="#{()-> viewModel.onSearchClicked()}"
android:text="#string/search"
/>
</LinearLayout>
</layout>
So what are the changes i should do to make it work? I have tried many SO threads already but none of them is working. Please help me out.
It appears you are binding to a property that doesn't exit. You have the userText in your xml which doesn't exist. Also you are binding to a model of the viewModel, so you need to make sure your SearchModel also implements BaseObservable.
Then make sure it is not private in your viewModel or that you write appropriate getters to retrieve it. The get and set Names MUST match the property name that is labeled with bindable.
Also just an architecture comment. I'm not sure why you would create a searchViewModel and a searchModel. Seems sort of redundant, but maybe you have reasons.
Example SEARCH MODEL below
class SearchModel : BaseObservable() {
#Bindable
private var searchText = ""
fun setSearchText(text: String){
searchModel.text = text
notifyPropertyChanged(BR.searchText)
}
fun getSearchText(): String{
return searchModel.text
}
}
Then your XML needs to be bound correctly to the models property
<EditText
android:id="#+id/inImageSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:maxLines="1"
android:text="#={viewModel.searchModel.searchText}" />

error when data binding in android causes cannnot find symbol ActivityMainBindingImpl

So, I'm trying to create an onclick listener on a button using databinding using this tutorial.
databinding tutorial so as far as when I keep the onclick inside my activity_main.xml layout the button click works. But I want to send my data to an include_main_layout.xml but it tells me I have the error..
build.gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
dataBinding {
enabled = true
}
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.datahelpsworld"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
User user;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
user = new User();
user.setEmail("Ebola chebola chernobyl caronvirus");
user.setName("dilo dilo bolo bolo");
binding.setUser(user);
MyClickHandlers handlers = new MyClickHandlers(this);
binding.setHandlers(handlers);
}
public class MyClickHandlers {
Context context;
public MyClickHandlers(Context context) {
this.context = context;
}
public void onFabClicked(View view) {
user.setName("Ashutosh is awesome");
user.setEmail("email address is magical and cannot be retrieved");
Toast.makeText(getApplicationContext(), "FAB clicked!", Toast.LENGTH_SHORT).show();
}
public void onButtonClick() {
Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();
}
public void onButtonClickWithParam(View view, User user) {
Toast.makeText(getApplicationContext(), "Button clicked! Name: " + user.getName(), Toast.LENGTH_SHORT).show();
}
public boolean onButtonLongPressed(View view) {
Toast.makeText(getApplicationContext(), "Button long pressed!", Toast.LENGTH_SHORT).show();
return false;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="handlers"
type="com.example.datahelpsworld.MainActivity.MyClickHandlers" />
<variable
name="user"
type="com.example.datahelpsworld.User" />
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<include
layout="#layout/main_layout"
bind:handlers="#{handlers}"
bind:user="#{user}" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:longClickable="true"
android:onClick="#{(v)->handlers.onFabClicked()}"
android:text="activity main layout">
</Button>
</LinearLayout>
</layout>
main_layout.xml
<layout>
<data>
<variable
name="user"
type="com.example.datahelpsworld.User" />
<variable
name="handlers"
type="com.example.datahelpsworld.MainActivity.MyClickHandlers" />
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{user.name}">
</TextView>
<TextView
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{user.email}">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#={(v)->handlers.onButtonClick()}"
android:text="inside include">
</Button>
</LinearLayout>
</layout>
my error is the following lines..
error: cannot find symbol class ActivityMainBindingImpl
I tried cleaning the project and rebuilding from the ground base but still no luck. What is it that I'm doing wrong?
Problem is here.
android:onClick="#{(v)->handlers.onFabClicked()}"
it should be android:onClick="#{handlers.onFabClicked()}" simple.
If you want to pass view parameter from xml then your onClick methos should be like below.
android:onClick="#{(v)->handlers.onFabClicked(v)}"

Databingding V2 and kotlin,I can not find my BR?

Yesterday, I updated my Android Studio to 3.1. So I can only use Databinding V2.I can build successfully in Java.But fail in Kotlin.It told me Unresolved reference: BR
layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="mainViewModel"
type="com.example.shao.myapplication.ViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{mainViewModel.title}"/>
</LinearLayout>
</layout>
in Java
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setVariable(BR.mainViewModel, new ViewModel());
}
public static int getMainBr() {
return BR.mainViewModel;
}
}
in kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mBinding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
mBinding.setVariable(BR.mainViewModel, ViewModel())
}
}
Android’s Data Binding with Kotlin
First of all, after having a created Android project in Android Studio, we need to add the Data Binding dependency and the ones of Kotlin to the build.gradle file of our app.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
....
dataBinding {
enabled = true
}
}
dependencies {
...
// notice that the compiler version must be the same than our gradle version
kapt 'com.android.databinding:compiler:2.3.1'
}
First we need to create a model. In this case a basic one like User.kt
data class User(val name: String, val age: Int)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<variable
name="user"
type="com.kuma.sample.User"
/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.kuma.sample.MainActivity"
>
<TextView
android:id="#+id/user_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#{user.name}"
tools:text="Name"
/>
<TextView
android:id="#+id/user_age_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#{Integer.toString(user.age)}"
tools:text="XX"
/>
</LinearLayout>
</layout>
Kotlin Class
package com.kuma.sample
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.kuma.kotlinsteps.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val user = User("Kuma", 23)
binding.setVariable(BR.user, user)
binding.executePendingBindings()
}
}
Solve Error Br
enter code hereapply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.example.icarusud.recyclerviewtest"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled true
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//recyclerview
compile "com.android.support:recyclerview-v7:24.1.1"
//lastadapter
compile 'com.github.nitrico.lastadapter:lastadapter:1.1.1'
kapt "com.android.databinding:compiler:2.1.2"
}
repositories {
mavenCentral()
}
kapt { generateStubs = true }
Change your gradle setting according to below code try this code it helps you.
change
mBinding.setVariable(BR.mainViewModel, ViewModel())
to
mBinding.mainViewModel=ViewModel()
I worked around this issue by adding a BRUtils.java, then in the Kotlin files, accessing BR values in BRUtils.java
BRUtils.java:
/**
* since {#link BR} cannot be resolved in kotlin files during compile time
* for some reason, created this file to hold getters for {#link BR} fields
* that can then be accessed from said kotlin files.
*/
public class BRUtils {
private BRUtils() {}
public static int getMainViewModel() {
return BR.mainViewModel;
}
}
something.kt:
binding.setVariable(BRUtils.getMainViewModel(), ViewModel())

Error:(7, 36) error: package example.com.test.databinding does not exist

This is the First time i am using Data Binding ,and from Couple of Hours i am having above error. There is Sufficient question related to same Question but Not able to Solve this issue.
Gradle Module APP
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "example.com.test"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Main_xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="example.com.test.MainActivity" />
<import type="android.view.View" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.firstName}" />
<TextView
android:id="#+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.lastName}" />
</LinearLayout>
</layout>
Modal Class
public class User {
public final String firstName;
public final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
Main Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String sMan = "Test";
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User mainActivityPresenter = new User(sMan,sMan);
activityMainBinding.setUser(mainActivityPresenter);
}
}
Image is added of log
I am not able to solve this issue.How can this issue be solved??
There are lots of things incorrect in your implementation.
Your model class should extend BaseObservable.
Your getters should be #Bindable
Your setters should call notifyPropertyChanged(..)
In your xml, you are saying that "user" is a example.com.test.MainActivity, while I think it is a User?
User mainActivityPresenter = new User(sMan,sMan); is also very weird. Call a User "user" and a Presenter "xxxPresenter". Don't call a User "presenter" if you want to write clear code.
... these are some things I see at a glance, there's probably more. Please read the documentation completely first, it will make your implementation a lot easier:
https://developer.android.com/topic/libraries/data-binding/index.html
Its the Simple and Silly Error i have Done this Time.
Solved with Changing the MainActivity to User
<data>
<variable
name="user"
type="example.com.test.User" />
<import type="android.view.View" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.firstName}" />
<TextView
android:id="#+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.lastName}" />
</LinearLayout>

Butterknife not finding views from Android Library module

I have 3 modules in my project: common, admin and client.
The first is an Android Library with an abstract LoginActivity that the other modules (apps) extends from.
LoginActivity has its own layout and all methods implemented but one: tryLogin(user, password) that navigates to the main activity, which is different for each app.
I have followed the official documentation but when I click on signIn button the OnClickListener is not called. Moreover, all bound views are null...
This are all related code from admin (both modules are almost identical)
Project level build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath "io.realm:realm-gradle-plugin:1.1.1"
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.2.1'
classpath 'io.fabric.tools:gradle:1.21.7'
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Common library build.gradle
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'android-apt'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "0.1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
// Local
compile fileTree(dir: 'libs', include: ['*.jar'])
...
compile 'com.jakewharton:butterknife:8.2.1'
}
Admin app build.gradle
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'android-apt'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "..."
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "0.1"
}
buildTypes {
debug {
applicationIdSuffix '.debug'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
...
// Butterknife
compile 'com.jakewharton:butterknife:8.2.1'
apt 'com.jakewharton:butterknife-compiler:8.2.1'
}
LoginActivity (common)
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;
import xxx.common.R;
import xxx.common.R2;
import butterknife.BindView;
import butterknife.OnClick;
public abstract class LoginActivity extends BaseActivity {
#BindView(R2.id.login_user_form)
protected EditText user;
#BindView(R2.id.login_password_form)
protected EditText password;
#BindView(R2.id.login_sign_in)
protected Button signIn;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
}
#OnClick(R2.id.login_sign_in)
protected void onSignInClick() {
String user = this.user.toString();
String password = this.password.getText().toString();
/* Fields validations ... */
tryLogin(user, password);
}
protected abstract void tryLogin(String user, String password);
}
activity_login.xml (common)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="0.4"
android:scaleType="fitCenter"
android:src="#drawable/brand" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/login_user_form"
style="#style/LoginEditText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/login_password_form"
style="#style/LoginEditText" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_sign_in"
style="#style/LoginButton"
android:text="#string/login_sign_in" />
</LinearLayout>
</LinearLayout>
AdminLoginActivity (admin)
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import xxx.activity.LoginActivity;
import butterknife.ButterKnife;
public class AdminLoginActivity extends LoginActivity {
#Override
protected void tryLogin(String user, String password) {
// This is never called!!
Toast.makeText(this, "Login in...", Toast.LENGTH_SHORT).show();
}
}
Te application is not crashing or anything, it does just nothing.
What am I doing wrong??
My guess is that the code has the wrong R file being imported.
To verify this, you can look at the R file that is automagically built in the build/generated/source of the library module in issue and see if the id your looking for is included. There are several R files generated. Find the one that has the id your looking for and make sure it matches your R import namespace.

Categories

Resources