Not able to access toolbar in fragment viewbindig - android

i am facing issue with viewbinding access child of activity toolbar inside fragment.
here i am accessing edittext of toolbar inside my fragment class. but not able to access via view binding. please help me solve out this issue.
activityhome.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.activity.HomeActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_home"
app:layout_constraintTop_toTopOf="parent"/>
<fragment
android:id="#+id/nav_main_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintTop_toBottomOf="#id/toolbar"
app:layout_constraintBottom_toTopOf="#id/actHomeBottomNavigation"
app:navGraph="#navigation/nav_graph_main"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/actHomeBottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/_4sdp"
android:background="#color/black"
android:visibility="visible"
app:elevation="#dimen/_10sdp"
app:itemIconTint="#drawable/selector_color"
app:itemTextColor="#drawable/selector_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
toolbarhome.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbarLayoutFragment"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/blackgrey">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/searchLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<ImageView
android:id="#+id/toolbarBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_back"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<EditText
android:id="#+id/toolbarEditSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Search"
android:textSize="#dimen/_14ssp"
android:background="#android:color/transparent"
android:textColorHint="#color/lightgrey"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#id/toolbarBack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
HomeFragment.kt
class HomeFragment : BaseFragment(), TextWatcher {
lateinit var binding: FragmentHomeBinding
lateinit var editSearch: EditText
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// return inflater.inflate(R.layout.fragment_home, container, false)
binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
editSearch=activity?.findViewById(R.id.toolbarEditSearch)!!
// act?.binding?.toolbar?.toolbarEditSearch?.addTextChangedListener(this)
}
}
error i am getting in logcat is below.
Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
Caused by: java.lang.NullPointerException
at com.webforest.sft.ui.fragment.HomeFragment.onViewCreated(HomeFragment.kt:61)

set your xml code in
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout>
// your other layout or anything
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and use this code in your fagment
private var fragmentFirstBinding: FragmentFirstBinding? = null
in the onCreate
val binding = FragmentBlankBinding.bind(view)
fragmentFirstBinding = binding

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()

Android RecyclerView item width doesn't match parent and preview doesn't match emulator

This is my recycler view items xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/recyclerItem"
app:cardElevation="10dp"
app:cardUseCompatPadding="false"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/recyclerItemImageView"
android:padding="8dp"
android:src="#mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/recyclerItemTextView"
android:layout_marginStart="4dp"
android:textSize="18sp"
android:textColor="#color/black"
android:text="item"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
And preview show me like this:
Preview
But emulator result this:
Emulator result
So items in recycler view does not match parent width on emulator. Other xml's there are:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:layout_margin="16dp"
android:name="com.example.databinding_practice.ListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/activityTextView"/>
<TextView
android:id="#+id/activityTextView"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#color/black"
android:textSize="20sp" />
</RelativeLayout>
And 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=".ListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
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>
Adapter class and ListFragment like this:
RecyclerAdapter.kt:
class RecyclerAdapter(var recyclerItems: ArrayList<RecyclerItemModel>) : RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>() {
class RecyclerViewHolder(val binding: RecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
val binding = RecyclerItemBinding.inflate(LayoutInflater.from(parent.context))
return RecyclerViewHolder(binding)
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
holder.binding.recyclerItemTextView.text = recyclerItems[position].text
recyclerItems[position].imageMipmapSource?.let { holder.binding.recyclerItemImageView.setImageResource(it) }
}
override fun getItemCount(): Int {
return recyclerItems.size
}
}
ListFragment.kt:
class ListFragment : Fragment() {
private var itemList : ArrayList<RecyclerItemModel> = arrayListOf()
private var recyclerAdapter: RecyclerAdapter = RecyclerAdapter(arrayListOf())
private lateinit var fragmentListBinding: FragmentListBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
fragmentListBinding = FragmentListBinding.inflate(layoutInflater)
return fragmentListBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
for (i in 0..3){
itemList.add(RecyclerItemModel(R.mipmap.ic_launcher,"Item $i"))
}
fragmentListBinding.recyclerView.layoutManager = LinearLayoutManager(context)
fragmentListBinding.recyclerView.adapter = recyclerAdapter
recyclerAdapter.recyclerItems = itemList
recyclerAdapter.notifyDataSetChanged()
}
Where am I doing wrong? If I replace match parent to fixed size like 200 dp - 300dp , thats working correctly but match parent does not. And why preview and emulator does not match ? Thank you.
In the fragment you should change layout_width and layout_height to "0dp" in your RecyclerView.
<?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=".ListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
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_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Android: Can not get view from activity in fragment when starting up application [duplicate]

This question already has answers here:
Navigation Component: How to set drawer with toolbar in each fragment
(2 answers)
Closed 1 year ago.
I am using NavHostFragment to display fragments. When I am trying to obtain activity's FloatingActionButton in fragment application crashes.
MainActivity.kt
class MainActivity : AppCompatActivity(), ImageDrawerListDialogFragment.OnImageClickListener {
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // <--
...
}
}
SearchFragment.kt
class SearchFragment : Fragment(), ImageDrawerListDialogFragment.OnImageClickListener {
private lateinit var fab: FloatingActionButton
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
_binding = FragmentSearchBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fab = (requireActivity() as MainActivity).findViewById(R.id.floatingActionButton) // <--
fab.setOnSafeClickListener { requestPermission() }
...
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AppName.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/Theme.AppName.PopupOverlay">
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:imeOptions="actionDone"
android:inputType="text"
app:iconifiedByDefault="false" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:contentDescription="create new search request"
app:layout_anchorGravity="bottom|end"
app:srcCompat="#android:drawable/ic_input_add" />
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/SearchFragment">
...
</navigation>
error
Caused by: android.view.InflateException: Binary XML file line #36 in com.dmytroa.appName:layout/activity_main: Binary XML file line #18 in com.dmytroa.appName:layout/content_main: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #18 in com.dmytroa.appName:layout/content_main: Error inflating class fragment Caused by: java.lang.NullPointerException: requireActivity() as MainActivity).findViewById(R.id.floatingActionButton) must not be null
fab = (requireActivity() as
MainActivity).findViewById(R.id.floatingActionButton) // <--
fab.setOnSafeClickListener { requestPermission() }
I would suggest you not "reach up" from the Fragment into the Activity to operate on a view that exists in its layout. This creates an explicit dependency on that Activity from your Fragment which will break if you every try to use that Fragment anywhere else.
Execute logic that operates on the Activity's views within the Activity itself.
MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fab = findViewById(R.id.floatingActionButton)
fab.setOnSafeClickListener { requestPermission() }
...
}

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)

InflateException while implementing Navigation Library

I am applying navigation library to my project.
And I get this error:
android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
This is MainActivity.kt:
val fragmentManager = supportFragmentManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(savedInstanceState == null){
fragmentManager.beginTransaction().add(R.id.nav_host_fragment, InitFragment()).commit()
}else{
}
}
This is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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">
<data>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:tint="#555"
tools:context="com.example.view.main.MainActivity">
<ImageView
android:id="#+id/iv_flame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<fragment
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_graph"/>
</RelativeLayout>
</layout>
This is InitFragment.kt
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
var binding = DataBindingUtil.inflate<FragmentInitBinding>(inflater, R.layout.fragment_init, container, false)
binding!!.initVm = InitViewModel(this#InitFragment)
var view = binding.root
return view
}
this is fragment_init.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="initVm"
type="com.example.vm.InitViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent"
tools:context=".view.main.fragment.LoginFragment">
</layout>
I don't see any problem with this code. Is there any extra job I have to implement for this? I am using Data Binding. But I don't think I need to it for MainActivity.kt.
MainActivity.kt contains all the fragments. And initFragment.kt will be the first navigation that has menu to navigate.
What should I do?
Edit: In case of JAVA and NOT KOTLIN
after setContentView()
add
Fragment fragment = findViewById(R.id.nav_host_fragment);
Basically an initialization error.
AccountActivity :
class AccountActivity : AppCompatActivity(){
private lateinit var activityAccountBinding: ActivityAccountBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ViewDataBinding>(this,
R.layout.activity_account)
setDataBinder(binding)
setup()
}
override fun setDataBinder(viewDataBinding: ViewDataBinding) {
activityAccountBinding = viewDataBinding as ActivityAccountBinding
// to getting events in this file(Click event)
activityAccountBinding.accountActivity = this
}
override fun setup() {
}
}
activity_account:-
<?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="accountActivity"
type="com.app.presentation.myaccount.activity.AccountActivity"
/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red_fd3d50">
<fragment
android:id="#+id/fragmentContainer"
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_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/my_account_graph"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
my_account_graph:-
<?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/my_account_graph"
app:startDestination="#id/profileFragment">
<fragment
android:id="#+id/profileFragment"
android:name="com.app.presentation.myaccount.ProfileFragment"
android:label="ProfileFragment"
tools:layout="#layout/fragment_profile" />
</navigation>

Categories

Resources