Kotlin databinding does not work and I dont know why - android

I am new to Android development and have been working through an Android development course online. The last lesson taught me how to use fragments. I was able to follow along with the lesson using the provided framework project file. However when I tried to implement the principles in my own project to practice the data binding wouldn't work. There are two errors. The last import gives the error: "unresolved reference: databinding", and then another error is "unresolved reference: FragmentLoginBinding". I have spent the last 5 hours combing through forum posts and documentation but I cannot find my error. Any help would be greatly appreciated thank you.
*Edit: There is no databinding file in my generated java project file
My code is as follows:
fragment_login.kt:
package com.example.project2019
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.example.project2019.databinding.FragmentLoginBinding
class fragment_login : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = DataBindingUtil.inflate<FragmentLoginBinding>(inflater,R.layout.fragment_login,container,false)
return binding.root
}
}
fragment_login.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context="com.example.project2019.fragment_login">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_blank_fragment"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/loginbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Log In"
/>
</LinearLayout>
Build.Gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
dataBinding {
enabled = true
}
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.project2019"
minSdkVersion 19
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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "com.google.android.material:material:$version_material"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

You have to enclose your fragment_login.xml with layout to work data binding
<?xml version="1.0" encoding="utf-8"?>
<layout>
<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="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context="com.example.project2019.fragment_login">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_blank_fragment"
android:layout_gravity="center_horizontal" />
<Button
android:id="#+id/loginbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Log In" />
</LinearLayout>
</layout>

Try adding the following lines to the build.gradle file
buildFeatures {
viewBinding true
}
dataBinding.enabled=true

Related

cannot resolve activitycompat and appcompatactivity

PLS HELP. I think android studio Gradle files or settings got disturbed may be when adding external jar libraries or when I tried to rename a package. Whenever I open new project or even an already finished project Im getting errors.
Even whem i open new project
BUILD FAILED in 24s
6 actionable tasks: 6 executed
ERROR: Manifest merger failed with multiple errors, see logs
in Mainactivity
cannot resolve activitycompat and appcompatactivity
package com.example.android1.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.android1.myapplication"
minSdkVersion 26
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.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/tv"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/tx01"
app:layout_constraintBottom_toBottomOf="#+id/tv"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="go"
app:layout_constraintBottom_toBottomOf="#+id/tv"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
enter image description here
MainActivity Screenshot attached

In current theme failed to find style 'floatingActionButtonStyle'

Failed to find style 'floatingActionButtonStyle' in current theme.
I even tried doing this in my style.xml
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="floatingActionButtonStyle">#style/Widget.Design.FloatingActionButton</item>
</style>
It did not work. I used Base.theme because of appcompat problem I was facing . My appcompat is - 'com.android.support:appcompat-v7:28.0.0-alpha1
Here is the XML file where error is shown:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:src="#drawable/ic_send"
android:id="#+id/fab"
android:tint="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:fabSize="mini"
/>
<android.support.design.widget.TextInputLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_toLeftOf="#+id/fab"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/fab"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
>
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message..."
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<ListView
android:id="#+id/list_of_message"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/fab"
android:dividerHeight="16dp"
android:divider="#android:color/transparent"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true">
</ListView>
</RelativeLayout>
Build.gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.hp.chatapp"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//add library
implementation 'com.android.support:design:27.1.1'
// implementation 'com.firebaseui:firebase-ui-database:4.1.0'
}
apply plugin: 'com.google.gms.google-services'
After all the comments this is what I came up to:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.hp.chatapp"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//add library
implementation 'com.android.support:design:27.1.1'
// implementation 'com.firebaseui:firebase-ui-database:4.1.0'
// consider using implementation 'com.firebaseui:firebase-ui-database:3.1.1'
// see the question below
}
apply plugin: 'com.google.gms.google-services'
I suggest using compileSdkVersion and targetSdkVersion 27 because, as you noticed,
28.0.0-alpha1 doesnt support FloatingActionButton style.
and might also cause problems as it is in alpha version.
Question about firebase-ui-database here.
Extra: You can search for libraries here and see how many people are using it or the release date.

Android Library CardView attribute not found

I have created a library that uses Android CardView.
The library is available as aar file.
When I add the aar file to the project, it gives the following error, on syncing. I still have not used it in the project
attribute for 'cardElevation' not found in com.sampleapp
How do I resolve this namespace issue? So anyone using the library does not have to add cardview to their gradle.
library xml file using cardview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/networkCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardUseCompatPadding="true"
app:cardCornerRadius="#dimen/cardCornerRadius"
app:cardElevation="6dp"
app:cardPreventCornerOverlap="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/card_background"
android:clipToPadding="false">
<ImageView
android:id="#+id/source_type_icon"
style="#style/ServiceConnectUIComponentsTheme"
android:layout_alignParentTop="true"
android:src="#drawable/ic_linkedin3x"
android:tint="#android:color/white"
/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/connect_to_source_rational"
style="#style/ServiceConnectUIComponentsTheme"
android:layout_centerInParent="true"
android:text="Your LinkedIn account allows us to check your professional info, employment & education history"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="#dimen/connect_buttons_extra_margin"
android:layout_marginStart="#dimen/connect_buttons_extra_margin"
android:layout_marginEnd="#dimen/connect_buttons_extra_margin"
android:layout_marginRight="#dimen/connect_buttons_extra_margin"
android:layout_marginTop="#dimen/connect_button_margin"
android:layout_marginBottom="#dimen/circle_indicator_connect_button_margin"
>
<Button
android:id="#+id/access_source"
android:text="Connect LinkedIn"
style="#style/Widget.AppCompat.Button.Colored"
android:background="#drawable/connect_data_button_drawable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp"
android:textAllCaps="false"
/>
<com.UIUtils.HorizontalDottedProgress
android:id="#+id/connectDataProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
></com.UIUtils.HorizontalDottedProgress>
</FrameLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
The sampleapp/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.sampleapp"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
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:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta1'
testCompile 'junit:junit:4.12'
compile project(':UI-release')
}
Thanks
sorry, this answer is late am just putting it others might find it useful in their project.
Any library you used building your library or aar you have to add that library in any project you use the library you build.
let me put it like this.
for instance, I compiled a library that uses card view, I have to add the card view library to any project I want to use my library.

android.support.v7.widget.Toolbar not found (Android Studio)

for round about 2 weeks from now, I receive an error message, whenever I try to preview an .xml design-file in my Android Studio Project. As I am relatively new to the whole topic I'd like to ask whether you have an answer.
The error message:
Rendering Problems:
The following classes could not be found:
- android.support.v7.widget.Toolbar
OR
Rendering Problems:
The following classes could not be found:
- android.support.v7.widget.CardView
One of the .xml files:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar_tabs"
android:background="#color/primaryColor"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="56dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VPlan App RC1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginLeft="16dp"
android:fontFamily="sans-serif-medium"
android:id="#+id/textView7"
android:layout_marginStart="16dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_refresh"
android:id="#+id/ic_refresh"/>
</RelativeLayout>
<com.astuetz.PagerSlidingTabStrip
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/primaryColor"
android:textColorPrimary="#color/white"
android:textSize="14sp"
android:id="#+id/tabs"
android:layout_marginTop="?attr/actionBarSize" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Also as it might help, here's the build.gradle file where the support library is located.
The build.grade file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId 'com.nocomment.vplanehs'
minSdkVersion 10
targetSdkVersion 22
versionCode 16
versionName '0.8.5_RC1'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':volley')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:cardview-v7:22.1.1'
compile 'com.android.support:recyclerview-v7:22.1.1'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.jpardogo.materialtabstrip:library:1.0.9'
}
In this project we also use volley as well as libraries from ZXing's QR Code reader.
I'd be very thankful if someone could help me. Thank you in advance!

Android MapBox inflating error

Hello I try to use MapBox Android SDK, but I have problem.
When APK is launched error shows like : Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.views.MapView
I use Android studio.
My XML Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.views.MapView
android:id="#+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapid="map ID "/>
<LinearLayout
android:id="#+id/llCardInfo"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCffffff"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp"
android:orientation="horizontal">
<LinearLayout
android:orientation="horizontal"
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.99">
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="Hello" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/ivCloseCard"
android:visibility="invisible"
android:src="#drawable/close_card_anim"
android:layout_width="40dp"
android:layout_height="40dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And This is Fragment.
this is my gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dexOptions {
preDexLibraries = false
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.android.gms:play-services:5.0.77'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile project(':lib:SlidingMenu')
compile files('libs/okhttp-2.0.0.jar')
compile files('libs/android-support-v4.jar')
compile files('libs/guava-17.0.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/disklrucache-2.0.2.jar')
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:0.3.0#aar'){
transitive=true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/android-support-v4.jar')
compile files('libs/Parse-1.5.1.jar')
}
The issue is being caused by overlapping versions of OkHttp. Mapbox Android 0.3.0 uses OkHttp 1.x and the provided Gradle dependency lists also contains OkHttp 2.0. This causes confusion at runtime with this error message being the result.
Mapbox Android 0.5.0 has addresses this issue and will allow parallel use of OkHttp 2.0.

Categories

Resources