....... does not have a NavController set - android

When I go to fragmentr_bag using the frag function in mainActivity and then back to home_fragment
And when I want to click on the digital button to go to CategorymodeFragment, this problem occurs
java.lang.IllegalStateException: View android.widget.FrameLayout{dc05058 V.E...... ........ 0,0-1080,2063} does not have a NavController set
mainActivity:
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.hide()
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
try {
binding.re.setOnNavigationItemSelectedListener {
if (it.itemId == R.id.home) {
val fragment1 = homeFragment()
frag(fragment1)
} else if (it.itemId == R.id.bag) {
val fragment3 = bagFragment()
frag(fragment3)
} else if (it.itemId == R.id.person) {
val fragment2 = accontFragment()
frag(fragment2)
}
true
}
}catch (e: Throwable){
}
}
fun frag (fragment: Fragment) {
try {
val manager = supportFragmentManager
val tra = manager.beginTransaction()
tra.replace(R.id.fragmentContainerView, fragment)
tra.commit()
}catch (E:Throwable){}
}
}
homefragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
binding.digital.setOnClickListener() {
Navigation.findNavController(it).navigate(
homeFragmentDirections.actionHomeFragmentToListFragment()
.setSenderCategory("electronics")
)
}
return binding.root
}
mainActivity.xml
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/re"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/menu" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/re"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/navigations" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
fragmenthome.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>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.homeFragment">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="clothing"
android:textColor="#000000"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/mode" />
>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager2"
android:layout_width="409dp"
android:layout_height="188dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.viewpager.widget.ViewPager>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/digital"
android:layout_width="72dp"
android:layout_height="68dp"
android:layout_marginTop="50dp"
android:src="#drawable/digital"
app:civ_border_color="#color/black"
app:civ_border_width="1dp"
app:layout_constraintBottom_toBottomOf="#+id/mode"
app:layout_constraintEnd_toStartOf="#+id/mode"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tahrir"
app:layout_constraintTop_toBottomOf="#+id/recyclerviewProducts/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</FrameLayout>

You need to use activity's navcontroller as navhost fragment is in activity. Library's findNavController() may not work if use FragmentContainerView or FrameLayout.You can try 2 things. You can use the below function in fragment.
If you have FragmentContainerView:
private fun findNavController():NavController? {
val navHostFragment = (requireActivity() as? MainActivity)?.supportFragmentManager?.findFragmentById(R.id.fragmentContainerView) as? NavHostFragment
return navHostFragment?.navController
}
2)You can make it as <fragment> in xml instead of FragmentContainerView and use below function to get NavController.
private fun findNavController() = (requireActivity() as? MainActivity)?.findNavController()
Usage:
binding.digital.setOnClickListener() {
findNavController()?.navigate(
homeFragmentDirections.actionHomeFragmentToListFragment()
.setSenderCategory("electronics")
)
}

Related

Set a Fragment inside a Fragment

I have seen similar questions here, but none of them helps.
I have MainActivity and a few Fragments which works fine, but I want to set one fragment inside of another.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomMenu)
val hostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainer) as NavHostFragment
val navigationController = hostFragment.navController
bottomNavigationView.setupWithNavController(navigationController)
setupActionBarWithNavController(navigationController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragmentContainer)
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Parent Fragment:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
incomeButton = view.findViewById(R.id.addIncomeButton)
expenseButton = view.findViewById(R.id.addExpenseButton)
val childFragment: Fragment = ChartFragment()
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.childFragment, childFragment).commit()
return view
}
Child Fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
return ComposeView(requireContext()).apply {
setContent {
Text(
text = "here is another important code",
fontSize = 28.sp
)
}
}
The layouts for Activity:
<?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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomMenu"
android:layout_width="match_parent"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:menu="#menu/bottom_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainer"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="475dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomMenu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_map" />
</androidx.constraintlayout.widget.ConstraintLayout>
The layouts for Fragment:
<?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=".fragments.MainFragment">
<Button
android:id="#+id/addIncomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginBottom="21dp"
android:backgroundTint="#color/primary"
android:text="#string/addIncomeButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/addExpenseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginBottom="10dp"
android:backgroundTint="#color/red"
android:text="#string/addExpenseButton"
app:layout_constraintBottom_toTopOf="#+id/addIncomeButton"
app:layout_constraintEnd_toEndOf="parent" />
<FrameLayout
android:id="#+id/forChart"
android:layout_width="match_parent"
android:layout_height="350dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The most of the same questions' solvations say to use childFragmentManager so do I but it doesn't work.
I get an error java.lang.IllegalArgumentException: No view found for id 0x7f08023f.
I guess I do not understand something, but I can't realize what.
Thank you!
Seems that you're mixing the ids of the container for the transaction - that's also what the error says. You're trying to put the ChartFragment into a container with R.id.childFragment id:
transaction.replace(R.id.childFragment, childFragment).commit()
And from what I see in the MainFragment layout, the container id is R.id.forChart. Try changing it to this:
transaction.replace(R.id.forChart, childFragment).commit()

Canvas drawing not visible on DialogFragment

I have trying make a custom dialog, and I want to draw some shapes on it's layout. The shapes are drawn fine when I put in an activity, but disappears when I use in the a dialog fragment.
The example below has a button that shows MyDialog() which inflates view_ticket, which contains CanvasView. However, the red dot is not drawn! :(
Below is a screenshot. Notice the red dot in the background, but not in the dialog!
SCREENSHOT
CanvasView.kt
class CanvasView(context: Context, attributeSet: AttributeSet) : LinearLayout(context, attributeSet) {
init {
setWillNotDraw(false)
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
if(canvas == null) return
val p = Paint()
p.color = Color.RED
canvas.drawCircle(400f, 200f, 50f, p)
}
}
MainActivity.kt
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 {
MyDialog().show(supportFragmentManager,null)
}
}
}
class MyDialog : DialogFragment(){
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return View.inflate(context,R.layout.dialog_ticket,container)
}
}
dialog_ticket.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="300dp"
android:background="#color/white"
android:layout_height="400dp">
<TextView
android:layout_width="300dp"
android:layout_height="400dp"
android:text="hello"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.mikkelthygesen.canvassample.CanvasView
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DRAW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.mikkelthygesen.canvassample.CanvasView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

fab for change maptype does nothing when I click

I'm trying to create a fab that when clicked changes my map to terrain type. I set the map to open as satellite instead of default. The button appears but when clicked does nothing.
I set my maptype in onMapReady:
private val callback = OnMapReadyCallback { googleMap ->
googleMap.mapType = GoogleMap.MAP_TYPE_HYBRID
And call my fab in onViewCreated:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(callback)
val fab = binding?.fab
fab?.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View?) {
if (GoogleMap.MAP_TYPE_HYBRID == googleMap.mapType) {
googleMap.mapType = GoogleMap.MAP_TYPE_TERRAIN
}else {
googleMap.mapType = GoogleMap.MAP_TYPE_HYBRID
}
}
})
in layout XML file is define like that:
<?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:contentDescription="#string/title_dashboard"
android:visibility="visible"
tools:context=".ui.dashboard.DashboardFragment">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:context=".ui.dashboard.DashboardFragment" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_baseline_layers_24"
app:backgroundTint="#70FFFFFF"
app:borderWidth="100dp"
app:fabSize="mini"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MAPBOX: The map does not appear when I run the app with no errors (developing in Kotlin - for android - map in a fragment)

I am developing an app in Kotlin for android. I am adding Mapbox to this app.
In particular, I need to insert the Mapbox's map in FragmentHome.
The map needs to interact with other components, ad a navigation bar, and some buttons.
In the HostActivity I inserted
class HostActivity : AppCompatActivity() {
lateinit var googleSignInClient: GoogleSignInClient
private lateinit var navController: NavController
private val mAuth: FirebaseAuth = FirebaseAuth.getInstance()
private val db: FirebaseFirestore = FirebaseFirestore.getInstance()
private lateinit var drawerLayout: DrawerLayout
private lateinit var navViewBinding: DrawerHeaderLayoutBinding
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_host)
val toolbar = customToolbar
setSupportActionBar(toolbar)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
drawerLayout = drawer_layout
navViewBinding = DrawerHeaderLayoutBinding.inflate(layoutInflater, navView, true)
val navHost =
supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
navController = navHost.navController
val navInflater = navController.navInflater
val graph = navInflater.inflate(R.navigation.main_graph)
navController.addOnDestinationChangedListener { _, destination, _ ->
if (destination.id == R.id.onBoarding ||
destination.id == R.id.authFragment ||
destination.id == R.id.loginFragment ||
destination.id == R.id.signUpFragment
) {
toolbar.visibility = View.GONE
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
} else {
toolbar.visibility = View.VISIBLE
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
}
}
if (!Prefs.getInstance(this)!!.hasCompletedWalkthrough!!) {
if (mAuth.currentUser == null) {
graph.startDestination = R.id.authFragment
} else {
getUserData()
graph.startDestination = R.id.homeFragment
}
} else {
graph.startDestination = R.id.onBoarding
}
navController.graph = graph
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
navView.setupWithNavController(navController)
navView.setNavigationItemSelectedListener {
it.isChecked
drawerLayout.closeDrawers()
when (it.itemId) {
R.id.action_logout -> {
MyApplication.currentUser!!.active = false
FirestoreUtil.updateUser(MyApplication.currentUser!!) {
mAuth.signOut()
}
googleSignInClient.signOut()
MyApplication.currentUser = null
navController.navigate(R.id.action_logout)
}
}
true
}
}
private fun getUserData() {
val ref = db.collection("users").document(mAuth.currentUser!!.uid)
ref.get().addOnSuccessListener {
val userInfo = it.toObject(UserModel::class.java)
navViewBinding.user = userInfo
MyApplication.currentUser = userInfo
MyApplication.currentUser!!.active = true
FirestoreUtil.updateUser(MyApplication.currentUser!!) {
}
}.addOnFailureListener {
val intent = Intent(this, MyApplication::class.java)
startActivity(intent)
finish()
}
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController, drawerLayout)
}
}
and the XML graphic part related to the HostActivity is the following:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.instamobile.firebaseStarterKit.ui.activity.host.HostActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/navHostFragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/customToolbar"
style="#style/MyToolbarThemeSimple.Base"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/appBar"
app:navGraph="#navigation/main_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_header_layout"
app:itemIconTint="#color/colorPrimary"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
In the HomeFragment I put only the map and a navigation bar, as you can see in the following code:
class HomeFragment : Fragment() {
private var mapView: MapView? = null
#Nullable
override fun onCreateView(
inflater: LayoutInflater,
#Nullable container: ViewGroup?,
#Nullable savedInstanceState: Bundle?
): View? {
Mapbox.getInstance(
context!!.applicationContext,
"******KEY******"
)
val view: View = inflater.inflate(R.layout.fragment_home, container, false)
mapView = view.findViewById<View>(R.id.mapView) as MapView
mapView!!.onCreate(savedInstanceState)
return view
}
override fun onStart() {
super.onStart()
mapView!!.onStart()
}
override fun onResume() {
super.onResume()
mapView!!.onResume()
}
override fun onPause() {
super.onPause()
mapView!!.onPause()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
}
override fun onLowMemory() {
super.onLowMemory()
mapView!!.onLowMemory()
}
override fun onDestroyView() {
super.onDestroyView()
mapView!!.onDestroy()
}
}
Finally, the XML code related the HomeFragment graphic part is the following:
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragment.home.HomeFragment">
<com.mapbox.mapboxsdk.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="bottom|right"
android:clickable="true"
android:focusable="true"
app:backgroundTint="#4CAF50"
app:srcCompat="#drawable/ic_navigation_white_24dp"
tools:ignore="ContentDescription,MissingConstraints,RtlHardcoded,VectorDrawableCompat" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginBottom="100dp"
android:layout_gravity="right|bottom"
android:clickable="true"
android:focusable="true"
app:backgroundTint="#4CAF50"
app:srcCompat="#drawable/ic_taxi"
tools:ignore="ContentDescription,DuplicateIds,MissingConstraints,RtlHardcoded,VectorDrawableCompat" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_gravity="top|left"
android:clickable="true"
android:focusable="true"
app:backgroundTint="#4CAF50"
app:srcCompat="#drawable/ic_search"
tools:ignore="ContentDescription,DuplicateIds,MissingConstraints,RtlHardcoded,VectorDrawableCompat" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="25dp"
android:layout_gravity="bottom|left"
android:clickable="true"
android:focusable="true"
app:backgroundTint="#4CAF50"
app:srcCompat="#drawable/ic_filter"
tools:ignore="ContentDescription,DuplicateIds,MissingConstraints,RtlHardcoded,VectorDrawableCompat" />
</com.mapbox.mapboxsdk.maps.MapView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
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"
app:layout_constraintTop_toBottomOf="#+id/mapView"
app:menu="#menu/bottom_navigation_menu"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
The code run, with no errors, but the map does not appear.
Are we doing well?
We cannot find any error in the code. Could you?
Many thanks.
Best
I have the same problem.
I resolved it by the use of the call to getMapAsync.
You have to add this code before return view in HomeFragment.
mapView!!.getMapAsync { mapboxMap ->
mapboxMap.setStyle(Style.MAPBOX_STREETS) {
}

IllegalStateException: TabLayoutMediator attached before ViewPager2 has an adapter

fragment_news_details
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="NewsDetailsBinding">
<variable
name="adapter"
type="com.abc.xyz.base.BaseAdapter" />
</data>
<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=".news.NewsDetailsFragment">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/viewPagerGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/imageViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:adapter="#{adapter}"
app:layout_constraintBottom_toBottomOf="#id/viewPagerGuideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/dotsTab"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#color/transparent"
app:layout_constraintBottom_toBottomOf="#id/imageViewPager"
app:layout_constraintEnd_toEndOf="#+id/imageViewPager"
app:layout_constraintStart_toStartOf="#+id/imageViewPager"
app:tabBackground="#drawable/dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
<TextView
android:id="#+id/descriptionTitleTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="#color/textColorPrimary"
android:textSize="#dimen/text_size_20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/viewPagerGuideline"
tools:text="Fresh snow at Dunga" />
<TextView
android:id="#+id/descriptionTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="#color/textColorSecondary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/descriptionTitleTV"
tools:text="Lorem ipsum dolor sit." />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout><?xml version="1.0" encoding="utf-8"?>
NewsDetailsFragment
class NewsDetailsFragment : Fragment() {
lateinit var binding: NewsDetailsBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = inflater.bind(R.layout.fragment_news_details, container)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val templist = ArrayList<String>().apply {
add("https://picsum.photos/id/237/200/300")
add("https://picsum.photos/seed/picsum/200/300")
add("https://picsum.photos/id/1002/300/400")
add("https://picsum.photos/id/237/200/300")
}
binding.adapter =
BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)
if (binding.adapter != null)
TabLayoutMediator(binding.dotsTab, binding.imageViewPager) { tab, position ->
tab.icon = ContextCompat.getDrawable(requireContext(), R.drawable.anim_add_star)
}.attach()
}
inner class NewsImageViewHolder(itemBinding: NewsImageItemBinding) :
BaseViewHolder<String, NewsImageItemBinding>(itemBinding) {
}
}
in on activity created its throwing error TabLayoutMediator attached before ViewPager2 has an adapter. I tried adding the adapter above and below the TabloutMediatorI().attach(), does not work in both cases.What I need is to show dots indicator using tabslayout .I am new using ViewPager 2 ,Any help would be appreciated.Thanks.
ViewPager doesn't have an adapter attribute, so android:adapter="#{adapter}" doesn't set the adapter on the ViewPager.
Instead, you can get the view pager from binding and set the adapter in your fragment like this: binding.imageViewPager.adapter = BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)

Categories

Resources