Issue
I am pretty new to Android development, so please go easy on me, but I have hit a dead-end trying to figure out why I can't add an event listener to any buttons within a fragment. What I am trying to do is I have a Fragment that will serve as the general landing page for my app that has a series of buttons the user should be able to click on that will navigate them to the other pages of the app. All the buttons are there, but I can't get them to do anything. The println statement that is supposed to print out the the id of the current button does work, so the code is entering the switch case, but the setOnClickListener isn't behaving the way I would expect. Here is what I have currently for the code of the Fragment class I am working on (please note there is a closing bracket to end the class I just couldn't get it in the code styling for some reason):
class HomeFragment : Fragment() {
private val buttonFragments = listOf("recipes", "budget", "inventory", "customers",
"reports")
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)
for(button in buttonFragments) {
var currentHomeButton: View
when(button) {
"recipes" -> {
currentHomeButton = view.findViewById(R.id.button_recipes)
println(currentHomeButton.id)
currentHomeButton.setOnClickListener { println("Hello") }
}
}
}
}
Here is what I have tried:
- The implementation you currently are seeing
- Implementing View.OnClickListener and overriding the onClick function
- trying to override the onClick function from within the setOnClickListener call
I hope this enough to go off of, but I can share more if needed.
UPDATE After doing some more exploration, I think I better understand why my issue is occurring, but still can't find how to fix it. The HomeFragment is comprised of a series of custom buttons I have created called HomeButton. HomeButton is made up of a MaterialButton with a bunch of specific styling done to it. When I attach the setOnClick to the id of the material button, it works, but when trying to attach it to my custom component, it doesn't.
Solution
So after a good deal more research and trying out different solutions, I have figured out what the problem is and fixed it. The problem is that I am trying to attach a setOnClickListener on a custom view component I made. Because of this, the setOnClickListener gets attached to nothing (or at least nothing useful), because the function doesn't know how to work with a custom view component. To solve this, I implemented the following code in the HomeButton.kt custom view file:
private var listener: OnClickListener? = null
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_UP) {
if (listener != null) listener!!.onClick(this)
}
return super.dispatchTouchEvent(event)
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.action == KeyEvent.ACTION_UP &&
(event.keyCode == KeyEvent.KEYCODE_DPAD_CENTER ||
event.keyCode == KeyEvent.KEYCODE_ENTER)) {
if (listener != null) listener!!.onClick(this)
}
return super.dispatchKeyEvent(event)
}
override fun setOnClickListener(listener: OnClickListener?) {
this.listener = listener
}
This allows for the setOnClickListener to be used on the HomeButton custom view component. Then back in the HomeFragment.kt fragment file, this was the code I implemented and it worked with no problems:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
for(button in buttonFragments) {
var currentHomeButton: HomeButton
when(button) {
"recipes" -> {
currentHomeButton = view.findViewById(R.id.button_recipes)
currentHomeButton.setOnClickListener { utils.replaceFragment(
RecipesListFragment(), currentHomeButton.getText()) }
}
"budget" -> {
currentHomeButton = view.findViewById(R.id.button_budget)
currentHomeButton.setOnClickListener { utils.replaceFragment(
BudgetListFragment(), currentHomeButton.getText()) }
}
...
}
}
Hope this can help someone in the future
Answered By - Connor Smith
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.