androidandroid-fragmentscallbacktimepickerdialog

I need to put a DialogFragment inside a Fragment


it's me again. I'm doing a school project and I got some help with my app in Android. I need to put a TimePickerDialog inside a Fragment, at this fragment I have a TextView, I want to put the hour selected here. But I can't find a way to do it. When I do the Listener, it doesn't work.

I tried to change getActivity to another thing that may solve but it doesn't work.

return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute,
                DateFormat.is24HourFormat(getActivity()));

Here is my main fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".DespertarFragment">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="83dp"
        android:layout_marginEnd="83dp"
        android:text="Seleccione la hora"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="162dp"
        android:text="Poner"
        app:layout_constraintBottom_toTopOf="@+id/button_cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="145dp"
        android:layout_marginEnd="162dp"
        android:layout_marginBottom="156dp"
        android:text="Cancelar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</android.support.constraint.ConstraintLayout>

Here is the class:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_despertar, container, false);
        Button poner = (Button) view.findViewById(R.id.button);
        poner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment timePicker = new TimePickerFragment();
                timePicker.show(getFragmentManager(), "Timepo");
            }
        });
        return view;
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        TextView textView = (TextView) view.findViewById(R.id.textView1);
        textView.setText("Hora: " + hourOfDay + minute);
    }

And here is my TimePickerDialog class:

public class TimePickerFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(), HERE I DON'T KNOW WHAT CAN I PUT HERE TO PASS THE TIME TO MY MAIN FRAGMENT, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }
}

NOTE: I don't need the onTimeSet in the TimePickerDialog class, I need the time in the main fragment.

I expect the ViewText with the time but it changed nothing.


Solution

  • Try this, this is a simple way to show time picker

     private void timePick(final TextView textView) {
        TimePickerDialog.OnTimeSetListener timer = new 
        TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
               //set time in your text view
            }
        };
    
        new TimePickerDialog(this,timer,currenthour,currentMinuts,false).show();
    
    }
    

    call this method

     poner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 timePick(yourextView);
            }
        });