Reputation: 1025
I have spent time to search to find the working solution. Some doesn't apply to my case and some doesn't work. That is why I am posting this here.
I am trying to pick a date using the DatePickerFragment in a fragment. I was following the directions explained in Android Developer's site here. I am not getting the code compiled even after following all the steps.
By the way, I am using Navigation Graph to switch between fragment.
Here is what I did and what I faced problem on:
My DatePickerFragment
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val dayOfMonth = c.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(requireActivity(), this, year, month, dayOfMonth)
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, day: Int) {
// Do something with the date chosen by the user
// I want to update the button text of my AddNewTransactionFragment with the date picked
}
}
My AddNewTransactionFragment
The supportFragmentManager is not recognized.
class AddNewTransactionFragment : Fragment(R.layout.fragment_add_new_transaction) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fun showDatePickerDialog(v: View) {
val newFragment = DatePickerFragment()
newFragment.show(supportFragmentManager, "datePicker")
// here this 'supportFragmentManager' is red and the code won't compile with the
// error: Unresolved reference: supportFragmentManager.
// and the function showDatePickerDialog is grayed out even though I have
// android:onClick="showDatePickerDialog" in the corresponding xml file.
}
}
}
My xml file of fragment AddNewTransactionFragment
Here, the button android:onClick= attribute has a method but it is red, which is not recognized from the Fragment code.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="@color/backgroundGray"
android:padding="25dp"
tools:context="com.sukajee.splitexpense.AddNewTransactionFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewAddNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp"
android:text="Add New Transaction"
android:textColor="@color/colorAccent"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewTransactionDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Date"
android:textColor="@color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textViewAddNew" />
<Button
android:id="@+id/buttonTransactionDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friday, 01 January 2021"
app:layout_constraintStart_toStartOf="parent"
android:onClick="showDatePickerDialog"
app:layout_constraintTop_toBottomOf="@id/textViewTransactionDate" />
<EditText
android:id="@+id/editTextDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Description (Grocery, Food, Laundry etc.)"
android:maxLines="1"
android:textColorHint="@color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/buttonTransactionDate" />
<EditText
android:id="@+id/editTextStore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Store Name"
android:maxLines="1"
android:textColorHint="@color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editTextDescription" />
<EditText
android:id="@+id/editTextAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Amount"
android:inputType="numberDecimal"
android:maxLines="1"
android:textColorHint="@color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editTextStore" />
<EditText
android:id="@+id/editTextNote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:hint="Note"
android:inputType="textMultiLine"
android:textColorHint="@color/colorAccent"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editTextAmount" />
<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editTextNote" />
<Button
android:id="@+id/buttonCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cancel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/buttonSubmit" />
<Button
android:id="@+id/buttonClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Clear"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/buttonCancel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
This is what explained in the developer's site. What could be the reason for me not getting the required output even after following everything described? Please help.
Upvotes: 1
Views: 465
Reputation: 2056
You have used supportFragmentManager
which is available in activity, and also you wrote a method inside another method.
Try with this.
class AddNewTransactionFragment : Fragment(R.layout.fragment_add_new_transaction) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
someButton.setOnClickListener{v->showDatePickerDialog(v)}
}
fun showDatePickerDialog(v: View) {
val newFragment = DatePickerFragment()
newFragment.show(childFragmentManager, "datePicker")
// here this 'childFragmentManager' is red and the code won't compile with the
// error: Unresolved reference: supportFragmentManager.
// and the function showDatePickerDialog is grayed out even though I have
// android:onClick="showDatePickerDialog" in the corresponding xml file.
}
}
Upvotes: 0
Reputation: 471
You are trying to use the supportFragmentManager from a Fragment but the supportFragmentManager is only available from an Activity specifically an AppCompatActivity or FragmentActivity.
From a Fragment you could use the childFragmentManager as a FragmentManager but i've never tried to open a DialogFragment from a Fragment.
Upvotes: 1