Hi Android developers,
For a POC of a components architecture for an Android app, I am building a ui component containing a fragment called 'resultfragment' which is a very simple fragment containing only a textview that displays a popup when you click on it.
Fragment code :
/**
* A simple [Fragment] subclass.
* create an instance of this fragment.
*/
open class ResultFragment : Fragment() {
protected var _binding: FragmentResultBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentResultBinding.inflate(inflater, container, false)
_binding?.lifecycleOwner = this
return _binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding?.ProductTextview?.setOnClickListener {
Toast.makeText(requireContext(), "product details", Toast.LENGTH_LONG).show()
}
}
}
and XML layout :
<?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>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultFragment">
<TextView
android:id="@+id/product_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Now I have the following scenario, I need to write a new android component called 'resultfragmentbutton' that extends that fragment but has an extra button. But the native 'resultfragment' logic needs to remain intact, in this case the click listener on the textview needs to remain.
It's the first time I have to implement such scenario and I am not sure on the approach, any help would be appreciated. Here is the code that I tried, but the native logic of the resultfragment is not working (the listener is not set).
ResultFragmentButton class :
class ResultFragmentButton : ResultFragment() {
private var _Rbinding: FragmentResultButtonBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_Rbinding = FragmentResultButtonBinding.inflate(inflater, container, false)
_Rbinding?.lifecycleOwner = this
return _Rbinding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_Rbinding?.newButton?.setOnClickListener {
Toast.makeText(requireContext(), "Go to website button", Toast.LENGTH_LONG).show()
}
}
}
and 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>
<include layout="@layout/fragment_result">
</include>
<Button
android:id="@+id/new_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="New Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</layout>
Any help would be much appreciated, thanks.