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.
CodePudding user response:
You're using standard output (println(...)
function), which is not being used in Android apps.
Meaning, your click listener works but the code inside is useless unless you execute it manually on your development machine (with main
function).
Learn how to use write logs using LogCat
CodePudding user response:
Suppose we have a Fragment called (TestFragment)
Its layout : fragment_testfragment.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=".presintation.TestFragment">
<TextView
android:id="@ id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxxxxxxxx"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.257" />
<Button
android:id="@ id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b1"
app:layout_constraintBottom_toTopOf="@ id/b2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@ id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b2"
app:layout_constraintBottom_toTopOf="@ id/b3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@ id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b3"
app:layout_constraintBottom_toTopOf="@ id/b4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@ id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="228dp"
android:text="b4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
to init 4 buttons (b1,b2,b3,b4) and change the text with the name of the clicked button the code in the fragment class :
TestFragment.kt:
package com.mostafan3ma.android.hilttesting.presintation
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import com.mostafan3ma.android.hilttesting.R
import kotlin.math.log
class TestFragment : Fragment() {
private val TAG = "TestFragment"
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_testfragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.b1).setOnClickListener {
Log.d(TAG, "onViewCreated: b1 clicked")
Toast.makeText(context, "b1 clicked", Toast.LENGTH_SHORT).show()
view.findViewById<TextView>(R.id.txt).text = "b1"
}
view.findViewById<Button>(R.id.b2).setOnClickListener {
Log.d(TAG, "onViewCreated: b2 clicked")
Toast.makeText(context, "b2 clicked", Toast.LENGTH_SHORT).show()
view.findViewById<TextView>(R.id.txt).text = "b2"
}
view.findViewById<Button>(R.id.b3).setOnClickListener {
Log.d(TAG, "onViewCreated: b3 clicked")
Toast.makeText(context, "b3 clicked", Toast.LENGTH_SHORT).show()
view.findViewById<TextView>(R.id.txt).text = "b3"
}
view.findViewById<Button>(R.id.b4).setOnClickListener {
Log.d(TAG, "onViewCreated: b4 clicked")
Toast.makeText(context, "b4 clicked", Toast.LENGTH_SHORT).show()
view.findViewById<TextView>(R.id.txt).text = "b4"
}
}
}