bottom navigation setupWithNavController doesnt work [duplicate] - android

This question already has an answer here:
How to tie fragment destination to menu items in bottom nav bar?
(1 answer)
Closed 1 year ago.
I Use Navigation Components.
I Have A Bottom Navigation And I Want To Switch Between Fragments. But When I Use setupWithNavController Method , Nothing Happens.
Even The Navigation Options Doesn't Change.
setOnNavigationItemSelectedListener Method Works Correctly But I Want To Know What Is My Problem.
Here Is activity_main.xml
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph = "#navigation/nav_home"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="#menu/nav_menu"
/>
MainActivity class :
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
appBarConfiguration =
AppBarConfiguration(setOf(R.id.bottomNavFragment, R.id.infoFragment))
val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNav.setupWithNavController( navController);
}
}
navigation :
<?xml version="1.0" encoding="utf-8"?>
<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_home"
app:startDestination="#id/bottomNavFragment">
<fragment
android:id="#+id/bottomNavFragment"
android:name="com.example.bottomnavtestproject.BlankFragment1"
android:label="Home"
tools:layout="#layout/fragment_blank1" />
<fragment
android:id="#+id/infoFragment"
android:name="com.example.bottomnavtestproject.BlankFragment2"
android:label="Info"
tools:layout="#layout/fragment_blank2" />
</navigation>
menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_favorite"
android:icon="#drawable/ic_baseline_favorite_24"
android:title="Favorite"/>
<item
android:id="#+id/nav_home1"
android:icon="#drawable/ic_baseline_home_24"
android:title="Home"/>
build.gradle :
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.example.bottomnavtest"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
def fragment_version = "1.3.1"
// Kotlin
implementation "androidx.fragment:fragment-ktx:$fragment_version"
def nav_version = "2.3.3"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}

set menu items id's same as in navigation fragments id
For example if in your navigation fragment id android:id="#+id/bottomNavFragment"
In menu xml file item id should be the same, like:
<item android:id="#+id/bottomNavFragment"...

Related

Navigation bug with BottomNavigationView

Navigation from my BottomNavigationView isn't working correctly and I'm stumped.
When I first open the app, it works fine. I click a button in the BottomNavigationView and the corresponding fragment is displayed.
I start seeing the bug after I navigate from fragment A to fragment B using a "go to B" button on fragment A. From then on, when I click the A button in the BottomNavigationView, B is displayed.
Things I've tried:
I overrode all the fragment lifecycle callbacks in A (onCreate(), onCreateView(), onViewCreated(), onViewStateRestored(), onStart(), onResume()). I put breakpoints in each. None are hit when in the buggy state.
I created a new app with the Android template for Bottom Navigation Activity, and reproduced the bug there. The only thing I changed was to add a button on fragment A that navigates to fragment B.
I'm new to both Android and stack overflow so let me know if I'm doing anything silly. Thanks in advance for any help!
Here's the code that reproduces the bug. (Fragment A is the HomeFragment)
HomeFragment.kt
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentHomeBinding
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
val textView: TextView = binding.textHome
homeViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}
return root
}
// This is the code I added
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.setOnClickListener {view ->
view.findNavController().navigate(HomeFragmentDirections.actionNavigationHomeToNavigationDashboard())
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
fragment_home.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=".ui.home.HomeFragment">
<TextView
android:id="#+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- I added this button -->
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dashboard"
app:layout_constraintTop_toBottomOf="#id/text_home"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
mobile_navigation.xml
<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/mobile_navigation"
app:startDestination="#+id/navigation_home">
<fragment
android:id="#+id/navigation_home"
android:name="com.example.myapplication.ui.home.HomeFragment"
android:label="#string/title_home"
tools:layout="#layout/fragment_home" >
<!-- I added this action -->
<action
android:id="#+id/action_navigation_home_to_navigation_dashboard"
app:destination="#id/navigation_dashboard" />
</fragment>
<fragment
android:id="#+id/navigation_dashboard"
android:name="com.example.myapplication.ui.dashboard.DashboardFragment"
android:label="#string/title_dashboard"
tools:layout="#layout/fragment_dashboard" />
<fragment
android:id="#+id/navigation_notifications"
android:name="com.example.myapplication.ui.notifications.NotificationsFragment"
android:label="#string/title_notifications"
tools:layout="#layout/fragment_notifications" />
</navigation>
MainActivity.kt (didn't modify this)
package com.example.myapplication
import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.myapplication.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
activity_main.xml (didn't modify this)
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Top level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// I added everything within the buildscript block
buildscript {
repositories {
google()
}
dependencies {
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.1")
}
}
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle (app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
// I added this line
id 'androidx.navigation.safeargs.kotlin'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.myapplication"
minSdk 26
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
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'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
I figured it out. The fix is to add the last three lines to the action tag in mobile_navigation.xml:
<action
android:id="#+id/action_navigation_home_to_navigation_dashboard"
app:destination="#id/navigation_dashboard"
app:restoreState="true"
app:popUpTo="#id/navigation_home"
app:popUpToSaveState="true"
/>
More information about why this works: https://developer.android.com/guide/navigation/multi-back-stacks#nav-xml
I have searched for a lot of solutions to this problem but none of them worked for me.
Finally, I have found a lazy hack that can "fix" this "bug". I have already tested this solution for a while and no issues have been found so far.
Note: this is NOT an official way to fix this issue. I suggest NOT to use this solution UNTIL none of the solutions worked for you.
Solution:
Replace this line:
navController.navigate(R.id.navigation_dashboard);
with this line:
findViewById(R.id.nav_view).findViewById(R.id.navigation_dashboard).performClick();
R.id.nav_view is the id of the BottomNavigationView component. It could be inside your activity_main.xml.
R.id.navigation_dashboard is the id of the fragment that you want to navigate to. It could be inside your res/navigation/mobile_navigation.xml or res/navigation/nav_graph.xml.

How to solve an error `None of the following functions can be called with the arguments supplied.` with show()

I'm currently developing an application using android-studio with Kotlin.
I want to use a bottom sheet, but when I call the show() method I have an error and I have no idea how to solve it...
When I hover a cursor on the show() I have this error message.
None of the following functions can be called with the arguments supplied.
・show(FragmentManager!, String!) defined in com sample.myprojectname.BottomSheetFragment
・show(FragmentTransaction!, String!) defined in com sample.myprojectname.BottomSheetFragment
How can I solve this error?
Here are the codes:
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.sample.bottomsheetvalue"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.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 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.1'
implementation 'com.android.support:support-v4:28.0.0'
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'
implementation 'com.google.android.material:material:1.0.0'
}
MainActivity.kt
package com.sample.bottomsheetvalue
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val bottomSheet = BottomSheet()
bottomSheet.show(supportFragmentManager,"navigation_bottom_sheet")
//↑ I have the error here
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</android.support.constraint.ConstraintLayout>
BottomSheet.kt
package com.sample.bottomsheetvalue
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
class BottomSheet : BottomSheetDialogFragment()
{
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_bottom_sheet, container, false)
return view
}
}
fragment_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".BottomSheet">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment"/>
</FrameLayout>
Android Studio : 3.3.2

cannot find symbol import yodgorbekkomilov.edgar.footballapp.databinding.FootballItemBindingImpl

I am developing football statics app but when I run the code I am getting following exception
cannot find symbol
import yodgorbekkomilov.edgar.footballapp.databinding.FootballItemBindingImpl;
^
symbol: class FootballItemBindingImpl
below XML class
<?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">
<data>
<variable
name="viewModel"
type="yodgorbekkomilov.edgar.footballapp.ui.FootballViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.clubName}"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.countryName}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.clubValue}"/>
<ImageView
app:imageUrl="#{viewModel.image}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/clubImage"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.europeanTitle}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
below my FootballAdapter.kt where I have used databinding
import android.os.Build
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import yodgorbekkomilov.edgar.footballapp.FootballResponse
import yodgorbekkomilov.edgar.footballapp.R
import yodgorbekkomilov.edgar.footballapp.databinding.FootballItemBinding
import yodgorbekkomilov.edgar.footballapp.ui.FootballViewModel
class FootballAdapter :
RecyclerView.Adapter<FootballAdapter.ViewHolder>() {
private lateinit var footballList: List<FootballResponse>
fun updatePostList(footballList: List<FootballResponse>) {
this.footballList = footballList
notifyDataSetChanged()
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): FootballAdapter.ViewHolder {
val binding: FootballItemBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.football_item,
parent,
false
)
return ViewHolder(binding)
}
override fun getItemCount(): Int {
return footballList.size
}
#RequiresApi(Build.VERSION_CODES.O)
override fun onBindViewHolder(holder: FootballAdapter.ViewHolder, position: Int) {
holder.bind(footballList[position])
}
class ViewHolder(private val binding: FootballItemBinding) : RecyclerView.ViewHolder(binding.root) {
private val viewModel = FootballViewModel()
fun bind(model: FootballResponse) {
viewModel.bind(model)
binding.viewModel = viewModel
}
}
}
below FootballViewModel.kt
import androidx.lifecycle.MutableLiveData
import yodgorbekkomilov.edgar.footballapp.FootballResponse
import yodgorbekkomilov.edgar.footballapp.base.BaseViewModel
class FootballViewModel: BaseViewModel() {
private val clubName = MutableLiveData<String>()
private val countryName = MutableLiveData<String>()
private val clubValue = MutableLiveData<String>()
private val clubImage = MutableLiveData<String>()
private val europeanTitle = MutableLiveData<String>()
fun bind(football: FootballResponse){
clubName.value= football[0].name
countryName.value = football[1].country
clubValue.value = football[2].value.toString()
clubImage.value = football[3].image
europeanTitle.value = football[4].europeanTitles.toString()
}
fun getClubName():MutableLiveData<String>{
return clubName
}
fun getCountryName():MutableLiveData<String>{
return countryName
}
fun getClubValue():MutableLiveData<String>{
return clubValue
}
fun getImage():MutableLiveData<String> {
return clubImage
}
fun getEuropeanTitle():MutableLiveData<String> {
return europeanTitle
}
}
below my app.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "yodgorbekkomilov.edgar.footballapp"
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'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures{
dataBinding = true
viewBinding = true
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
def room_version = "2.2.5"
def nav_version = "2.3.0"
//Navigation component
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
//RxJava
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation "com.squareup.retrofit2:adapter-rxjava2:2.6.1"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
//Room
implementation "androidx.room:room-rxjava2:$room_version"
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
//Dagger2
implementation 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
what I have tried
1.invalidate cache restart
2. I have checked xml carefully it did not solve problem
3. tried following After migration to androidX: Databinding problem (Android Studio 4) and ActivityMainBindingImpl cannot be found in this one as well it did not solve my problem
I want to know where I am making mistake

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}" />

Kotlin-Android-Extension doesn't work across module?

I'm using Kotlint 1.3.50 and Android Studio 3.4
I have my code
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
my_txt.text = "ABC"
}
}
Any my layout as below
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
android:id="#+id/my_txt"
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.support.constraint.ConstraintLayout>
Everything compiles fine if they are in the same module.
However, if I move my layout to an android library, with gradle as below
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
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'
}
I can still import kotlinx.android.synthetic.main.activity_main.* and Android Studio doesn't complaint. But, when I compile, it complaints
Unresolved reference: activity_main
Unresolved reference: my_txt
Looks like the kotlin-extension can't cross use layout from another library?
If I change to findViewById<TextView>(R.id.my_txt).text = "XYZ", it works fine.
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<TextView>(R.id.my_txt).text = "XYZ"
}
}

Categories

Resources