Can't Resolve AcitivityMainBinding for DataBinding - android

I'm trying to use MVVM Architure Pattern, but when I typed ActivityMainBinding nothing appeared in the suggestion menu
I tried to change the XML file name and changed it back to the original name but nothing happened
here is my dependency
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.alphabet"
minSdkVersion 16
targetSdkVersion 28
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 {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.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'
def lifecycle_version = "2.1.0"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
}
Main Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
}
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">
<data>
</data>
<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" />
</androidx.constraintlayout.widget.ConstraintLayout>
.............................................................

The xml is malformed for data binding, it should be :
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<ConstraintLayout... /> <!-- UI layout's root element -->
</layout>
As per the documentation : https://developer.android.com/topic/libraries/data-binding the root element should be <layout> ... </layout>
And easy way to convert to xml data binding layout is to ALT-Enter on your standard root element view/viewgroup in existing, non data binding layouts and choose the "convert" option.

Related

live view of bottom navigation in design view is not showing icons and the menu resource

[![enter image description here][2]][2]The bottom navigation view is not showing the icons in it, in the live- design view of the android studio as of material io 1.5.0. and I see on my previous projects that in material io 1.3.0 it's working on old but not in the new version.
I am just seeing an empty bottom navigation bar, and nothing on it. I will really appreciate your help in this regard.
here is the code 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=".HomeActivity">
<ImageView
android:id="#+id/img_semirec_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/ic_semirec_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
style="#style/Widget.MaterialComponents.BottomNavigationView.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:itemIconSize="20dp"
app:menu="#menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Gradle:
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
minSdk 26
targetSdk 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation project (':resource')
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
}
This problem occurred while using the new material io 1.5.0 library, and it's working fine on 1.3.0.
Thanks Niaj Mahmud for your help and support
remove
style="#style/Widget.MaterialComponents.BottomNavigationView.Colored"
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:itemIconSize="20dp"
app:menu="#menu/bottom_navigation_menu" />
Update material library to latest version (now 1.6.1) solve your problem :
implementation 'com.google.android.material:material:1.6.1'
Note: You can find the latest version in this link :
https://maven.google.com/web/index.html#com.google.android.material:material

Is it possible to use androidx-navigation with onClick-databinding without writing boilercode in Fragment?

I want to implement an androidx-navigation by using databinding instead of implementing onClick handler in code
I have a List-fragment with a livedata element selectedItemId and want to open a correspondeng detail-fragment when pressing the edit button. Is something similar to this possible
<layout>
<data>
<!-- assuming that com.example.MyGeneratedNavigation was
generated from .../res/navigation/my_navigation.xml -->
<variable name="myNavigation" type"com.example.MyGeneratedNavigation" />
<variable name="selectedItemId" type"..." />
</data>
<LinearLayout ...>
<Button android:id="#+id/edit"
android:onClick="#{() -> myNavigation.onShowDetails(selectedItemId)}" />
</LinearLayout>
</layout>
Is this possible without implementing a onClick-handler in the List-fragment ?
[Update after #ABr answer]
The proposed solution does not trigger the button event. For further investigations i created my own static function to debug if it gets called
<layout ...>
<data>
<import type="de.k3b.androidx.navigationdemo.R" />
<import type="de.k3b.androidx.navigationdemo.GalleryFragment" />
</data>
<RelativeLayout ...>
<Button ...
android:onClick="#{view -> GalleryFragment.myNavigate(view,
R.id.action_gallery_to_editProperties)}"
/>
</RelativeLayout>
</layout>
public class GalleryFragment extends Fragment {
public static final String TAG = "GalleryFragment";
public static void myNavigate(Object view, #IdRes int id) {
// this gets never called neither with `Object view` nor with `View view`
Log.d (TAG, "myNavigate clicked");
// Navigation.findNavController(view).navigate(id);
}
}
Unfortulately the statement Log.d is never called.
If i rename the static method myNavigate in the java code and not in layout i get as expected a compile error so databinding is syntactically ok.
i use this nav graph
<navigation 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/nav_gallery"
app:startDestination="#id/galleryFragment">
<fragment
android:id="#+id/galleryFragment"
android:name="de.k3b.androidx.navigationdemo.GalleryFragment"
android:label="fragment_gallery"
tools:layout="#layout/fragment_gallery" >
<action
android:id="#+id/action_gallery_to_editProperties"
app:destination="#id/editPropertiesFragment" />
</fragment>
<fragment
android:id="#+id/editPropertiesFragment"
android:name="de.k3b.androidx.navigationdemo.EditPropertiesFragment"
android:label="fragment_edit_properties"
tools:layout="#layout/fragment_edit_properties" />
</navigation>
and these build.gradle
global build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
app build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "de.k3b.androidx.navigationdemo"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding.enabled=true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'android.arch.navigation:navigation-fragment:1.0.0'
implementation 'android.arch.navigation:navigation-ui:1.0.0'
}
Yes of course you can. Firstly you should create a connection in the navgraph to which direction you are planning to move.Then you can use the below imports and onclick.
Please note I am not sure if you can pass parameters inside this method!!!!!
<layout>
<data>
<import type="com.rf.comm.R"/>
<import type="androidx.navigation.Navigation"/>
<!-- assuming that com.example.MyGeneratedNavigation was
generated from .../res/navigation/my_navigation.xml -->
<variable name="myNavigation" type"com.example.MyGeneratedNavigation" />
<variable name="selectedItemId" type"..." />
</data>
<LinearLayout ...>
<Button android:id="#+id/edit"
android:onClick="#{view -> Navigation.findNavController(view).navigate(R.id.createServiceFragment)}" />
</LinearLayout>
</layout>

Error recognizing dataBinding class android

in one of my projects I have started to implement dataBinding in the main activity by separating the data in a class called view, in another class called viewModel and in the model class. Using these classes and the dataBinding, the application has been compiled correctly, but when I put the classes in different packages to follow the MVVM architecture, the self-generated class named ActivityLoginBinding does not recognize the class that is inside the ViewModel package. I have already done searches of different projects to see if it was my mistake at syntax level, but they are well written. I have also noticed that all the projects are divided into different packages and work correctly. Any idea why this happens and how could it be solved? Thank you.
my xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="student3"
type="com.juan97.prueba.ViewModel.Student3ViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".View.MainActivity">
<TextView
android:layout_marginTop="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#{student3.hint1}"/>
<TextView
android:layout_marginTop="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#{student3.hint2}"/>
</LinearLayout>
</layout>
gradle module app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.juan97.prueba"
minSdkVersion 19
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'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
}
Different packages I have:
compile error

Androidx error inflating view

I'm trying to use Androidx. The app is pretty new, so there is not much code. I did use the "Refactor to Androidx" option in android studio. But sometime after that, it stopped working. I don't know what made it stop working.
What am I supposed to do?
But it get this error
Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Main activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
pick_image.setOnClickListener {
toast("Pick image clicked")
}
}
}
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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/pick_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Pick image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle (app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.alvarlagerlof.blurr"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Androidx
implementation 'androidx.core:core-ktx:1.0.0-alpha3'
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
EDITED 2: Since the newer version, they've reverted back to androidx.constraintlayout.widget.ConstraintLayout.
Just keep reading if you're using constraint layout version 1.1.1
EDITED: As Arturo Mejia's answer, just press ⇧⌘R or Ctrl + Shift + R to
replace any
androidx.constraintlayout.widget.ConstraintLayout
with
androidx.constraintlayout.ConstraintLayout
in all XML file that use Constraint Layout.
That's a change since constraint layout version v1.1.1 (in v1.1.0 ConstraintLayout class is still inside ".widget" package)
Old workaround answer:
Change from
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
to
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
I tried to press Command + Click at ConstraintLayout in
androidx.constraintlayout.widget.ConstraintLayout from XML file to
show the original class but it doesn't find any thing. After I edited
the constraint layout version to 1.1.0 then it's there.
In my case updating dependency
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
to
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
has helped
I fixed it by changing the gradle dependencies form alpha3 to alpha1

NavHostFragment not resolved (AndroidX)

For some reason I always get confronted with the following error:
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists, is public, and has an empty constructor that is public
I was following the Guide on Android Developers yet the Error above occurs right after starting the App.
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="projects.ferrari.rene.sken.ui.main.MainFragment"
android:label="main_fragment"
tools:layout="#layout/main_fragment" >
<action
android:id="#+id/action_mainFragment_to_takeImageFragment"
app:destination="#id/takeImageFragment" />
</fragment>
<fragment
android:id="#+id/takeImageFragment"
android:name="projects.ferrari.rene.sken.ui.takeimage.TakeImageFragment"
android:label="take_image_fragment"
tools:layout="#layout/take_image_fragment" />
activity_main.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">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="#navigation/nav_graph"
app:defaultNavHost="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package projects.ferrari.rene.sken
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}
override fun onSupportNavigateUp()
= findNavController(R.id.my_nav_host_fragment).navigateUp()
}
build.gradle (app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 'android-P'
defaultConfig {
applicationId "projects.ferrari.rene.sken"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def anko_version = "0.10.5"
def nav_version = "1.0.0-alpha01"
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation 'com.google.android.material:material:1.0.0-alpha1'
implementation 'com.google.android.gms:play-services-vision:15.0.2'
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
implementation 'com.github.pqpo:SmartCropper:v1.1.3#aar'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'
}
Basically I am using Kotlin, AndroidX and targeting Android P. I really can't figure out where the problem lies.
EDIT There might be an issue with the import. For navigation I don't import it using AndroidX. In Build your first App with Jetpack they use implementation 'androidx.navigation:navigation-fragment:' + rootProject.navigationVersion yet I could not figure out which version they were using so I read the Architecture Component Release Notes where it says for AndroidX '2.0.0-alpha1' should be used. Unfortunately this can not be resolved.
For Java:
dependencies {
def nav_version = "2.3+"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
}
For Kotlin:
dependencies {
def nav_version = "2.3+"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}

Categories

Resources