I'm developing an Android app, in which I utilize both Android Navigation Component and BottomNavigationView.
The problem is that assigning setupWithNavController to my BottomNavigationView, and when i run the app is crashed and generate java.lang.RuntimeException error
Code
NewsActivity.ky
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.myapplication.databinding.ActivityNewsBinding
import com.example.myapplication.db.ArticleDatabase
import com.example.myapplication.repository.NewsRepository
import kotlinx.android.synthetic.main.activity_news.*
class NewsActivity : AppCompatActivity() {
lateinit var viewModel: NewsViewModel
lateinit var binding : ActivityNewsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityNewsBinding.inflate(layoutInflater)
setContentView(binding.root)
val newsRepository = NewsRepository(ArticleDatabase(this))
val viewModelProviderFactory = NewsViewModelProviderFactory(newsRepository)
viewModel = ViewModelProvider(this, viewModelProviderFactory)[NewsViewModel::class.java]
binding.bottomNavigationView.setupWithNavController(binding.newsNavHostFragment.findNavController())
}
}
activity_news.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.NewsActivity">
<FrameLayout
android:id="#+id/flFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/newsNavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="#navigation/news_nav_graph"/>
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
app:menu="#menu/bottom_navigation_menu"
android:layout_height="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>
**news_nav_graph.xml**
<?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"
android:id="#+id/news_nav_graph"
app:startDestination="#id/breakingNewsFragment">
<fragment
android:id="#+id/articleFragment"
android:name="com.example.myapplication.ui.fragments.ArticleFragment"
android:label="ArticleFragment" />
<fragment
android:id="#+id/breakingNewsFragment"
android:name="com.example.myapplication.ui.fragments.BreakingNewsFragment"
android:label="BreakingNewsFragment">
<action
android:id="#+id/action_breakingNewsFragment_to_articleFragment2"
app:destination="#id/articleFragment"
app:enterAnim="#anim/slide_in_right"
app:exitAnim="#anim/slide_out_left"
app:popEnterAnim="#anim/slide_in_left"
app:popExitAnim="#anim/slide_out_right" />
</fragment>
<fragment
android:id="#+id/savedNewsFragment"
android:name="com.example.myapplication.ui.fragments.SavedNewsFragment"
android:label="SavedNewsFragment">
<action
android:id="#+id/action_savedNewsFragment_to_articleFragment2"
app:destination="#id/articleFragment"
app:enterAnim="#anim/slide_in_right"
app:exitAnim="#anim/slide_out_left"
app:popEnterAnim="#anim/slide_in_left"
app:popExitAnim="#anim/slide_out_right" />
</fragment>
<fragment
android:id="#+id/searchNewsFragment"
android:name="com.example.myapplication.ui.fragments.SearchNewsFragment"
android:label="SearchNewsFragment">
<action
android:id="#+id/action_searchNewsFragment_to_articleFragment2"
app:destination="#id/articleFragment"
app:enterAnim="#anim/slide_in_right"
app:exitAnim="#anim/slide_out_left"
app:popEnterAnim="#anim/slide_in_left"
app:popExitAnim="#anim/slide_out_right" />
</fragment>
</navigation>
bottom_navigation_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/breakingNewsFragment"
android:title="Breaking news"
android:icon="#drawable/ic_breaking_news"/>
<item android:id="#+id/savedNewsFragment"
android:title="Breaking news"
android:icon="#drawable/ic_favorite"/>
<item android:id="#+id/searchNewsFragment"
android:title="Search news"
android:icon="#drawable/ic_all_news"/>
</menu>
Error
2022-06-29 13:55:57.305 19324-19324/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 19324
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.ui.NewsActivity}: java.lang.IllegalStateException: View androidx.fragment.app.FragmentContainerView{b62db28 V.E...... ......I. 0,0-0,0 #7f080148 app:id/newsNavHostFragment} does not have a NavController set
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalStateException: View androidx.fragment.app.FragmentContainerView{b62db28 V.E...... ......I. 0,0-0,0 #7f080148 app:id/newsNavHostFragment} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
at androidx.navigation.ViewKt.findNavController(View.kt:28)
at com.example.myapplication.ui.NewsActivity.onCreate(NewsActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
Related
I have a foreground Service with a button without any activities (initial was closed). When I click button the new activity window is created. The problem is that all buttons in new activity don't work
In foreground service I do:
override fun onClick(p0: View?) {
if (!moving) {
startActivity(Intent(applicationContext, Mmap_activity::class.java)
.setAction(Intent.ACTION_VIEW)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT))
}
}
In Manifest new activity declared:
<activity
android:name=".Mmap_activity"
android:exported="true"
android:theme="#style/ActionBarTheme">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
activity_mmap.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=".Mmap_activity">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/leftBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent=".80"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/bottomBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".10" />
<ImageButton
android:id="#+id/mapexitButton"
style="#style/Widget.AppCompat.ImageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFFFF"
android:contentDescription="#string/Enter"
android:src="#drawable/ic_baseline_close_24"
app:layout_constraintBottom_toBottomOf="#id/bottomBorder"
app:layout_constraintLeft_toLeftOf="#id/leftBorder"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mmap_activity.kt:
class Mmap_activity : AppCompatActivity() {
private lateinit var binding: ActivityMmapBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mmap)
binding = ActivityMmapBinding.inflate(layoutInflater)
binding.mapexitButton.setOnClickListener{
println("button pressed")
}
}
}
How to repair button in new activity? Why it doesn't work?
The error was here.
binding = ActivityMmapBinding.inflate(layoutInflater)
setContentView(binding.root)
In Home Fragment I am having xml as
<androidx.constraintlayout.widget.ConstraintLayout
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/bottomNavi"
app:navGraph="#navigation/bottm_navi" />
<meow.bottomnavigation.MeowBottomNavigation
android:id="#+id/bottomNavi"
app:mbn_circleColor="#color/white"
app:mbn_backgroundBottomColor="#color/app_dark_blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and my bottom navigation is
<?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/bottm_navi"
app:startDestination="#id/homeBottomOneFragment">
<fragment
android:id="#+id/homeBottomOneFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomOneFragment"
android:label="fragment_home_bottom_one"
tools:layout="#layout/fragment_home_bottom_one" />
<fragment
android:id="#+id/homeBottomTwoFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomTwoFragment"
android:label="fragment_home_bottom_two"
tools:layout="#layout/fragment_home_bottom_two" />
<fragment
android:id="#+id/homeBottomFourFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomFourFragment"
android:label="fragment_home_bottom_four"
tools:layout="#layout/fragment_home_bottom_four" />
<fragment
android:id="#+id/homeBottomThreeFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomThreeFragment"
android:label="fragment_home_bottom_three"
tools:layout="#layout/fragment_home_bottom_three" />
<fragment
android:id="#+id/homeFragment2"
android:name="com.krassier.customer.ui.home.HomeFragment"
android:label="HomeFragment" >
<action
android:id="#+id/action_homeFragment2_to_homeBottomOneFragment"
app:destination="#id/homeBottomOneFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomTwoFragment"
app:destination="#id/homeBottomTwoFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomThreeFragment"
app:destination="#id/homeBottomThreeFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomFourFragment"
app:destination="#id/homeBottomFourFragment" />
</fragment>
</navigation>
in homefragment I wrote
findNavController().navigate(R.id.action_homeFragment2_to_homeBottomOneFragment)
when I clicked on bottomnavigation I am trying to load another fragment in homefragment as bottom navigation is in home fragment, but error is:
java.lang.IllegalArgumentException: Navigation action/destination com.krassier.customer:id/action_homeFragment2_to_homeBottomOneFragment cannot be found from the current destination Destination(com.krassier.customer:id/homeFragment) label=fragment_home class=com.krassier.customer.ui.home.HomeFragment
At last I followed conventional way
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = activity?.supportFragmentManager
val fragmentTransaction = fragmentManager?.beginTransaction()
fragmentTransaction?.replace(R.id.homeFragmentContainer, fragment)
fragmentTransaction?.commit()
}
Since you are in a fragment, you should try finding the navController this way.
val navHostFragment = supportFragmentManager.findFragmentById(R.id. fragment) as NavHostFragment. // the id of your FragmentContainerView
navController = navHostFragment.navController
You should take advantage of the NavigationUI library and let it handle the navigation fragment change logic for you
binding.bottomNavi.setupWithNavController(navController)
You can try this:
currentDestination?.getAction(direction.actionId)?.run { navigate(direction) }
}
fun NavController.safeNavigate(
#IdRes currentDestinationId: Int,
#IdRes id: Int,
args: Bundle? = null
) {
if (currentDestinationId == currentDestination?.id) {
navigate(id, args)
}
}
This crash occurs when you make multiple calls the navigate method.
https://nezspencer.medium.com/navigation-components-a-fix-for-navigation-action-cannot-be-found-in-the-current-destination-95b63e16152e
If you want to only just change the fragment like Fragment Transaction using Navigation component, you could do it like this:
private fun getNavController(): NavController {
val navController = findNavController(R.id.fragment)
return navController
}
private fun changeFragment(#IdRes viewId: Int) {
getNavController().popBackStack()
getNavController().navigate(viewId)
}
// how to use it
changeFragment(R.id.homeBottomFourFragment)
I am trying to set up an activity with a fragment with a recyclerview and a botom navigation menu. When I tap on the 'Dashboard', I can see the progress bar and it updates the data from Firestore but when I hit the Quizzes I cannot see the progress bar or new data. All I am seeing is the same data. I think it's not moving to other fragment.
As a beginner I do not know the actual working of fragments and recyclerview I am doing it with the help of people and resources available online.
I couldn't figure out where the problem is. I can provide more codes if needed.
NOTE: I am not getting any error.
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_dashboard"
android:icon="#drawable/ic_vector_dashboard"
android:title="Dashboard" />
<item
android:id="#+id/navigation_quizzes"
android:icon="#drawable/ic_vector_quizzes"
android:title="Quizzes" />
</menu>
mobile_navigation.xml
<?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/mobile_navigation"
app:startDestination="#+id/navigation_dashboard">
<fragment
android:id="#+id/navigation_dashboard"
android:name="com.example.quiz.ui.fragments.DashboardFragment"
android:label="#string/title_dashboard"
tools:layout="#layout/fragment_dashboard" />
<fragment
android:id="#+id/navigation_quiz"
android:name="com.example.quiz.ui.fragments.QuizFragment"
android:label="#string/title_products"
tools:layout="#layout/fragment_quiz" />
</navigation>
activity_dashboard.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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="#drawable/app_gradient_color_background"
app:itemIconTint="#color/colorWhite"
app:itemTextColor="#color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<androidx.fragment.app.FragmentContainerView
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:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_dashboard.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"
android:background="#color/white"
tools:context=".ui.fragments.DashboardFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_dashboard_items"
tools:listitem="#layout/item_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_no_dashboard_items_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="no_dashboard_item_found"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_quiz.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"
android:background="#color/white"
tools:context=".ui.fragments.QuizFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvQuizzesFrag"
tools:listitem="#layout/item_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_no_products_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="NO QUIZZES TODAY"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
DashboardActivity.kt
package com.example.quiz.ui.activities
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.example.quiz.R
class DashboardActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
supportActionBar!!.setBackgroundDrawable(
ContextCompat.getDrawable(
this#DashboardActivity,
R.drawable.app_gradient_color_background
)
)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_quiz,
R.id.navigation_dashboard
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onBackPressed() {
doubleBackToExit()
}
}
Can you tell me where these R.id.navigation_quiz, R.id.navigation_dashboard should be referring to? Whether to the bottom_nav_menu.xml or mobile_navigation.xml.
If you think it's not moving to Quiz Fragment then it might be because your BottomNavigation is not working correctly....
For bottom navigation to work correct with Nav Controller the id's of items in bottom_nav_menu.xml and moile_navigation.xml must be same
In your case the id of dashboard is same in bottom_nav_menu.xml and moile_navigation.xml but for quiz the id's are different in both of these files, it is #+id/navigation_quizzes in bottom_nav_menu.xml and in moile_navigation.xml it is #+id/navigation_quiz
Make the id's same in both files
bottom_nav_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_dashboard"
android:icon="#drawable/ic_vector_dashboard"
android:title="Dashboard" />
<item
android:id="#+id/navigation_quiz"
android:icon="#drawable/ic_vector_quiz"
android:title="Quizzes" />
</menu>
moile_navigation.xml
<?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/mobile_navigation"
app:startDestination="#+id/navigation_dashboard">
<fragment
android:id="#+id/navigation_dashboard"
android:name="com.example.quiz.ui.fragments.DashboardFragment"
android:label="#string/title_dashboard"
tools:layout="#layout/fragment_dashboard" />
<fragment
android:id="#+id/navigation_quiz"
android:name="com.example.quiz.ui.fragments.QuizFragment"
android:label="#string/title_products"
tools:layout="#layout/fragment_quiz" />
</navigation>
Android Studio 3.1, java 1.8, Gradle 4.5.
Here my activity:
public class OrdersActivity extends AppCompatActivity {
ObservableArrayList<OrderEntry> orderList = new ObservableArrayList<>();
#Override
public void setOrderList(List<OrderEntry> list) {
this.orderList.clear();
this.orderList.addAll(list);
}
}
I want to show list's size in layout. So here layout's 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>
<import type="android.view.View" />
<variable
name="handler"
type="com.myproject.ui.OrdersActivity" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="#{handler.orderList.size > 0 ? View.VISIBLE : View.GONE}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</layout>
But I get error:
What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.myproject.ui.OrdersActivity.orderList
file:\app\src\main\res\layout\orders.xml
loc:118:38 - 118:54
****\ data binding error ****
Did you try making it public or wrapping it with a public getOrderList()?
Android Studio 3.1, Java 1.8.
I use data binding from Google.
Here my profile.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="handler"
type="com.myproject.ui.ProfileActivity" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/common_color_bg">
<include
android:id="#+id/prfoileToolbar"
layout="#layout/tool_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="#{#string/profile}" />
</android.support.constraint.ConstraintLayout>
</layout>
Here tool_bar.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="title"
type="String" />
</data>
<android.support.constraint.ConstraintLayout
android:id="#+id/toolBarConstraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="0dp"
android:layout_height="#dimen/tool_bar_height"
android:background="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/toolbaTitleTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:textAllCaps="true"
android:textColor="#android:color/black"
android:textSize="13sp"
android:textStyle="bold"
android:text="#{title}"
app:layout_constraintBottom_toBottomOf="#+id/toolBar"
app:layout_constraintEnd_toEndOf="#+id/toolBar"
app:layout_constraintStart_toStartOf="#+id/toolBar"
app:layout_constraintTop_toTopOf="#+id/toolBar" />
</android.support.constraint.ConstraintLayout>
</layout>
And here my activity:
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProfileBinding binding = DataBindingUtil.setContentView(this, R.layout.profile);
binding.setHandler(this);
}
}
And it's work fine. In included xml (tool_bar) success show text "Profile". Nice.
I do this by create variabel "title" in tool_bar.xml
The question is:
Is it possible to pass string value to tool_bar.xml and set it to "toolbaTitleTextView" without create variable "title" ONLY by xml?