DialogFragment shows nothing when i use custom layout instead of inflation - android

I want to create DialogFragment with custom ui. It's important for me to use custom layout made by myself.
But when returning one from onCreateView DialogFragment shows nothing. By the way when use same code in simple Fragment's child, view shows as expected.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val context = requireActivity()
contentLoaderView = createContentLoaderView(context)
return contentLoaderView
}
private fun createContentLoaderView(context: Context): ContentLoaderView{
return ContentLoaderView(context).apply{
attachContentView(R.layout.dialog_product_order)
attachViewModel(viewLifecycleOwner, viewModel)
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}
}
While when i use the inflater all is nice.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return LayoutInflater.from(context).inflate(R.layout.dialog_product_order, container, false)
}
What am i doing wrong?

Related

Difference between onCreateView and onViewCreated in Fragment - Kotlin

I know this question has been asked many times but just wanted to have some more clarity.
So I wanted to create a simple fragment with a button that should change the fragment when clicked - very simple and basic one.
hence I create a function, and called it on onCreateView. Nothing happeded.
Then I created onViewCreated after onCreateView and called the same function in it.
It Worked.
My Question is What exactly made it work ?
here is the code
class homeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
hello()
}
fun hello()
{
val button = view?.findViewById<Button>(R.id.button_login)
button?.setOnClickListener{
val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
findNavController().navigate(action)
Toast.makeText(context,"wao",Toast.LENGTH_LONG).show()
}
}
}
As per your comments, you did something like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
hello() // the method has already returned a View, so this call is never reached.
}
So, when you call hello() in onCreateView after the return statement,
the method has no effect because of the return statement.
Since onViewCreated is called after the onCreateView,
the underlying View is no more null in your hello function.
If you still want to use onCreateView, you can do something like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragmentView = inflater.inflate(R.layout.fragment_home, container, false)
hello(fragmentView)
return fragmentView
}
fun hello(buttonHolderView: View?) {
val button = buttonHolderView?.findViewById<Button>(R.id.button_login)
button?.setOnClickListener {
val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
findNavController().navigate(action)
Toast.makeText(context, "wao", Toast.LENGTH_LONG).show()
}
}
You need to inflate the view before calling the function:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragmentHome = inflater.inflate(R.layout.fragment_home, container, false)
hello(fragmentHome)
return fragmentHome;
}
Change your hello function:
fun hello(view: View)
{
....

How to return multiple values VIEW in Kotlin?

I was trying to change ImageView in one Fragment from another Fragment. But since this cannot be done without container, I have to create another view value with the inflater method. Then I need to return these view values with the inflater methods. How do I return two views?
code is written in a fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.firstfragment, container, false)
val view2 = inflater.inflate(R.layout.secondfragment, container, false)
val imgPF: ImageView = view2.findViewById(R.id.imageView)
imgPF.setImageResource(R.drawable.image_29)
view.ButtonFromFirstFragment.setOnClickListener{
//Unimportant code
}
setHasOptionsMenu(true)
return view //Approximately here i need to return two views, but how?
//And can it be done somehow differently?
}
Basically you should not have 2 fragments within a fragment. Please double check that you are not confusing fragments with views.
If you want to have those 2 fragments in an activity and you are showing both of your fragments at the same time then you should have 2 separate fragments:
Fragment A:
(...)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.firstfragment, container, false)
view.ButtonFromFirstFragment.setOnClickListener{
//Unimportant code
}
return view
}
(...)
Fragment B:
(...)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.secondfragment, container, false)
val imgPF: ImageView = view.findViewById(R.id.imageView)
imgPF.setImageResource(R.drawable.image_29)
return view
}

BaseFragment with viewbinding in android

I have created a BaseFragment to inflate other fragment's layout using view binding.
I have setup everything but getting an issue mViewBinding is not initialized. Guide me in the right direction.
BaseFragment.kt:
abstract class BaseFragment<VM : ViewModel, VB : ViewBinding> : Fragment() {
protected abstract val mViewModel: VM
protected lateinit var mViewBinding: VB
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mViewBinding = getViewBinding(inflater, container)
return mViewBinding.root
}
abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB
}
DemoFragment.kt:
class DemoFragment : BaseFragment<DemoBaseViewModel, FragmentDemoBinding>() {
override val mViewModel: DemoBaseViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
return mViewBinding.root
}
override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentDemoBinding {
return FragmentDemoBinding.inflate(inflater, container, false)
}
}
With a design like this, your derived class onCreateView() should call to base class super.onCreateView() where the binding is initialised.
Or as Primož Ivančič points out, you don't need to override onCreateView() at all.

Can I get the click event of this layout

I use the following RelativeLayout to display Fragment
<RelativeLayout
android:id="#+id/readfile_relalayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
And my click listener is this, but this click listener is not take effect
readfile_relalayout.setOnClickListener {
showMessage("Test1")
}
This is my fragment class
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = layoutInflater.inflate(R.layout.pdf_frm, container, false)
return view
}
After that, I will use the readfile_relalayout id to make the fragment use this layout as a container, but I have a new requirement, that is, I want to get the click event through the readfile_relalayout id. I have tried to add clickable = "true" to readfile_relalayout but this does not Solve my problem, hope you can help me
Please Try below:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = layoutInflater.inflate(R.layout.pdf_frm, container, false)
view.setOnClickListener {
// write your code here
}
return view
}

What would be the best practices to test this Fragment?

Lets suppose I have a fragment class like this one below named EmailSearchResultPageFragment().
class EmailSearchResultPageFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_email_search_result_page, container, false)
someFunctionThatRequiresViewFromOnCreate(view)
showPage(fragment)
return view
}
fun someFunctionThatRequiresViewFromOnCreate(view: View) {
view.DoSomethingWithView(view)
}
fun showPage(fragment: Fragment) {
fragmentManager!!.beginTransaction().addToBackStack(null).replace(R.id.container, fragment).commit()
}
}
If i want to unit test someFunctionThatRequiresViewFromOnCreate(:), and showPage(:) what would be the best practice ??
How can I possibly mock view and fragment that is being instantiated inside onCreate???!

Categories

Resources